@azure-tools/codegen
Advanced tools
Comparing version 2.2.244 to 2.3.245
import { Dictionary } from '@azure-tools/linq'; | ||
export declare type Styler = ((identifier: string | Array<string>, removeDuplicates: boolean | undefined, overrides: Dictionary<string> | undefined) => string); | ||
export declare class Style { | ||
static select(style: any, fallback: Styler): Styler; | ||
static kebab(identifier: string | Array<string>, removeDuplicates?: boolean, overrides?: Dictionary<string>): string; | ||
static space(identifier: string | Array<string>, removeDuplicates?: boolean, overrides?: Dictionary<string>): string; | ||
static snake(identifier: string | Array<string>, removeDuplicates?: boolean, overrides?: Dictionary<string>): string; | ||
static upper(identifier: string | Array<string>, removeDuplicates?: boolean, overrides?: Dictionary<string>): string; | ||
static pascal(identifier: string | Array<string>, removeDuplicates?: boolean, overrides?: Dictionary<string>): string; | ||
static camel(identifier: string | Array<string>, removeDuplicates?: boolean, overrides?: Dictionary<string>): string; | ||
static select(style: any, fallback: Styler, maxUppercasePreserve: number): Styler; | ||
static kebab(identifier: string | Array<string>, removeDuplicates?: boolean, overrides?: Dictionary<string>, maxUppercasePreserve?: number): string; | ||
static space(identifier: string | Array<string>, removeDuplicates?: boolean, overrides?: Dictionary<string>, maxUppercasePreserve?: number): string; | ||
static snake(identifier: string | Array<string>, removeDuplicates?: boolean, overrides?: Dictionary<string>, maxUppercasePreserve?: number): string; | ||
static upper(identifier: string | Array<string>, removeDuplicates?: boolean, overrides?: Dictionary<string>, maxUppercasePreserve?: number): string; | ||
static pascal(identifier: string | Array<string>, removeDuplicates?: boolean, overrides?: Dictionary<string>, maxUppercasePreserve?: number): string; | ||
static camel(identifier: string | Array<string>, removeDuplicates?: boolean, overrides?: Dictionary<string>, maxUppercasePreserve?: number): string; | ||
} | ||
//# sourceMappingURL=formatter.d.ts.map |
@@ -5,5 +5,14 @@ "use strict"; | ||
const linq_1 = require("@azure-tools/linq"); | ||
function deconstruct(identifier) { | ||
function capitalize(s) { | ||
return s ? `${s.charAt(0).toUpperCase()}${s.substr(1)}` : s; | ||
} | ||
function uncapitalize(s) { | ||
return s ? `${s.charAt(0).toLowerCase()}${s.substr(1)}` : s; | ||
} | ||
function IsFullyUpperCase(identifier, maxUppercasePreserve) { | ||
return identifier.length <= maxUppercasePreserve && identifier === identifier.toUpperCase(); | ||
} | ||
function deconstruct(identifier, maxUppercasePreserve) { | ||
if (Array.isArray(identifier)) { | ||
return [...linq_1.values(identifier).selectMany(deconstruct)]; | ||
return [...linq_1.values(identifier).selectMany(each => deconstruct(each, maxUppercasePreserve))]; | ||
} | ||
@@ -13,10 +22,11 @@ return `${identifier}`. | ||
replace(/(\d+)([a-z|A-Z]+)/g, '$1 $2'). | ||
//replace(/\b([A-Z])([A-Z])([a-z])([A-Z])/g, '$1$2 $3 $4'). | ||
replace(/\b([A-Z]+)([A-Z])([a-z])/g, '$1 $2$3'). | ||
split(/[\W|_]+/).map(each => each.toLowerCase()); | ||
split(/[\W|_]+/).map(each => IsFullyUpperCase(each, maxUppercasePreserve) ? each : each.toLowerCase()); | ||
} | ||
function wrap(prefix, postfix, style) { | ||
function wrap(prefix, postfix, style, maxUppercasePreserve) { | ||
if (postfix || prefix) { | ||
return (i, r, o) => typeof i === 'string' && typeof (o) === 'object' ? o[i.toLowerCase()] || `${prefix}${style(i, r, o)}${postfix}` : `${prefix}${style(i, r, o)}${postfix}`; | ||
return (i, r, o) => typeof i === 'string' && typeof (o) === 'object' ? o[i.toLowerCase()] || `${prefix}${style(i, r, o, maxUppercasePreserve)}${postfix}` : `${prefix}${style(i, r, o, maxUppercasePreserve)}${postfix}`; | ||
} | ||
return style; | ||
return (i, r, o) => style(i, r, o, maxUppercasePreserve); | ||
} | ||
@@ -26,10 +36,10 @@ function applyFormat(normalizedContent, overrides = {}, separator = '', formatter = (s, i) => s) { | ||
} | ||
function normalize(identifier, removeDuplicates = true, overrides = {}) { | ||
function normalize(identifier, removeDuplicates = true, overrides = {}, maxUppercasePreserve = 0) { | ||
if (!identifier || identifier.length === 0) { | ||
return ['']; | ||
} | ||
return typeof identifier === 'string' ? normalize(text_manipulation_1.fixLeadingNumber(deconstruct(identifier))) : removeDuplicates ? text_manipulation_1.removeSequentialDuplicates(identifier) : identifier; | ||
return typeof identifier === 'string' ? normalize(text_manipulation_1.fixLeadingNumber(deconstruct(identifier, maxUppercasePreserve))) : removeDuplicates ? text_manipulation_1.removeSequentialDuplicates(identifier) : identifier; | ||
} | ||
class Style { | ||
static select(style, fallback) { | ||
static select(style, fallback, maxUppercasePreserve) { | ||
if (style) { | ||
@@ -43,40 +53,40 @@ const styles = /^([a-zA-Z0-9_]*?\+?)([a-zA-Z]+)(\+?[a-zA-Z0-9_]*)$/g.exec(style.replace(/\s*/g, '')); | ||
case 'camel': | ||
return wrap(prefix, postfix, Style.camel); | ||
return wrap(prefix, postfix, Style.camel, maxUppercasePreserve); | ||
case 'pascalcase': | ||
case 'pascal': | ||
return wrap(prefix, postfix, Style.pascal); | ||
return wrap(prefix, postfix, Style.pascal, maxUppercasePreserve); | ||
case 'snakecase': | ||
case 'snake': | ||
return wrap(prefix, postfix, Style.snake); | ||
return wrap(prefix, postfix, Style.snake, maxUppercasePreserve); | ||
case 'uppercase': | ||
case 'upper': | ||
return wrap(prefix, postfix, Style.upper); | ||
return wrap(prefix, postfix, Style.upper, maxUppercasePreserve); | ||
case 'kebabcase': | ||
case 'kebab': | ||
return wrap(prefix, postfix, Style.kebab); | ||
return wrap(prefix, postfix, Style.kebab, maxUppercasePreserve); | ||
case 'spacecase': | ||
case 'space': | ||
return wrap(prefix, postfix, Style.space); | ||
return wrap(prefix, postfix, Style.space, maxUppercasePreserve); | ||
} | ||
} | ||
} | ||
return fallback; | ||
return wrap('', '', fallback, maxUppercasePreserve); | ||
} | ||
static kebab(identifier, removeDuplicates = true, overrides = {}) { | ||
return overrides[identifier] || applyFormat(normalize(identifier, removeDuplicates), overrides, '-'); | ||
static kebab(identifier, removeDuplicates = true, overrides = {}, maxUppercasePreserve = 0) { | ||
return overrides[identifier] || applyFormat(normalize(identifier, removeDuplicates, overrides, maxUppercasePreserve), overrides, '-'); | ||
} | ||
static space(identifier, removeDuplicates = true, overrides = {}) { | ||
return overrides[identifier] || applyFormat(normalize(identifier, removeDuplicates), overrides, ''); | ||
static space(identifier, removeDuplicates = true, overrides = {}, maxUppercasePreserve = 0) { | ||
return overrides[identifier] || applyFormat(normalize(identifier, removeDuplicates, overrides, maxUppercasePreserve), overrides, ''); | ||
} | ||
static snake(identifier, removeDuplicates = true, overrides = {}) { | ||
return overrides[identifier] || applyFormat(normalize(identifier, removeDuplicates), overrides, '_'); | ||
static snake(identifier, removeDuplicates = true, overrides = {}, maxUppercasePreserve = 0) { | ||
return overrides[identifier] || applyFormat(normalize(identifier, removeDuplicates, overrides, maxUppercasePreserve), overrides, '_'); | ||
} | ||
static upper(identifier, removeDuplicates = true, overrides = {}) { | ||
return overrides[identifier] || applyFormat(normalize(identifier, removeDuplicates), overrides, '_', each => each.toUpperCase()); | ||
static upper(identifier, removeDuplicates = true, overrides = {}, maxUppercasePreserve = 0) { | ||
return overrides[identifier] || applyFormat(normalize(identifier, removeDuplicates, overrides, maxUppercasePreserve), overrides, '_', each => each.toUpperCase()); | ||
} | ||
static pascal(identifier, removeDuplicates = true, overrides = {}) { | ||
return overrides[identifier] || applyFormat(normalize(identifier, removeDuplicates), overrides, '', each => each.capitalize()); | ||
static pascal(identifier, removeDuplicates = true, overrides = {}, maxUppercasePreserve = 0) { | ||
return overrides[identifier] || applyFormat(normalize(identifier, removeDuplicates, overrides, maxUppercasePreserve), overrides, '', each => capitalize(each)); | ||
} | ||
static camel(identifier, removeDuplicates = true, overrides = {}) { | ||
return overrides[identifier] || applyFormat(normalize(identifier, removeDuplicates), overrides, '', (each, index) => index ? each.capitalize() : each.uncapitalize()); | ||
static camel(identifier, removeDuplicates = true, overrides = {}, maxUppercasePreserve = 0) { | ||
return overrides[identifier] || applyFormat(normalize(identifier, removeDuplicates, overrides, maxUppercasePreserve), overrides, '', (each, index) => index ? capitalize(each) : IsFullyUpperCase(each, maxUppercasePreserve) ? each : uncapitalize(each)); | ||
} | ||
@@ -83,0 +93,0 @@ } |
{ | ||
"name": "@azure-tools/codegen", | ||
"version": "2.2.244", | ||
"version": "2.3.245", | ||
"patchOffset": 100, | ||
@@ -5,0 +5,0 @@ "description": "Autorest Code generator common and base classes", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
207353
2583