From 64a624a77d972cec7cf9a52380256b681f268ec7 Mon Sep 17 00:00:00 2001 From: TimMikeladze Date: Thu, 10 Aug 2023 17:58:31 -0700 Subject: [PATCH] feat: hard coded inputs --- README.md | 27 ++++++++++++++++++++++++--- src/index.ts | 33 ++++++++++++++++++++++++--------- tests/spaceSlug.test.ts | 28 ++++++++++++++++++++++------ 3 files changed, 70 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 37257ca..a7a76c1 100644 --- a/README.md +++ b/README.md @@ -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 + ## :toolbox: Functions @@ -110,19 +131,19 @@ await spaceSlug([], { | Function | Type | | ---------- | ---------- | -| `digits` | `(count?: number) => (options: SpaceSlugOptions) => Set` | +| `digits` | `(count?: number) => (options: SpaceSlugOptions) => string` | ### :gear: uniqueSpaceSlug | Function | Type | | ---------- | ---------- | -| `uniqueSpaceSlug` | `(spaceSlugFn: SpaceSlugFn[], options?: SpaceSlugOptions and UniqueSpaceSlugOptions) => Promise` | +| `uniqueSpaceSlug` | `(spaceSlugFn: SpaceSlugInput[], options?: SpaceSlugOptions and UniqueSpaceSlugOptions) => Promise` | ### :gear: spaceSlug | Function | Type | | ---------- | ---------- | -| `spaceSlug` | `(spaceSlugFns?: SpaceSlugFn[], options?: SpaceSlugOptions) => string` | +| `spaceSlug` | `(spaceSlugInputs?: SpaceSlugInput[], options?: SpaceSlugOptions) => string` | ## :wrench: Constants diff --git a/src/index.ts b/src/index.ts index 4978ca5..45fdedd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,7 +53,9 @@ export type SpaceSlugOptions = { export type SpaceSlugFn = (options: SpaceSlugOptions) => SpaceSlugFnOutput; -export type SpaceSlugFnOutput = Set; +export type SpaceSlugFnOutput = Set | string | string[]; + +export type SpaceSlugInput = SpaceSlugFn | SpaceSlugFnOutput; export type UniqueSpaceSlugOptions = { isUnique?: (slug: string) => Promise; @@ -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 => { @@ -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 = { @@ -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); diff --git a/tests/spaceSlug.test.ts b/tests/spaceSlug.test.ts index 9fa1c49..57e24fd 100644 --- a/tests/spaceSlug.test.ts +++ b/tests/spaceSlug.test.ts @@ -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', () => { @@ -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); }); }); @@ -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', () => {