UNPKG

typedash

Version:

modern, type-safe collection of utility functions

28 lines (26 loc) 827 B
//#region src/functions/snakeCase/snakeCase.ts /** * Changes the casing of a string to snake case. * @param string The input string to change the casing of. * @returns A new string with the casing changed to snake case. * @example * ```ts * snakeCase('fooBar') // 'foo_bar' * snakeCase('foo bar') // 'foo_bar' * snakeCase('foo-bar') // 'foo_bar' * snakeCase('fooBar42') // 'foo_bar42' * ``` */ function snakeCase(string) { if (!/[a-z]+/i.test(string)) return string; return string.replace(/[_-]+/g, " ").match(SNAKE_REGEX)?.map((x) => x.toLowerCase()).join("_"); } const SNAKE_REGEX = /[A-Z]{2,}(?=[A-Z][a-z]+\d*|\b)|[A-Z]?[a-z]+\d*|[A-Z]|\d+/g; //#endregion Object.defineProperty(exports, 'snakeCase', { enumerable: true, get: function () { return snakeCase; } }); //# sourceMappingURL=snakeCase-BFtmH0S_.cjs.map