// @ts-nocheck /* ============================================================================ - TAON READONLY SOURCE - DO NOT MODIFY THIS FILE. - Changes may be overwritten. - Use command: taon build:watch:lib inside magic-renamer project repository for watch development of third party modules like this. * ========================================================================== */ import { Log, Level } from 'ng2-logger/browser'; import { Helpers, Utils } from 'tnp-core/browser'; import { _, UtilsString } from 'tnp-core/browser'; const log = Log.create('rename-rule', Level.WARN, Level.ERROR); export class RenameRule { static from(pArgs: string): RenameRule[] { pArgs = pArgs.replace(/\=\>/g, '->'); let args = pArgs.split(/(\'|\")(\ )+(\'|\")/).filter(f => !!f) as string[]; log.d('---- Rules ----'); args.forEach(a => { log.d(a); }); log.d('---------------'); return args .filter(a => a.search('->') !== -1) .map(a => { const [from, to] = a.split('->'); if (!from || !to) { Helpers.error(`Incorrect rule "${from}" -> "${to}" please follow pattern: 'test name -> my new name '`, false, true); } return new RenameRule(from.trim(), to.trim()); }); } public readonly fromWhiteSpaceReplaced: string; public readonly toWhiteSpaceReplaced: string; constructor(public readonly from: string, public readonly to: string) { this.fromWhiteSpaceReplaced = from.trim().toLowerCase().replace(/\W/g, ' '); this.toWhiteSpaceReplaced = to.trim().toLowerCase().replace(/\W/g, ' '); } applyTo(s: string): boolean { s = s.trim().toLowerCase().replace(/\W/g, ''); const res = s.search(this.from.replace(/\W/g, '')) !== -1; return res; } toString = () => { return `${this.from} => ${this.to}`; }; includes(orgString) { return !_.isUndefined(this.combinationsData(true).combinationsAll.find(v => { const [from, to] = v; return orgString === to; })); } combinationsData(getAll = false): { isWeakFrom: boolean; combinationsAll?: string[][]; combinationsInClass?: string[][]; combinationsInTemplate?: string[][]; } { const thisTo = this.toWhiteSpaceReplaced; const thisFrom = this.fromWhiteSpaceReplaced; const isWeakFrom = _.kebabCase(thisFrom).split('-').length <= 1; if (isWeakFrom && !getAll) { return { isWeakFrom, combinationsInTemplate: [ [_.upperCase(thisFrom), _.snakeCase(thisTo).toUpperCase()], [ _.upperFirst(_.camelCase(thisFrom)), _.upperFirst(_.camelCase(thisTo)), ], ['"' + _.camelCase(thisFrom) + '.', '"' + _.camelCase(thisTo) + '.'], [ UtilsString.kebabCaseNoSplitNumbers(thisFrom), UtilsString.kebabCaseNoSplitNumbers(thisTo), ], ], combinationsInClass: [ [ _.upperFirst(_.camelCase(thisFrom)), _.upperFirst(_.camelCase(thisTo)), ], [_.upperCase(thisFrom), _.snakeCase(thisTo).toUpperCase()], [_.camelCase(thisFrom), _.camelCase(thisTo)], ], }; } return { isWeakFrom: false, combinationsAll: [ [ UtilsString.kebabCaseNoSplitNumbers(thisFrom), UtilsString.kebabCaseNoSplitNumbers(thisTo), ], [_.kebabCase(thisFrom), _.kebabCase(thisTo)], [_.camelCase(thisFrom), _.camelCase(thisTo)], [ _.upperFirst(_.camelCase(thisFrom)), _.upperFirst(_.camelCase(thisTo)), ], [_.snakeCase(thisFrom), _.snakeCase(thisTo)], [ _.snakeCase(thisFrom).toUpperCase(), _.snakeCase(thisTo).toUpperCase(), ], [_.startCase(thisFrom), _.startCase(thisTo)], [_.upperCase(thisFrom), _.upperCase(thisTo)], [_.lowerCase(thisFrom), _.lowerCase(thisTo)], [ _.camelCase(thisFrom).toLocaleLowerCase(), _.camelCase(thisTo).toLocaleLowerCase(), ], ], }; } replaceInString(orgString: string): string { return this.replace({ orgString, replaceAllPossibilities: true, }); } replace(options: { fileName?: string; orgString: string; replaceAllPossibilities?: boolean; }) { let { fileName, orgString, replaceAllPossibilities } = options; if (typeof orgString !== 'string') { Helpers.log({ orgString }); Helpers.warn(`[magic-renamer][not a string] fielname ${fileName}`); return orgString; } replaceAllPossibilities = !!replaceAllPossibilities; const combinationsData = this.combinationsData(); if (combinationsData.isWeakFrom) { const lines = orgString.split('\n'); for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { const line = lines[lineIndex]; let combinations = combinationsData.combinationsInTemplate; if (line.includes('inject(')) { combinations = combinationsData.combinationsInClass; } for (let combinationIndex = 0; combinationIndex < combinations.length; combinationIndex++) { let [from, to] = combinations[combinationIndex]; lines[lineIndex] = lines[lineIndex].replace(new RegExp(Utils.escapeStringForRegEx(from), 'g'), to); } } orgString = lines.join('\n'); } else { const combinations = combinationsData.combinationsAll; for (let combinationIndex = 0; combinationIndex < combinations.length; combinationIndex++) { const combination = combinations[combinationIndex]; let [from, to] = combination; if (orgString.search(from) !== -1) { const regex = new RegExp(Utils.escapeStringForRegEx(from)); log.i(`apply! "${regex.source}" to file ${fileName} => "${to}"`); orgString = orgString.replace(new RegExp(Utils.escapeStringForRegEx(from), 'g'), to); if (!replaceAllPossibilities) { return orgString; } } } } return orgString; } }