@samchon/openapi
Advanced tools
Comparing version 0.4.6 to 0.4.7
@@ -1,3 +0,5 @@ | ||
export declare function snake(str: string): string; | ||
export declare function camel(str: string): string; | ||
export declare function pascal(str: string): string; | ||
export declare namespace NamingConvention { | ||
function snake(str: string): string; | ||
function camel(str: string): string; | ||
function pascal(str: string): string; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.snake = snake; | ||
exports.camel = camel; | ||
exports.pascal = pascal; | ||
exports.NamingConvention = void 0; | ||
const StringUtil_1 = require("./StringUtil"); | ||
function snake(str) { | ||
const indexes = []; | ||
for (let i = 0; i < str.length; i++) { | ||
const code = str.charCodeAt(i); | ||
if (65 <= code && code <= 90) | ||
indexes.push(i); | ||
var NamingConvention; | ||
(function (NamingConvention) { | ||
function snake(str) { | ||
const indexes = []; | ||
for (let i = 0; i < str.length; i++) { | ||
const code = str.charCodeAt(i); | ||
if (65 <= code && code <= 90) | ||
indexes.push(i); | ||
} | ||
for (let i = indexes.length - 1; i > 0; --i) { | ||
const now = indexes[i]; | ||
const prev = indexes[i - 1]; | ||
if (now - prev === 1) | ||
indexes.splice(i, 1); | ||
} | ||
if (indexes.length !== 0 && indexes[0] === 0) | ||
indexes.splice(0, 1); | ||
if (indexes.length === 0) | ||
return str.toLowerCase(); | ||
let ret = ""; | ||
for (let i = 0; i < indexes.length; i++) { | ||
const first = i === 0 ? 0 : indexes[i - 1]; | ||
const last = indexes[i]; | ||
ret += str.substring(first, last).toLowerCase(); | ||
ret += "_"; | ||
} | ||
ret += str.substring(indexes[indexes.length - 1]).toLowerCase(); | ||
return ret; | ||
} | ||
for (let i = indexes.length - 1; i > 0; --i) { | ||
const now = indexes[i]; | ||
const prev = indexes[i - 1]; | ||
if (now - prev === 1) | ||
indexes.splice(i, 1); | ||
NamingConvention.snake = snake; | ||
function camel(str) { | ||
return unsnake((str) => { | ||
if (str.length === 0) | ||
return str; | ||
else if (str[0] === str[0].toUpperCase()) | ||
return str[0].toLowerCase() + str.substring(1); | ||
else | ||
return str; | ||
})(str); | ||
} | ||
if (indexes.length !== 0 && indexes[0] === 0) | ||
indexes.splice(0, 1); | ||
if (indexes.length === 0) | ||
return str.toLowerCase(); | ||
let ret = ""; | ||
for (let i = 0; i < indexes.length; i++) { | ||
const first = i === 0 ? 0 : indexes[i - 1]; | ||
const last = indexes[i]; | ||
ret += str.substring(first, last).toLowerCase(); | ||
ret += "_"; | ||
NamingConvention.camel = camel; | ||
function pascal(str) { | ||
return unsnake((str) => { | ||
if (str.length === 0) | ||
return str; | ||
else if (str[0] === str[0].toLowerCase()) | ||
return str[0].toUpperCase() + str.substring(1); | ||
else | ||
return str; | ||
})(str); | ||
} | ||
ret += str.substring(indexes[indexes.length - 1]).toLowerCase(); | ||
return ret; | ||
} | ||
function camel(str) { | ||
return unsnake((str) => { | ||
if (str.length === 0) | ||
return str; | ||
else if (str[0] === str[0].toUpperCase()) | ||
return str[0].toLowerCase() + str.substring(1); | ||
else | ||
return str; | ||
})(str); | ||
} | ||
function pascal(str) { | ||
return unsnake((str) => { | ||
if (str.length === 0) | ||
return str; | ||
else if (str[0] === str[0].toLowerCase()) | ||
return str[0].toUpperCase() + str.substring(1); | ||
else | ||
return str; | ||
})(str); | ||
} | ||
const unsnake = (escaper) => (str) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
let prefix = ""; | ||
for (let i = 0; i < str.length; i++) { | ||
if (str[i] === "_") | ||
prefix += "_"; | ||
else | ||
break; | ||
} | ||
if (prefix.length !== 0) | ||
str = str.substring(prefix.length); | ||
const indexes = []; | ||
for (let i = 0; i < str.length; i++) { | ||
const ch = str[i]; | ||
if (ch !== "_") | ||
continue; | ||
const last = indexes[indexes.length - 1]; | ||
if (last === undefined || last[0] + last[1] !== i) | ||
indexes.push([i, 1]); | ||
else | ||
++last[1]; | ||
} | ||
if (indexes.length === 0) | ||
return prefix + escaper(str); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
let ret = ""; | ||
for (let i = 0; i < indexes.length; i++) { | ||
const [first] = indexes[i]; | ||
if (i === 0) | ||
if (first === 0) | ||
ret += "_"; | ||
NamingConvention.pascal = pascal; | ||
const unsnake = (escaper) => (str) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
let prefix = ""; | ||
for (let i = 0; i < str.length; i++) { | ||
if (str[i] === "_") | ||
prefix += "_"; | ||
else | ||
ret += str.substring(0, first); | ||
else { | ||
const [prevFirst, prevLength] = indexes[i - 1]; | ||
const piece = str.substring(prevFirst + prevLength, first); | ||
if (piece.length) | ||
ret += StringUtil_1.StringUtil.capitalize(piece); | ||
break; | ||
} | ||
} | ||
const last = indexes[indexes.length - 1]; | ||
const piece = str.substring(last[0] + last[1]); | ||
if (last.length) | ||
ret += StringUtil_1.StringUtil.capitalize(piece); | ||
return prefix + escaper(ret); | ||
}; | ||
if (prefix.length !== 0) | ||
str = str.substring(prefix.length); | ||
const indexes = []; | ||
for (let i = 0; i < str.length; i++) { | ||
const ch = str[i]; | ||
if (ch !== "_") | ||
continue; | ||
const last = indexes[indexes.length - 1]; | ||
if (last === undefined || last[0] + last[1] !== i) | ||
indexes.push([i, 1]); | ||
else | ||
++last[1]; | ||
} | ||
if (indexes.length === 0) | ||
return prefix + escaper(str); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
let ret = ""; | ||
for (let i = 0; i < indexes.length; i++) { | ||
const [first] = indexes[i]; | ||
if (i === 0) | ||
if (first === 0) | ||
ret += "_"; | ||
else | ||
ret += str.substring(0, first); | ||
else { | ||
const [prevFirst, prevLength] = indexes[i - 1]; | ||
const piece = str.substring(prevFirst + prevLength, first); | ||
if (piece.length) | ||
ret += StringUtil_1.StringUtil.capitalize(piece); | ||
} | ||
} | ||
const last = indexes[indexes.length - 1]; | ||
const piece = str.substring(last[0] + last[1]); | ||
if (last.length) | ||
ret += StringUtil_1.StringUtil.capitalize(piece); | ||
return prefix + escaper(ret); | ||
}; | ||
})(NamingConvention || (exports.NamingConvention = NamingConvention = {})); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.StringUtil = void 0; | ||
const NamingConvention_1 = require("typia/lib/utils/NamingConvention"); | ||
const NamingConvention_1 = require("./NamingConvention"); | ||
var StringUtil; | ||
(function (StringUtil) { | ||
StringUtil.capitalize = (str) => str[0].toUpperCase() + str.slice(1).toLowerCase(); | ||
StringUtil.capitalize = (str) => str.length !== 0 ? str[0].toUpperCase() + str.slice(1).toLowerCase() : str; | ||
StringUtil.pascal = (path) => StringUtil.splitWithNormalization(path) | ||
@@ -23,4 +23,50 @@ .filter((str) => str[0] !== "{") | ||
.join("/"); | ||
StringUtil.normalize = (str) => str.split(".").join("_").split("-").join("_"); | ||
StringUtil.normalize = (str) => { | ||
str = str.split(".").join("_").split("-").join("_").trim(); | ||
if (RESERVED.has(str)) | ||
return `_${str}`; | ||
else if (str.length !== 0 && "0" <= str[0] && str[0] <= "9") | ||
str = `_${str}`; | ||
return str; | ||
}; | ||
StringUtil.escapeDuplicate = (keep) => (change) => keep.includes(change) ? StringUtil.escapeDuplicate(keep)(`_${change}`) : change; | ||
})(StringUtil || (exports.StringUtil = StringUtil = {})); | ||
const RESERVED = new Set([ | ||
"break", | ||
"case", | ||
"catch", | ||
"class", | ||
"const", | ||
"continue", | ||
"debugger", | ||
"default", | ||
"delete", | ||
"do", | ||
"else", | ||
"enum", | ||
"export", | ||
"extends", | ||
"false", | ||
"finally", | ||
"for", | ||
"function", | ||
"if", | ||
"import", | ||
"in", | ||
"instanceof", | ||
"new", | ||
"null", | ||
"package", | ||
"return", | ||
"super", | ||
"switch", | ||
"this", | ||
"throw", | ||
"true", | ||
"try", | ||
"typeof", | ||
"var", | ||
"void", | ||
"while", | ||
"with", | ||
]); |
{ | ||
"name": "@samchon/openapi", | ||
"version": "0.4.6", | ||
"version": "0.4.7", | ||
"description": "OpenAPI definitions and converters for 'typia' and 'nestia'.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
import { StringUtil } from "./StringUtil"; | ||
export function snake(str: string): string { | ||
const indexes: number[] = []; | ||
for (let i: number = 0; i < str.length; i++) { | ||
const code: number = str.charCodeAt(i); | ||
if (65 <= code && code <= 90) indexes.push(i); | ||
} | ||
for (let i: number = indexes.length - 1; i > 0; --i) { | ||
const now: number = indexes[i]!; | ||
const prev: number = indexes[i - 1]!; | ||
if (now - prev === 1) indexes.splice(i, 1); | ||
} | ||
if (indexes.length !== 0 && indexes[0] === 0) indexes.splice(0, 1); | ||
if (indexes.length === 0) return str.toLowerCase(); | ||
export namespace NamingConvention { | ||
export function snake(str: string): string { | ||
const indexes: number[] = []; | ||
for (let i: number = 0; i < str.length; i++) { | ||
const code: number = str.charCodeAt(i); | ||
if (65 <= code && code <= 90) indexes.push(i); | ||
} | ||
for (let i: number = indexes.length - 1; i > 0; --i) { | ||
const now: number = indexes[i]!; | ||
const prev: number = indexes[i - 1]!; | ||
if (now - prev === 1) indexes.splice(i, 1); | ||
} | ||
if (indexes.length !== 0 && indexes[0] === 0) indexes.splice(0, 1); | ||
if (indexes.length === 0) return str.toLowerCase(); | ||
let ret: string = ""; | ||
for (let i: number = 0; i < indexes.length; i++) { | ||
const first: number = i === 0 ? 0 : indexes[i - 1]!; | ||
const last: number = indexes[i]!; | ||
let ret: string = ""; | ||
for (let i: number = 0; i < indexes.length; i++) { | ||
const first: number = i === 0 ? 0 : indexes[i - 1]!; | ||
const last: number = indexes[i]!; | ||
ret += str.substring(first, last).toLowerCase(); | ||
ret += "_"; | ||
ret += str.substring(first, last).toLowerCase(); | ||
ret += "_"; | ||
} | ||
ret += str.substring(indexes[indexes.length - 1]!).toLowerCase(); | ||
return ret; | ||
} | ||
ret += str.substring(indexes[indexes.length - 1]!).toLowerCase(); | ||
return ret; | ||
} | ||
export function camel(str: string): string { | ||
return unsnake((str: string) => { | ||
if (str.length === 0) return str; | ||
else if (str[0] === str[0]!.toUpperCase()) | ||
return str[0]!.toLowerCase() + str.substring(1); | ||
else return str; | ||
})(str); | ||
} | ||
export function camel(str: string): string { | ||
return unsnake((str: string) => { | ||
if (str.length === 0) return str; | ||
else if (str[0] === str[0]!.toUpperCase()) | ||
return str[0]!.toLowerCase() + str.substring(1); | ||
else return str; | ||
})(str); | ||
} | ||
export function pascal(str: string): string { | ||
return unsnake((str: string) => { | ||
if (str.length === 0) return str; | ||
else if (str[0] === str[0]!.toLowerCase()) | ||
return str[0]!.toUpperCase() + str.substring(1); | ||
else return str; | ||
})(str); | ||
} | ||
export function pascal(str: string): string { | ||
return unsnake((str: string) => { | ||
if (str.length === 0) return str; | ||
else if (str[0] === str[0]!.toLowerCase()) | ||
return str[0]!.toUpperCase() + str.substring(1); | ||
else return str; | ||
})(str); | ||
} | ||
const unsnake = | ||
(escaper: (str: string) => string) => | ||
(str: string): string => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
let prefix: string = ""; | ||
for (let i: number = 0; i < str.length; i++) { | ||
if (str[i] === "_") prefix += "_"; | ||
else break; | ||
} | ||
if (prefix.length !== 0) str = str.substring(prefix.length); | ||
const unsnake = | ||
(escaper: (str: string) => string) => | ||
(str: string): string => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
let prefix: string = ""; | ||
for (let i: number = 0; i < str.length; i++) { | ||
if (str[i] === "_") prefix += "_"; | ||
else break; | ||
} | ||
if (prefix.length !== 0) str = str.substring(prefix.length); | ||
const indexes: [number, number][] = []; | ||
for (let i: number = 0; i < str.length; i++) { | ||
const ch: string = str[i]!; | ||
if (ch !== "_") continue; | ||
const indexes: [number, number][] = []; | ||
for (let i: number = 0; i < str.length; i++) { | ||
const ch: string = str[i]!; | ||
if (ch !== "_") continue; | ||
const last = indexes[indexes.length - 1]; | ||
if (last === undefined || last[0] + last[1] !== i) indexes.push([i, 1]); | ||
else ++last[1]; | ||
} | ||
if (indexes.length === 0) return prefix + escaper(str); | ||
const last = indexes[indexes.length - 1]; | ||
if (last === undefined || last[0] + last[1] !== i) indexes.push([i, 1]); | ||
else ++last[1]; | ||
} | ||
if (indexes.length === 0) return prefix + escaper(str); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
let ret: string = ""; | ||
for (let i: number = 0; i < indexes.length; i++) { | ||
const [first] = indexes[i]!; | ||
if (i === 0) | ||
if (first === 0) ret += "_"; | ||
else ret += str.substring(0, first); | ||
else { | ||
const [prevFirst, prevLength] = indexes[i - 1]!; | ||
const piece: string = str.substring(prevFirst + prevLength, first); | ||
if (piece.length) ret += StringUtil.capitalize(piece); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
let ret: string = ""; | ||
for (let i: number = 0; i < indexes.length; i++) { | ||
const [first] = indexes[i]!; | ||
if (i === 0) | ||
if (first === 0) ret += "_"; | ||
else ret += str.substring(0, first); | ||
else { | ||
const [prevFirst, prevLength] = indexes[i - 1]!; | ||
const piece: string = str.substring(prevFirst + prevLength, first); | ||
if (piece.length) ret += StringUtil.capitalize(piece); | ||
} | ||
} | ||
} | ||
const last = indexes[indexes.length - 1]!; | ||
const piece: string = str.substring(last[0] + last[1]); | ||
if (last.length) ret += StringUtil.capitalize(piece); | ||
return prefix + escaper(ret); | ||
}; | ||
const last = indexes[indexes.length - 1]!; | ||
const piece: string = str.substring(last[0] + last[1]); | ||
if (last.length) ret += StringUtil.capitalize(piece); | ||
return prefix + escaper(ret); | ||
}; | ||
} |
@@ -1,8 +0,8 @@ | ||
import { NamingConvention } from "typia/lib/utils/NamingConvention"; | ||
import { NamingConvention } from "./NamingConvention"; | ||
export namespace StringUtil { | ||
export const capitalize = (str: string) => | ||
str[0].toUpperCase() + str.slice(1).toLowerCase(); | ||
export const capitalize = (str: string): string => | ||
str.length !== 0 ? str[0].toUpperCase() + str.slice(1).toLowerCase() : str; | ||
export const pascal = (path: string) => | ||
export const pascal = (path: string): string => | ||
splitWithNormalization(path) | ||
@@ -13,3 +13,3 @@ .filter((str) => str[0] !== "{") | ||
export const splitWithNormalization = (path: string) => | ||
export const splitWithNormalization = (path: string): string[] => | ||
path | ||
@@ -31,4 +31,9 @@ .split("/") | ||
export const normalize = (str: string) => | ||
str.split(".").join("_").split("-").join("_"); | ||
export const normalize = (str: string): string => { | ||
str = str.split(".").join("_").split("-").join("_").trim(); | ||
if (RESERVED.has(str)) return `_${str}`; | ||
else if (str.length !== 0 && "0" <= str[0] && str[0] <= "9") | ||
str = `_${str}`; | ||
return str; | ||
}; | ||
@@ -40,1 +45,41 @@ export const escapeDuplicate = | ||
} | ||
const RESERVED: Set<string> = new Set([ | ||
"break", | ||
"case", | ||
"catch", | ||
"class", | ||
"const", | ||
"continue", | ||
"debugger", | ||
"default", | ||
"delete", | ||
"do", | ||
"else", | ||
"enum", | ||
"export", | ||
"extends", | ||
"false", | ||
"finally", | ||
"for", | ||
"function", | ||
"if", | ||
"import", | ||
"in", | ||
"instanceof", | ||
"new", | ||
"null", | ||
"package", | ||
"return", | ||
"super", | ||
"switch", | ||
"this", | ||
"throw", | ||
"true", | ||
"try", | ||
"typeof", | ||
"var", | ||
"void", | ||
"while", | ||
"with", | ||
]); |
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
763448
12040