Skip to content

snakeCase

Edit

Converts text from common word boundaries into lowercase snake case.

snakeCase.ts
const words = (value: string): string[] => {
return value
.replace(/([\p{Ll}\d])(\p{Lu}(?=\p{Ll}))/gu, "$1 $2")
.replace(/(\p{Lu}+)(\p{Lu}\p{Ll})/gu, "$1 $2")
.match(/[\p{L}\p{N}]+/gu) ?? [];
};
/** Converts text from common word boundaries into lowercase snake case. */
export const snakeCase = (value: string): string => {
return words(value).map((word) => word.toLocaleLowerCase()).join("_");
};
Terminal
wget -O src/lib/snakeCase.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/functions/snakeCase.ts
snakeCase.example.ts
import { snakeCase } from "./snakeCase";
const column = snakeCase("createdAt value");
// "created_at_value"
  • value (string) — Text containing common word boundaries.
  • Returns lowercase words separated by underscores.