diff --git a/npm/ng-packs/packages/schematics/src/utils/text.ts b/npm/ng-packs/packages/schematics/src/utils/text.ts index a3c177c1a4..379ea78601 100644 --- a/npm/ng-packs/packages/schematics/src/utils/text.ts +++ b/npm/ng-packs/packages/schematics/src/utils/text.ts @@ -2,7 +2,7 @@ import { strings } from '@angular-devkit/core'; export const lower = (text: string) => text.toLowerCase(); export const upper = (text: string) => text.toUpperCase(); -export const camel = (text: string) => strings.camelize(_(text)); +export const camel = (text: string) => toCamelCase(_(text)); export const pascal = (text: string) => strings.classify(_(text)); export const kebab = (text: string) => strings.dasherize(_(text)); export const snake = (text: string) => strings.underscore(_(text)); @@ -13,3 +13,41 @@ export const dir = (text: string) => function _(text: string): string { return text.replace(/\./g, '_'); } + +// https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Utilities/StringUtils.cs#L155 +function toCamelCase(str: string) { + if (!str || !isUpperCase(str[0])) return str; + + const chars = str.split(''); + const { length } = chars; + + for (let i = 0; i < length; i++) { + if (i === 1 && !isUpperCase(chars[i])) break; + + const hasNext = i + 1 < length; + + if (i > 0 && hasNext && !isUpperCase(chars[i + 1])) { + if (isSeparator(chars[i + 1])) { + chars[i] = toLowerCase(chars[i]); + } + + break; + } + + chars[i] = toLowerCase(chars[i]); + } + + return chars.join(''); +} + +function isSeparator(str = '') { + return /[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]+/.test(str); +} + +function isUpperCase(str = '') { + return /[A-Z]+/.test(str); +} + +function toLowerCase(str = '') { + return str.toLowerCase(); +}