-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
45 lines (37 loc) · 947 Bytes
/
index.js
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
/**
* Transform the case in `value` (`string`) to match that of `base` (`string`).
*
* @param {string} value
* @param {string} base
* @returns {string}
*/
export function matchCasing(value, base) {
let index = -1
let cap = false
if (base.toUpperCase() === base) {
return value.toUpperCase()
}
if (base.toLowerCase() === base) {
return value.toLowerCase()
}
while (++index < base.length) {
const char = base.charAt(index)
if (char.toUpperCase() !== char.toLowerCase()) {
const rest = base.slice(index + 1)
cap = char === char.toUpperCase() && rest === rest.toLowerCase()
break
}
}
if (cap) {
index = -1
while (++index < value.length) {
const char = value.charAt(index).toUpperCase()
if (char !== char.toLowerCase()) {
return (
value.slice(0, index) + char + value.slice(index + 1).toLowerCase()
)
}
}
}
return value
}