npm.io
2.1.0 • Published 2 years ago

space-slug

Licence
MIT
Version
2.1.0
Deps
0
Size
103 kB
Vulns
0
Weekly
0
Stars
11

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, adjective, color, digits, noun } from 'space-slug';

const slug = spaceSlug([color(), adjective(), noun(1), 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

Functions

word
Function Type
word (type: string) => (count?: number or undefined, _words?: string[] or undefined) => (options: SpaceSlugOptions) => Set<string>
digits
Function Type
digits (count?: number or undefined, noConsecutive?: boolean or undefined) => (options: SpaceSlugOptions) => string
cleanString
Function Type
cleanString (inputString: string, separator: string) => string
uniqueSpaceSlug
Function Type
uniqueSpaceSlug (spaceSlugFn: SpaceSlugInput[], options?: SpaceSlugOptions and UniqueSpaceSlugOptions) => Promise<string>
spaceSlug
Function Type
spaceSlug (spaceSlugInputs?: SpaceSlugInput[] or undefined, options?: SpaceSlugOptions) => string

Constants

spaceSlugDefaultDictionary
Constant Type
spaceSlugDefaultDictionary SpaceSlugDictionary
spaceSlugDefaultOptions
Constant Type
spaceSlugDefaultOptions Partial<SpaceSlugOptions>
noun
Constant Type
noun (count?: number or undefined, _words?: string[] or undefined) => (options: SpaceSlugOptions) => Set<string>
adjective
Constant Type
adjective (count?: number or undefined, _words?: string[] or undefined) => (options: SpaceSlugOptions) => Set<string>
color
Constant Type
color (count?: number or undefined, _words?: string[] or undefined) => (options: SpaceSlugOptions) => Set<string>
season
Constant Type
season (count?: number or undefined, _words?: string[] or undefined) => (options: SpaceSlugOptions) => Set<string>
emoji
Constant Type
emoji (count?: number or undefined, _words?: string[] or undefined) => (options: SpaceSlugOptions) => Set<string>
verb
Constant Type
verb (count?: number or undefined, _words?: string[] or undefined) => (options: SpaceSlugOptions) => Set<string>
animal
Constant Type
animal (count?: number or undefined, _words?: string[] or undefined) => (options: SpaceSlugOptions) => Set<string>
cosmos
Constant Type
cosmos (count?: number or undefined, _words?: string[] or undefined) => (options: SpaceSlugOptions) => Set<string>

Keywords