2.0.2 • Published 3 months ago

space-slug v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

🐌 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

FunctionType
word(type: string) => (count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string>

:gear: digits

FunctionType
digits(count?: number, noConsecutive?: boolean) => (options: SpaceSlugOptions) => string

:gear: cleanString

FunctionType
cleanString(inputString: string, separator: string) => string

:gear: uniqueSpaceSlug

FunctionType
uniqueSpaceSlug(spaceSlugFn: SpaceSlugInput[], options?: SpaceSlugOptions and UniqueSpaceSlugOptions) => Promise<string>

:gear: spaceSlug

FunctionType
spaceSlug(spaceSlugInputs?: SpaceSlugInput[], options?: SpaceSlugOptions) => string

:wrench: Constants

:gear: spaceSlugDefaultDictionary

ConstantType
spaceSlugDefaultDictionarySpaceSlugDictionary

:gear: spaceSlugDefaultOptions

ConstantType
spaceSlugDefaultOptionsPartial<SpaceSlugOptions>

:gear: noun

ConstantType
noun(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string>

:gear: adjective

ConstantType
adjective(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string>

:gear: color

ConstantType
color(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string>

:gear: season

ConstantType
season(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string>

:gear: emoji

ConstantType
emoji(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string>

:gear: verb

ConstantType
verb(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string>

:gear: animal

ConstantType
animal(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string>

:gear: cosmos

ConstantType
cosmos(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string>
2.0.2

3 months ago

2.0.1

3 months ago

2.0.0

3 months ago

1.1.0

8 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago