0.1.1Updated 7 months ago
import { flow } from "@infinity-beyond/utils/flow.ts";

export const Sluggify = (input: string) => {
  return flow(
    add_underscores_before_capitals,
    remove_invalid_characters,
    remove_repeated_underscores,
    trim_undescores_and_spaces,
    to_lowercase,
  )(input);
}

const add_underscores_before_capitals = (input: string) => input.replace(/([A-Z])/g, '_$1');
const remove_invalid_characters = (input: string) => input.replace(/[^\w]/g, '_');
const remove_repeated_underscores = (input: string) => input.replace(/_{2,}/g, '_');
const trim_undescores_and_spaces = (input: string) => input.replace(/^[\s_]+|[\s_]+$/, '');
const to_lowercase = (input: string) => input.toLowerCase();