-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkebab-case.ts
61 lines (58 loc) · 1.1 KB
/
kebab-case.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Please refer to the terms of the license agreement in the root of the project
*
* (c) 2024 Feedzai
*/
// Regex explained: https://regexr.com/5c55v
const kcRegex = /([0-9]+|([A-Z][a-z]+)|[a-z]+|([A-Z]+)(?![a-z]))/g;
/**
* Converts string to kebab case.
*
* @example
*
* kebabCase('Foo Bar')
* // => 'foo-bar'
*
* kebabCase('fooBar')
* // => 'foo-bar'
*
* kebabCase('__FOO_BAR__')
* // => 'foo-bar'
*
* kebabCase(null)
* // => ''
*
* kebabCase('UPPERCASE')
* // => 'uppercase'
*
* kebabCase(false)
* // => 'false'
*
* kebabCase(undefined)
* // => ''
*
* kebabCase(0)
* // => '0'
*
* kebabCase('camelCase')
* // => 'camel-case'
*
* kebabCase('?')
* // => ''
*
* kebabCase('Custom*XML*Parser')
* // => 'custom-xml-parser'
*
* kebabCase('APIFinder')
* // => 'api-finder'
*
* kebabCase('UserAPI20Endpoint')
* // => 'user-api-20-endpoint'
*
* kebabCase('30fghIJ')
* // => '30-fgh-ij'
*/
export function kebabCase(str: string): string {
const MatchingValue = String(str ?? "").match(kcRegex) || [];
return MatchingValue.map((char) => char.toLowerCase()).join("-");
}