froebel
Version:
TypeScript utility library
107 lines (97 loc) • 3.52 kB
JavaScript
/** Upper-case first letter of string. */
export const capitalize = str => str[0].toUpperCase() + str.slice(1);
/** Lower-case first letter of string */
export const uncapitalize = str => str[0].toLowerCase() + str.slice(1);
/** Strictly typed `String.toUpperCase()`. */
export const upper = str => str.toUpperCase();
/** Strictly typed `String.toLowerCase()`. */
export 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'
* ```
*/
export 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'
* ```
*/
export 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'
* ```
*/
export 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'
* ```
*/
export const pascal = str => capitalize(camel(str));
/**
* Transforms a variable name to screaming snake case.
*
* @see {@link snake}
*
* @example
* ```
* screamingSnake('fooBar') // 'FOO_BAR'
* ```
*/
export 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}
*/
export const transformCase = (str, targetCase) => {
if (!(targetCase in converters)) {
throw Error(`can't convert to ${targetCase} case`);
}
return converters[targetCase](str);
};
const converters = {
camel,
kebab,
pascal,
snake,
"screaming-snake": screamingSnake
};