Skip to content

Instantly share code, notes, and snippets.

@waptik
Created March 29, 2021 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waptik/2cbd5c9147f71b04be810a3d16cba491 to your computer and use it in GitHub Desktop.
Save waptik/2cbd5c9147f71b04be810a3d16cba491 to your computer and use it in GitHub Desktop.
Generate slugs following dev.to's pattern
import { parameterize } from "inflected"
import { nanoid } from "nanoid"
export function toSlug(slug: string) {
slug = parameterize(slug)
const nano = nanoid(4)
// https://stackoverflow.com/a/16577007/3342703
const id = nano.toLowerCase().replace(/[-_]/g, "")
return `${slug.replace("_", "")}-${id}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment