Skip to content

Commit

Permalink
feat: hard coded inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
TimMikeladze committed Aug 11, 2023
1 parent 9e404dc commit 64a624a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ await spaceSlug([], {
// Returns: QUAINT-HORIZON-1293
```

## ✏️ Using hard-coded values

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

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

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

<!-- TSDOC_START -->

## :toolbox: Functions
Expand All @@ -110,19 +131,19 @@ await spaceSlug([], {

| Function | Type |
| ---------- | ---------- |
| `digits` | `(count?: number) => (options: SpaceSlugOptions) => Set<string>` |
| `digits` | `(count?: number) => (options: SpaceSlugOptions) => string` |

### :gear: uniqueSpaceSlug

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

### :gear: spaceSlug

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


## :wrench: Constants
Expand Down
33 changes: 24 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export type SpaceSlugOptions = {

export type SpaceSlugFn = (options: SpaceSlugOptions) => SpaceSlugFnOutput;

export type SpaceSlugFnOutput = Set<string>;
export type SpaceSlugFnOutput = Set<string> | string | string[];

export type SpaceSlugInput = SpaceSlugFn | SpaceSlugFnOutput;

export type UniqueSpaceSlugOptions = {
isUnique?: (slug: string) => Promise<boolean>;
Expand Down Expand Up @@ -118,12 +120,12 @@ export const digits = (count?: number) => (options: SpaceSlugOptions) => {
set.add(index.toString());
}

return new Set([Array.from(set).join('')]);
return Array.from(set).join('');
};

// eslint-disable-next-line no-underscore-dangle
const _uniqueSpaceSlug = async (
spaceSlugFn: SpaceSlugFn[],
spaceSlugFn: SpaceSlugInput[],
options: SpaceSlugOptions & UniqueSpaceSlugOptions,
attempts: number
): Promise<string> => {
Expand Down Expand Up @@ -152,12 +154,12 @@ const _uniqueSpaceSlug = async (
};

export const uniqueSpaceSlug = async (
spaceSlugFn: SpaceSlugFn[],
spaceSlugFn: SpaceSlugInput[],
options: SpaceSlugOptions & UniqueSpaceSlugOptions = {}
) => _uniqueSpaceSlug(spaceSlugFn, options, 1);

export const spaceSlug = (
spaceSlugFns?: SpaceSlugFn[],
spaceSlugInputs?: SpaceSlugInput[],
options: SpaceSlugOptions = {}
) => {
const mergedOptions = {
Expand All @@ -168,13 +170,26 @@ export const spaceSlug = (
const transformFn = mergedOptions.transform || ((x) => x);

// eslint-disable-next-line no-underscore-dangle
const _spaceSlugFns = !spaceSlugFns?.length
const _spaceSlugFns = !spaceSlugInputs?.length
? [adjective(1), noun(1), digits(2)]
: spaceSlugFns;
: spaceSlugInputs;

const slug = _spaceSlugFns.map((fn) => {
const set = fn(mergedOptions);
return transformFn(Array.from(set).join(mergedOptions.separator));
const set = typeof fn === 'function' ? fn(mergedOptions) : fn;

let s: string;

if (typeof set === 'string') {
s = set;
} else if (typeof set === 'object' && set instanceof Set) {
s = Array.from(set).join(mergedOptions.separator);
} else if (Array.isArray(set)) {
s = set.join(mergedOptions.separator);
} else {
throw new Error('Invalid space slug function output');
}

return transformFn(s);
});

return slug.join(mergedOptions.separator);
Expand Down
28 changes: 22 additions & 6 deletions tests/spaceSlug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ describe('digits', () => {
separator: '-',
});

expect(res.size).toBe(1);
const [digit] = Array.from(res);
expect(digit).toHaveLength(4);
expect(res.length).toBe(4);
});

it('size 10', () => {
Expand All @@ -69,9 +67,7 @@ describe('digits', () => {
separator: '-',
});

expect(res.size).toBe(1);
const [digit] = Array.from(res);
expect(digit).toHaveLength(10);
expect(res.length).toBe(10);
});
});

Expand Down Expand Up @@ -162,6 +158,26 @@ describe('spaceSlug', () => {
expect(slug).toBe('HUTT-JABBA');
}
});

it('hardcoded inputs', () => {
expect(
spaceSlug([word('starwars')(2), 'ezra'], {
dictionary,
}).endsWith('-ezra')
).toBe(true);

expect(
spaceSlug([word('starwars')(2), ['ezra', 'holocron']], {
dictionary,
}).endsWith('-ezra-holocron')
).toBe(true);

expect(
spaceSlug([word('starwars')(2), new Set(['ezra', 'holocron'])], {
dictionary,
}).endsWith('-ezra-holocron')
).toBe(true);
});
});

describe('uniqueSpaceSlug', () => {
Expand Down

0 comments on commit 64a624a

Please sign in to comment.