-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
43 lines (38 loc) · 1.42 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import {StringTransformFunc} from "../atomic.js";
const PUNCTUATION_REGEX = new RegExp(/[`=(){}<>;',.~!@#$%^&*_+|:"?\-\\\[\]\/]/g);
const NON_ALPHANUMERIC_REGEX = new RegExp(/[^\w\s]|_/g);
const WHITESPACE_REGEX = new RegExp(/\s/g);
const MULTI_WHITESPACE_REGEX = new RegExp(/\s{2,}/g);
const lowercase: StringTransformFunc = (str: string) => str.toLocaleLowerCase();
const trim: StringTransformFunc = (str: string) => str.trim();
const replaceUnicode: StringTransformFunc = (str: string) => str.normalize('NFD').replace(/[\u0300-\u036f]/g, "");
const removePunctuation: StringTransformFunc = (str: string) => str.replace(PUNCTUATION_REGEX, '');
const removeNonAlphanumeric: StringTransformFunc = (str: string) => str.replace(NON_ALPHANUMERIC_REGEX, '');
const removeWhitespace: StringTransformFunc = (str: string) => str.replace(WHITESPACE_REGEX, '');
const replaceMultiWhitespace: StringTransformFunc = (str: string) => str.replace(MULTI_WHITESPACE_REGEX, ' ');
const transforms = {
lowercase,
trim,
replaceMultiWhitespace,
replaceUnicode,
removeWhitespace,
removePunctuation
}
const strDefaultTransforms: StringTransformFunc[] = [
replaceUnicode,
removePunctuation,
trim,
replaceMultiWhitespace,
lowercase
];
export {
lowercase,
trim,
replaceUnicode,
removePunctuation,
removeWhitespace,
removeNonAlphanumeric,
replaceMultiWhitespace,
transforms,
strDefaultTransforms
}