npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

space-slug

v2.0.2

Published

Generate unique slugs, usernames, numbers and more. If you need a unique string that looks like this `hyperspace-4812` or `blue-whimsical-summer` using an intuitive api with zero dependencies.

Downloads

238

Readme

🐌 space-slug

Get a unique string that looks like this wonderful-jabba or this hyperspace-4812.

Generate unique slugs, usernames, numbers, custom words, and more using an intuitive api with zero dependencies.

const { spaceSlug } from 'space-slug';

const slug = spaceSlug();
// Returns: joyful-illusion-30

📡 Install

npm install space-slug

yarn add space-slug

pnpm add space-slug

👋 Hello there! Follow me @linesofcode or visit linesofcode.dev for more cool projects like this one.

🚀 Getting Started

const { spaceSlug, color, digits, noun } from 'space-slug';

const slug = spaceSlug([color(), noun(2), digits(3)], {
  separator: '_'
});
// Returns: blue_celestial_labyrinth_718

📚 Custom dictionaries and locales

const { spaceSlug, word, SpaceSlugDictionary } from 'space-slug';

const dictionary: SpaceSlugDictionary = {
  en: {
    starwars: ['jabba', 'ezra'],
  },
};

const slug = spaceSlug([word('starwars')(2), digits(2)], {
  dictionary,
  locale: 'en',
});
/// Returns: jabba-ezra-39

🗃️ Tracking used slugs

const { uniqueSpaceSlug, color, digits } from 'space-slug';

const slug = await uniqueSpaceSlug([
  color(1),
  digits(4),
], {
  usedSlugs: ['orange-3918']
});
// Returns: a slug that is not orange-3918

✅ Verifying that a slug is a unique

const { uniqueSpaceSlug } from 'space-slug';

await uniqueSpaceSlug([], {
  maxAttempts: 10, // default is 10 attempts before throwing an error
  isUnique: async (slug) => {
    // check database to see if slug is unique
    return true;
  }
});
// Returns: a slug that you have verified is unique

🦄 Making a slug unique

await uniqueSpaceSlug(['jabba'], {
  isUnique: async (slug) => {
    // a db lookup to see if slug is unique
    return false;
  },
  makeUnique: async (slug) => {
    // somehow make the slug unique
    return slug + '-hutt';
  }
});

✨ Transforming a slug

const { spaceSlug } from 'space-slug';

await spaceSlug([], {
  transform: (x) => x.toUpperCase()
});
// Returns: QUAINT-HORIZON-1293

✏️ Using hard-coded values

const { spaceSlug, color, digits } from 'space-slug';

spaceSlug([
  'jabba',
  digits(),
];
// Returns: jabba-1293

spaceSlug([
  color(),
  ['jabba', 'hutt'],
  digits(),
];
// Returns: red-jabba-hutt-3979

:toolbox: Functions

:gear: word

| Function | Type | | ---------- | ---------- | | word | (type: string) => (count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |

:gear: digits

| Function | Type | | ---------- | ---------- | | digits | (count?: number, noConsecutive?: boolean) => (options: SpaceSlugOptions) => string |

:gear: cleanString

| Function | Type | | ---------- | ---------- | | cleanString | (inputString: string, separator: string) => string |

:gear: uniqueSpaceSlug

| Function | Type | | ---------- | ---------- | | uniqueSpaceSlug | (spaceSlugFn: SpaceSlugInput[], options?: SpaceSlugOptions and UniqueSpaceSlugOptions) => Promise<string> |

:gear: spaceSlug

| Function | Type | | ---------- | ---------- | | spaceSlug | (spaceSlugInputs?: SpaceSlugInput[], options?: SpaceSlugOptions) => string |

:wrench: Constants

:gear: spaceSlugDefaultDictionary

| Constant | Type | | ---------- | ---------- | | spaceSlugDefaultDictionary | SpaceSlugDictionary |

:gear: spaceSlugDefaultOptions

| Constant | Type | | ---------- | ---------- | | spaceSlugDefaultOptions | Partial<SpaceSlugOptions> |

:gear: noun

| Constant | Type | | ---------- | ---------- | | noun | (count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |

:gear: adjective

| Constant | Type | | ---------- | ---------- | | adjective | (count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |

:gear: color

| Constant | Type | | ---------- | ---------- | | color | (count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |

:gear: season

| Constant | Type | | ---------- | ---------- | | season | (count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |

:gear: emoji

| Constant | Type | | ---------- | ---------- | | emoji | (count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |

:gear: verb

| Constant | Type | | ---------- | ---------- | | verb | (count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |

:gear: animal

| Constant | Type | | ---------- | ---------- | | animal | (count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |

:gear: cosmos

| Constant | Type | | ---------- | ---------- | | cosmos | (count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |