UNPKG

froebel

Version:
145 lines (113 loc) 4.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.upper = exports.uncapitalize = exports.transformCase = exports.snake = exports.screamingSnake = exports.pascal = exports.lower = exports.kebab = exports.capitalize = exports.camel = void 0; /** Upper-case first letter of string. */ const capitalize = str => str[0].toUpperCase() + str.slice(1); /** Lower-case first letter of string */ exports.capitalize = capitalize; const uncapitalize = str => str[0].toLowerCase() + str.slice(1); /** Strictly typed `String.toUpperCase()`. */ exports.uncapitalize = uncapitalize; const upper = str => str.toUpperCase(); /** Strictly typed `String.toLowerCase()`. */ exports.upper = upper; const lower = str => str.toLowerCase(); /** * Transforms a variable name to snake case. * * Note: The rules for transforming anything to snake case are somewhat vague. * So use this only for very simple names where the resulting value is * absolutely unambiguous. For more examples of how names are transformed, have * a look at the test cases. * * @example * ``` * snake('fooBar') // 'foo_bar' * ``` */ exports.lower = lower; const snake = str => str.replace(/(\p{L})-(?=\p{L})/gu, "$1_").replace(/(^|_)(\p{Lu})(?!\p{Lu})/gu, (_, a, b) => `${a}${b.toLowerCase()}`).replace(/([^\p{Lu}])(\p{Lu})(?=\p{Lu})/gu, (_, a, b) => `${a}_${b}`).replace(/([^\p{Lu}_0-9])(\p{Lu})/gu, (_, a, b) => `${a}_${b.toLowerCase()}`).replace(/(\p{Lu}[^\p{L}_]*)(\p{Ll})/gu, (_, a, b) => `${a}_${b}`); /** * Transforms a variable name to kebab case. * * Note: The rules for transforming anything to kebab case are somewhat vague. * So use this only for very simple names where the resulting value is * absolutely unambiguous. For more examples of how names are transformed, have * a look at the test cases. * * @example * ``` * kebab('fooBar') // 'foo-bar' * ``` */ exports.snake = snake; const kebab = str => str.replace(/(\p{L})_(?=\p{L})/gu, "$1-").replace(/(^|-)(\p{Lu})(?!\p{Lu})/gu, (_, a, b) => `${a}${b.toLowerCase()}`).replace(/([^\p{Lu}])(\p{Lu})(?=\p{Lu})/gu, (_, a, b) => `${a}-${b}`).replace(/([^\p{Lu}\-0-9])(\p{Lu})/gu, (_, a, b) => `${a}-${b.toLowerCase()}`).replace(/(\p{Lu}[^\p{L}\-]*)(\p{Ll})/gu, (_, a, b) => `${a}-${b}`); /** * Transforms a variable name to camel case. * * Note: The rules for transforming anything to camel case are somewhat vague. * So use this only for very simple names where the resulting value is * absolutely unambiguous. For more examples of how names are transformed, have * a look at the test cases. * * @example * ``` * camel('foo_bar') // 'fooBar' * ``` */ exports.kebab = kebab; const camel = str => str.replace(/^\p{Lu}/u, v => v.toLowerCase()).replace(/([^_-][_-]*)[_-](\p{L})/gu, (_, a, b) => a + b.toUpperCase()); /** * Transforms a variable name to pascal case. * * Note: The rules for transforming anything to pascal case are somewhat vague. * So use this only for very simple names where the resulting value is * absolutely unambiguous. For more examples of how names are transformed, have * a look at the test cases. * * @example * ``` * pascal('foo_bar') // 'FooBar' * ``` */ exports.camel = camel; const pascal = str => capitalize(camel(str)); /** * Transforms a variable name to screaming snake case. * * @see {@link snake} * * @example * ``` * screamingSnake('fooBar') // 'FOO_BAR' * ``` */ exports.pascal = pascal; const screamingSnake = str => upper(snake(str)); /** * Transform a variable name to `targetCase` * * @see {@link snake} * @see {@link kebab} * @see {@link camel} * @see {@link pascal} * @see {@link screamingSnake} */ exports.screamingSnake = screamingSnake; const transformCase = (str, targetCase) => { if (!(targetCase in converters)) { throw Error(`can't convert to ${targetCase} case`); } return converters[targetCase](str); }; exports.transformCase = transformCase; const converters = { camel, kebab, pascal, snake, "screaming-snake": screamingSnake }; module.exports = Object.assign(exports.default || {}, exports);