-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsers.ts
More file actions
32 lines (24 loc) · 868 Bytes
/
parsers.ts
File metadata and controls
32 lines (24 loc) · 868 Bytes
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
type ParserType = (string: string) => string
type ParserGeneraterType =
(
matcher: RegExp,
replacer: (string: string) => string
) => ParserType
const generateParser: ParserGeneraterType = (matcher, replacer?) => {
const regex = RegExp(matcher, 'g')
return string => {
// * throw an error if not a string
if (typeof string !== 'string') {
throw new TypeError(`expected an argument of type string, but not`);
}
// * if no match between string and matcher
if (!string.match(regex)) {
return string;
}
// * executes the replacer function for each match
// ? replacer can take any arguments valid for String.prototype.replace
return string.replace(regex, replacer);
}
}
const camelToKebab:ParserType = generateParser(/[A-Z]/, match => `-${match.toLowerCase()}`)
export { ParserType, camelToKebab }