-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (46 loc) · 1.46 KB
/
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
46
47
48
49
50
51
52
const chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789-";
const numChars = 64; // chars.length
const validFirstChars = 53; // chars.indexOf("_") + 1
const twoCharPermutations = validFirstChars + validFirstChars * numChars;
module.exports = (index, prefix) => {
if (process.env.NODE_ENV !== "production") {
const validIdent = /^-?[_a-z][_a-z0-9-]*$/i;
if (prefix != null && prefix !== "-" && !validIdent.test(prefix)) {
// eslint-disable-next-line
console.warn(`Expected prefix (${prefix}) to be a valid css class name`);
}
}
let i;
let current;
if (prefix == null || prefix === "") {
if (index < validFirstChars) return chars[index];
const _index = index - validFirstChars;
const c = _index % twoCharPermutations;
if (c < validFirstChars) {
current = "-" + chars[c];
} else {
const _c = c - validFirstChars;
const c0 = _c % validFirstChars;
const c1 = (_c / validFirstChars) | 0;
current = chars[c0] + chars[c1];
}
i = (_index / twoCharPermutations) | 0;
} else if (prefix === "-") {
const c = index % validFirstChars;
current = "-" + chars[c];
i = (index / validFirstChars) | 0;
} else {
if (index === 0) return prefix + chars[0];
current = prefix;
i = index;
}
let base = numChars;
while (i > 0) {
const c = i % numChars;
current += chars[c];
i = (i / numChars) | 0;
base = base * numChars;
}
return current;
};