From d1fc18ffcf2f091ace2a12a1b3571eba54da8373 Mon Sep 17 00:00:00 2001 From: TimMikeladze Date: Thu, 10 Aug 2023 18:32:21 -0700 Subject: [PATCH] feat: clean string option --- README.md | 7 +++++++ src/index.ts | 20 ++++++++++++++++---- tests/spaceSlug.test.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a7a76c1..cd2e44f 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ spaceSlug([ - [word](#gear-word) - [digits](#gear-digits) +- [cleanString](#gear-cleanstring) - [uniqueSpaceSlug](#gear-uniquespaceslug) - [spaceSlug](#gear-spaceslug) @@ -133,6 +134,12 @@ spaceSlug([ | ---------- | ---------- | | `digits` | `(count?: number) => (options: SpaceSlugOptions) => string` | +### :gear: cleanString + +| Function | Type | +| ---------- | ---------- | +| `cleanString` | `(inputString: string, separator: string) => string` | + ### :gear: uniqueSpaceSlug | Function | Type | diff --git a/src/index.ts b/src/index.ts index 45fdedd..a11685a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,6 +45,7 @@ export type SpaceSlugDictionary = Partial<{ }>; export type SpaceSlugOptions = { + cleanString?: (word: string) => string; dictionary?: Record; locale?: string; separator?: string; @@ -123,6 +124,12 @@ export const digits = (count?: number) => (options: SpaceSlugOptions) => { return Array.from(set).join(''); }; +export const cleanString = (inputString: string, separator: string) => + inputString + .replace(/[^a-zA-Z0-9\s]+/g, '') + .replace(/\s+/g, separator) + .trim(); + // eslint-disable-next-line no-underscore-dangle const _uniqueSpaceSlug = async ( spaceSlugFn: SpaceSlugInput[], @@ -167,24 +174,29 @@ export const spaceSlug = ( ...options, }; - const transformFn = mergedOptions.transform || ((x) => x); + const transformFn = mergedOptions.transform || ((x) => x.toLowerCase()); // eslint-disable-next-line no-underscore-dangle const _spaceSlugFns = !spaceSlugInputs?.length ? [adjective(1), noun(1), digits(2)] : spaceSlugInputs; + // eslint-disable-next-line no-underscore-dangle + const _cleanString = options.cleanString + ? options.cleanString + : (s: string) => cleanString(s, mergedOptions.separator as string); + const slug = _spaceSlugFns.map((fn) => { const set = typeof fn === 'function' ? fn(mergedOptions) : fn; let s: string; if (typeof set === 'string') { - s = set; + s = _cleanString(set); } else if (typeof set === 'object' && set instanceof Set) { - s = Array.from(set).join(mergedOptions.separator); + s = Array.from(set).map(_cleanString).join(mergedOptions.separator); } else if (Array.isArray(set)) { - s = set.join(mergedOptions.separator); + s = set.map(_cleanString).join(mergedOptions.separator); } else { throw new Error('Invalid space slug function output'); } diff --git a/tests/spaceSlug.test.ts b/tests/spaceSlug.test.ts index 57e24fd..fb6c25d 100644 --- a/tests/spaceSlug.test.ts +++ b/tests/spaceSlug.test.ts @@ -178,6 +178,34 @@ describe('spaceSlug', () => { }).endsWith('-ezra-holocron') ).toBe(true); }); + + it('cleans string', () => { + expect( + spaceSlug( + [ + '#a very', + `b@d + string`, + ], + { + dictionary, + } + ) + ).toBe('a-very-bd-string'); + + expect( + spaceSlug(['a very', 'b@d string'], { + dictionary, + separator: '', + }) + ).toBe('averybdstring'); + + expect( + spaceSlug(['-_foo!bar-', 'JABBA'], { + dictionary, + }) + ).toBe('foobar-jabba'); + }); }); describe('uniqueSpaceSlug', () => {