-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstring-utils.js
86 lines (76 loc) · 2.22 KB
/
string-utils.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Case-insensitive compare two character codes
* @param {number} referenceCode
* @param {number} testCode
* @see https://github.com/csstree/csstree/blob/41f276e8862d8223eeaa01a3d113ab70bb13d2d9/lib/tokenizer/utils.js#L22
*/
function compareChar(referenceCode, testCode) {
// if uppercase
if (testCode >= 0x0041 && testCode <= 0x005A) {
// shifting the 6th bit makes a letter lowercase
testCode = testCode | 32
}
return referenceCode === testCode
}
/**
* Case-insensitive string-comparison
* @example
* strEquals('test', 'test') // true
* strEquals('test', 'TEST') // true
* strEquals('test', 'TesT') // true
* strEquals('test', 'derp') // false
*
* @param {string} base The string to check against
* @param {string} maybe The test string, possibly containing uppercased characters
* @returns {boolean} true if the two strings are the same, false otherwise
*/
export function strEquals(base, maybe) {
let len = base.length;
if (len !== maybe.length) return false
for (let i = 0; i < len; i++) {
if (compareChar(base.charCodeAt(i), maybe.charCodeAt(i)) === false) {
return false
}
}
return true
}
/**
* Case-insensitive testing whether a string ends with a given substring
*
* @example
* endsWith('test', 'my-test') // true
* endsWith('test', 'est') // false
*
* @param {string} base e.g. '-webkit-transform'
* @param {string} maybe e.g. 'transform'
* @returns {boolean} true if `test` ends with `base`, false otherwise
*/
export function endsWith(base, maybe) {
let len = maybe.length
let offset = len - base.length
if (offset < 0) {
return false
}
for (let i = len - 1; i >= offset; i--) {
if (compareChar(base.charCodeAt(i - offset), maybe.charCodeAt(i)) === false) {
return false
}
}
return true
}
/**
* Case-insensitive testing whether a string starts with a given substring
* @param {string} base
* @param {string} maybe
* @returns {boolean} true if `test` starts with `base`, false otherwise
*/
export function startsWith(base, maybe) {
let len = base.length
if (maybe.length < len) return false
for (let i = 0; i < len; i++) {
if (compareChar(base.charCodeAt(i), maybe.charCodeAt(i)) === false) {
return false
}
}
return true
}