Skip to content

Commit

Permalink
feat: makeUnique
Browse files Browse the repository at this point in the history
  • Loading branch information
TimMikeladze committed Sep 18, 2023
1 parent cdec9a3 commit 03ba69e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ await uniqueSpaceSlug([], {
// Returns: a slug that you have verified is unique
```

## 🦄 Making a slug unique

```ts
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

```tsx
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type SpaceSlugInput = SpaceSlugFn | SpaceSlugFnOutput;

export type UniqueSpaceSlugOptions = {
isUnique?: (slug: string) => Promise<boolean>;
makeUnique?: (slug: string) => Promise<string>;
maxAttempts?: number;
usedSlugs?: string[];
};
Expand Down Expand Up @@ -161,6 +162,11 @@ const _uniqueSpaceSlug = async (
const isUnique = await options.isUnique(slug);

if (!isUnique) {
if (options.makeUnique) {
const uniqueSlug = await options.makeUnique(slug);
return _uniqueSpaceSlug([uniqueSlug], options, attempts + 1);
}

return _uniqueSpaceSlug(spaceSlugFn, options, attempts + 1);
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/spaceSlug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ describe('digits', () => {
describe('spaceSlug', () => {
it('works by default', () => {
const slug = spaceSlug();
console.log(slug);
const parts = slug.split('-');
expect(parts).toHaveLength(3);
});
Expand Down

0 comments on commit 03ba69e

Please sign in to comment.