{"version":3,"file":"snakeCase-BFtmH0S_.cjs","names":[],"sources":["../src/functions/snakeCase/snakeCase.ts"],"sourcesContent":["import type { SnakeCase as SnakeCaseImplementation } from 'type-fest';\n\n/**\n * Changes the casing of a string to snake case.\n * @param string The input string to change the casing of.\n * @returns A new string with the casing changed to snake case.\n * @example\n * ```ts\n * snakeCase('fooBar') // 'foo_bar'\n * snakeCase('foo bar') // 'foo_bar'\n * snakeCase('foo-bar') // 'foo_bar'\n * snakeCase('fooBar42') // 'foo_bar42'\n * ```\n */\nexport function snakeCase<S extends string>(string: S): SnakeCase<S> {\n  if (!/[a-z]+/i.test(string)) {\n    return string as SnakeCase<S>;\n  }\n\n  return string\n    .replace(/[_-]+/g, ' ')\n    .match(SNAKE_REGEX)\n    ?.map((x) => x.toLowerCase())\n    .join('_') as SnakeCase<S>;\n}\n\nconst SNAKE_REGEX = /[A-Z]{2,}(?=[A-Z][a-z]+\\d*|\\b)|[A-Z]?[a-z]+\\d*|[A-Z]|\\d+/g;\n\n/**\n * Changes the casing of a string to snake case.\n * @see {@link snakeCase}.\n */\nexport type SnakeCase<S extends string> = TrimUnderscores<\n  ReduceUnderscores<SnakeCaseImplementation<S>>\n>;\n\n/**\n * Reduces multiple underscores to a single underscore.\n * @param S The input string.\n * @returns A new string with multiple underscores reduced to a single underscore.\n * @example\n * ```ts\n * ReduceUnderscores<'foo__bar'> // 'foo_bar'\n * ReduceUnderscores<'foo_bar'> // 'foo_bar'\n * ReduceUnderscores<'foo___bar'> // 'foo_bar'\n * ```\n */\ntype ReduceUnderscores<S extends string> = S extends `${infer L}__${infer R}`\n  ? ReduceUnderscores<`${L}_${R}`>\n  : S extends `${infer L}${infer D}__${infer R}`\n    ? ReduceUnderscores<`${L}${D}_${R}`>\n    : S;\n\n/**\n * Trims underscores from the start and end of a string.\n * @param S The input string.\n * @returns A new string with underscores trimmed from the start and end.\n * @example\n * ```ts\n * TrimUnderscores<'_foo_bar_'> // 'foo_bar'\n * TrimUnderscores<'foo_bar_'> // 'foo_bar'\n * TrimUnderscores<'_foo_bar'> // 'foo_bar'\n * TrimUnderscores<'foo_bar'> // 'foo_bar'\n * ```\n */\ntype TrimUnderscores<S extends string> = S extends `_${infer R}`\n  ? TrimUnderscores<R>\n  : S extends `${infer L}_`\n    ? TrimUnderscores<L>\n    : S;\n"],"mappings":";;;;;;;;;;;;;;AAcA,SAAgB,UAA4B,QAAyB;AACnE,KAAI,CAAC,UAAU,KAAK,OAAO,CACzB,QAAO;AAGT,QAAO,OACJ,QAAQ,UAAU,IAAI,CACtB,MAAM,YAAY,EACjB,KAAK,MAAM,EAAE,aAAa,CAAC,CAC5B,KAAK,IAAI;;AAGd,MAAM,cAAc"}