deno.land / x / froebel@v0.23.2 / case.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152import type { CamelCase, KebabCase, PascalCase, ScreamingSnakeCase, SnakeCase, StringCase, λ,} from "./types.ts";
/** Upper-case first letter of string. */export const capitalize = <T extends string>(str: T) => (str[0].toUpperCase() + str.slice(1)) as Capitalize<T>;
/** Lower-case first letter of string */export const uncapitalize = <T extends string>(str: T) => (str[0].toLowerCase() + str.slice(1)) as Uncapitalize<T>;
/** Strictly typed `String.toUpperCase()`. */export const upper = <T extends string>(str: T) => str.toUpperCase() as Uppercase<T>;
/** Strictly typed `String.toLowerCase()`. */export const lower = <T extends string>(str: T) => str.toLowerCase() as Lowercase<T>;
/** * 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 = <T extends string>(str: T): SnakeCase<T> => 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}`) as any;
/** * 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 = <T extends string>(str: T): KebabCase<T> => 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}`) as any;
/** * 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 = <T extends string>(str: T): CamelCase<T> => str .replace(/^\p{Lu}/u, (v) => v.toLowerCase()) .replace( /([^_-][_-]*)[_-](\p{L})/gu, (_, a, b) => a + b.toUpperCase(), ) as any;
/** * 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 = <T extends string>(str: T): PascalCase<T> => capitalize(camel(str));
/** * Transforms a variable name to screaming snake case. * * @see {@link snake} * * @example * ``` * screamingSnake('fooBar') // 'FOO_BAR' * ``` */export const screamingSnake = <T extends string>( str: T,): ScreamingSnakeCase<T> => 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 = <T extends string, C extends StringCase>( str: T, targetCase: C,): C extends "snake" ? SnakeCase<T> : never => { if (!(targetCase in converters)) { throw Error(`can't convert to ${targetCase} case`); } return converters[targetCase](str) as any;};
const converters: Record<StringCase, λ<[string], string>> = { camel, kebab, pascal, snake, "screaming-snake": screamingSnake,};
Version Info