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
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.
const { spaceSlug, color, digits, noun } from 'space-slug';
const slug = spaceSlug([color(), noun(2), digits(3)], {
separator: '_'
});
// Returns: blue_celestial_labyrinth_718
const { spaceSlug, words } from 'space-slug';
const starwars = words('starwars')
const slug = spaceSlug([starwars(2), digits(2)], {
dictionary: {
en: {
starwars: ['jabba', 'ezra']
}
}
});
/// Returns: jabba-ezra-39
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
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
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';
}
});
const { spaceSlug } from 'space-slug';
await spaceSlug([], {
transform: (x) => x.toUpperCase()
});
// Returns: QUAINT-HORIZON-1293
const { spaceSlug, color, digits } from 'space-slug';
spaceSlug([
'jabba',
digits(),
];
// Returns: jabba-1293
spaceSlug([
color(),
['jabba', 'hutt'],
digits(),
];
// Returns: red-jabba-hutt-3979
Function | Type |
---|---|
word |
(type: string) => (count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |
Function | Type |
---|---|
digits |
(count?: number, noConsecutive?: boolean) => (options: SpaceSlugOptions) => string |
Function | Type |
---|---|
cleanString |
(inputString: string, separator: string) => string |
Function | Type |
---|---|
uniqueSpaceSlug |
(spaceSlugFn: SpaceSlugInput[], options?: SpaceSlugOptions and UniqueSpaceSlugOptions) => Promise<string> |
Function | Type |
---|---|
spaceSlug |
(spaceSlugInputs?: SpaceSlugInput[], options?: SpaceSlugOptions) => string |
- spaceSlugDefaultDictionary
- spaceSlugDefaultOptions
- noun
- adjective
- color
- season
- emoji
- verb
- animal
- cosmos
Constant | Type |
---|---|
spaceSlugDefaultDictionary |
Record<string, Partial<{ [key: string]: string[]; adjectives: string[]; animals: string[]; colors: string[]; cosmos: string[]; emojis: string[]; nouns: string[]; seasons: string[]; verbs: string[]; }>> |
Constant | Type |
---|---|
spaceSlugDefaultOptions |
Partial<SpaceSlugOptions> |
Constant | Type |
---|---|
noun |
(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |
Constant | Type |
---|---|
adjective |
(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |
Constant | Type |
---|---|
color |
(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |
Constant | Type |
---|---|
season |
(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |
Constant | Type |
---|---|
emoji |
(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |
Constant | Type |
---|---|
verb |
(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |
Constant | Type |
---|---|
animal |
(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |
Constant | Type |
---|---|
cosmos |
(count?: number, _words?: string[]) => (options: SpaceSlugOptions) => Set<string> |