@microsoft.azure/autorest.incubator
Version:
AutoRest incubator project
235 lines • 8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const dictionary_1 = require("../common/dictionary");
const file_generator_1 = require("../common/file-generator");
let indentation = ' ';
exports.lineCommentPrefix = '//';
exports.docCommentPrefix = '///';
exports.EOL = '\n';
exports.CommaChar = ', ';
/** joins an array by passing thru a selector and uses the separator string (defaults to comma) */
Array.prototype.joinWith = function (selector, separator) {
return this.map(selector).filter(v => v ? true : false).join(separator || exports.CommaChar);
};
String.prototype.capitalize = function () {
const result = this;
return result ? `${result.charAt(0).toUpperCase()}${result.substr(1)}` : result;
};
String.prototype.uncapitalize = function () {
const result = this;
return result ? `${result.charAt(0).toLowerCase()}${result.substr(1)}` : result;
};
/** Trims the string and removes multi leading spaces? */
String.prototype.slim = function () {
return this.trim().replace(/([^ ]) +/g, '$1 ');
};
function joinComma(items, mapFn) {
return join(items.map(mapFn), exports.CommaChar);
}
exports.joinComma = joinComma;
function join(items, separator) {
return items.filter(v => v ? true : false).join(separator);
}
exports.join = join;
function sortByName(a, b) {
return a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
}
exports.sortByName = sortByName;
function setIndentation(spaces) {
indentation = ' '.repeat(spaces);
}
exports.setIndentation = setIndentation;
function trimDots(content) {
return content.replace(/^[\.\s]*(.*?)[\.\s]*$/g, '$1');
}
exports.trimDots = trimDots;
function toMap(source, eachFn) {
const result = new Map();
for (const each of source) {
const key = eachFn(each);
let values = result.get(key);
if (!values) {
values = new Array();
result.set(key, values);
}
values.push(each);
}
return result;
}
exports.toMap = toMap;
function docComment(content, prefix = exports.docCommentPrefix, factor = 0, maxLength = 120) {
return comment(content, prefix, factor, maxLength);
}
exports.docComment = docComment;
function comment(content, prefix = exports.lineCommentPrefix, factor = 0, maxLength = 120) {
const result = new Array();
let line = '';
prefix = indent(prefix, factor);
content = content.trim();
if (content) {
for (const word of content.replace(/\n+/g, ' » ').split(/\s+/g)) {
if (word === '»') {
result.push(line);
line = prefix;
continue;
}
if (maxLength < line.length) {
result.push(line);
line = '';
}
if (!line) {
line = prefix;
}
line += ` ${word}`;
}
if (line) {
result.push(line);
}
return result.join(exports.EOL);
}
return '';
}
exports.comment = comment;
function dotCombine(prefix, content) {
return trimDots([trimDots(prefix), trimDots(content)].join('.'));
}
exports.dotCombine = dotCombine;
function fixEOL(content) {
return content.replace(/\r\n/g, exports.EOL);
}
exports.fixEOL = fixEOL;
function map(dictionary, callbackfn, thisArg) {
return Object.getOwnPropertyNames(dictionary).map((key) => callbackfn(key, dictionary[key]));
}
exports.map = map;
function ToMap(dictionary) {
const result = new Map();
Object.getOwnPropertyNames(dictionary).map(key => result.set(key, dictionary[key]));
return result;
}
exports.ToMap = ToMap;
function selectMany(multiArray) {
const result = new Array();
multiArray.map(v => result.push(...v));
return result;
}
exports.selectMany = selectMany;
function indent(content, factor = 1) {
const i = indentation.repeat(factor);
content = i + fixEOL(content.trim());
return content.split(/\n/g).join(`${exports.EOL}${i}`);
}
exports.indent = indent;
function all(array, callbackfn, thisArg) {
return Promise.all(array.map(callbackfn));
}
exports.all = all;
function hasProperties(obj) {
return Object.getOwnPropertyNames(obj).length > 0 ? true : false;
}
exports.hasProperties = hasProperties;
function deconstruct(identifier) {
if (Array.isArray(identifier)) {
return [...dictionary_1.values(identifier).linq.selectMany(deconstruct)];
}
return identifier.replace(/([a-z]+)([A-Z])/g, '$1 $2').replace(/(\d+)([a-z|A-Z]+)/g, '$1 $2').split(/[\W|_]+/);
}
exports.deconstruct = deconstruct;
function fixLeadingNumber(identifier) {
if (identifier.length > 0 && /^\d+/.exec(identifier[0])) {
return [...convert(parseInt(identifier[0])), ...identifier.slice(1)];
}
return identifier;
}
exports.fixLeadingNumber = fixLeadingNumber;
const ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
const teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
const tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
const magnitude = ['thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'septillion', 'octillion'];
const magvalues = [10 ** 3, 10 ** 6, 10 ** 9, 10 ** 12, 10 ** 15, 10 ** 18, 10 ** 21, 10 ** 24, 10 ** 27];
function* convert(num) {
if (!num) {
yield 'zero';
return;
}
if (num > 1e+30) {
yield 'lots';
return;
}
if (num > 999) {
for (let i = magvalues.length; i > -1; i--) {
const c = magvalues[i];
if (num > c) {
yield* convert(Math.floor(num / c));
yield magnitude[i];
num = num % c;
}
}
}
if (num > 99) {
yield ones[Math.floor(num / 100)];
yield `hundred`;
num %= 100;
}
if (num > 19) {
yield tens[Math.floor(num / 10)];
num %= 10;
}
if (num) {
yield ones[num];
}
}
exports.convert = convert;
function camelCase(identifier) {
switch (identifier.length) {
case 0:
return '';
case 1:
return identifier[0].uncapitalize();
}
return `${identifier[0].uncapitalize()}${pascalCase(identifier.slice(1))}`;
}
exports.camelCase = camelCase;
function pascalCase(identifier) {
return identifier.map(each => each.capitalize()).join('');
}
exports.pascalCase = pascalCase;
function escapeString(text) {
if (text) {
const q = JSON.stringify(text);
return q.substr(1, q.length - 2);
}
return '';
}
exports.escapeString = escapeString;
/** emits c# to get the name of a property - uses nameof when it can, and uses a literal when it's an array value. */
function nameof(text) {
if (text.indexOf('[') > -1) {
return `$"${text.replace(/\[(.*)\]/, '[{$1}]')}"`;
}
return `nameof(${text})`;
}
exports.nameof = nameof;
function setRegion(source, region, content, prepend = true) {
const ct = new file_generator_1.Text(content).text.replace(/[\r?\n]/g, '«').replace(/^«*/, '').replace(/«*$/, '');
source = source.replace(/[\r?\n]/g, '«');
const rx = new RegExp(`«(\\s*#\\s*region\\s*${region})\\s*«.*?(«\\s*#\\s*endregion\\s*«?)`, 'g');
if (rx.test(source)) {
if (ct.length > 0) {
source = source.replace(rx, `«$1«${ct}$2`);
}
else {
source = source.replace(rx, '');
}
}
else {
if (ct.length > 0) {
const text = `«# region ${region}«${ct}«# endregion«`;
source = prepend ? text + source : source + text;
}
}
source = source.replace(/«««*/g, '««').replace(/«/g, '\n');
return source;
}
exports.setRegion = setRegion;
//# sourceMappingURL=text-manipulation.js.map