UNPKG

takin

Version:

Front end engineering base toolchain and scaffold

128 lines 4.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.camelcaseOptionName = exports.getFileName = exports.setByType = exports.setDotProp = exports.camelcase = exports.padRight = exports.findLongest = exports.getMriOptions = exports.findAllBrackets = exports.removeBrackets = void 0; const removeBrackets = (v) => v.replace(/[<[].+/, '').trim(); exports.removeBrackets = removeBrackets; const findAllBrackets = (v) => { const ANGLED_BRACKET_RE_GLOBAL = /<([^>]+)>/g; const SQUARE_BRACKET_RE_GLOBAL = /\[([^\]]+)\]/g; const res = []; const parse = (match) => { let variadic = false; let value = match[1]; if (value.startsWith('...')) { value = value.slice(3); variadic = true; } return { required: match[0].startsWith('<'), value, variadic }; }; let angledMatch; while ((angledMatch = ANGLED_BRACKET_RE_GLOBAL.exec(v))) { res.push(parse(angledMatch)); } let squareMatch; while ((squareMatch = SQUARE_BRACKET_RE_GLOBAL.exec(v))) { res.push(parse(squareMatch)); } return res; }; exports.findAllBrackets = findAllBrackets; const getMriOptions = (options) => { const result = { alias: {}, boolean: [] }; for (const [index, option] of options.entries()) { // We do not set default values in mri options // Since its type (typeof) will be used to cast parsed arguments. // Which mean `--foo foo` will be parsed as `{foo: true}` if we have `{default:{foo: true}}` // Set alias if (option.names.length > 1) { result.alias[option.names[0]] = option.names.slice(1); } // Set boolean if (option.isBoolean) { if (option.negated) { // For negated option // We only set it to `boolean` type when there's no string-type option with the same name const hasStringTypeOption = options.some((o, i) => { return (i !== index && o.names.some((name) => option.names.includes(name)) && typeof o.required === 'boolean'); }); if (!hasStringTypeOption) { result.boolean.push(option.names[0]); } } else { result.boolean.push(option.names[0]); } } } return result; }; exports.getMriOptions = getMriOptions; const findLongest = (arr) => { return arr.sort((a, b) => { return a.length > b.length ? -1 : 1; })[0]; }; exports.findLongest = findLongest; const padRight = (str, length) => { return str.length >= length ? str : `${str}${' '.repeat(length - str.length)}`; }; exports.padRight = padRight; const camelcase = (input) => { return input.replace(/([a-z])-([a-z])/g, (_, p1, p2) => { return p1 + p2.toUpperCase(); }); }; exports.camelcase = camelcase; const setDotProp = (obj, keys, val) => { let i = 0; const length = keys.length; let t = obj; let x; for (; i < length; ++i) { x = t[keys[i]]; t = t[keys[i]] = i === length - 1 ? val : x != null ? x : !!~keys[i + 1].indexOf('.') || !(+keys[i + 1] > -1) ? {} : []; } }; exports.setDotProp = setDotProp; const setByType = (obj, transforms) => { for (const key of Object.keys(transforms)) { const transform = transforms[key]; if (transform.shouldTransform) { obj[key] = Array.prototype.concat.call([], obj[key]); if (typeof transform.transformFunction === 'function') { obj[key] = obj[key].map(transform.transformFunction); } } } }; exports.setByType = setByType; const getFileName = (input) => { const m = /([^\\\/]+)$/.exec(input); return m ? m[1] : ''; }; exports.getFileName = getFileName; const camelcaseOptionName = (name) => { // Camelcase the option name // Don't camelcase anything after the dot `.` return name .split('.') .map((v, i) => { return i === 0 ? (0, exports.camelcase)(v) : v; }) .join('.'); }; exports.camelcaseOptionName = camelcaseOptionName; //# sourceMappingURL=utils.js.map