Skip to content

Commit

Permalink
feat: clean string option
Browse files Browse the repository at this point in the history
  • Loading branch information
TimMikeladze committed Aug 11, 2023
1 parent 64a624a commit d1fc18f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ spaceSlug([

- [word](#gear-word)
- [digits](#gear-digits)
- [cleanString](#gear-cleanstring)
- [uniqueSpaceSlug](#gear-uniquespaceslug)
- [spaceSlug](#gear-spaceslug)

Expand All @@ -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 |
Expand Down
20 changes: 16 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type SpaceSlugDictionary = Partial<{
}>;

export type SpaceSlugOptions = {
cleanString?: (word: string) => string;
dictionary?: Record<string, SpaceSlugDictionary>;
locale?: string;
separator?: string;
Expand Down Expand Up @@ -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[],
Expand Down Expand Up @@ -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');
}
Expand Down
28 changes: 28 additions & 0 deletions tests/spaceSlug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit d1fc18f

Please sign in to comment.