-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathutils.js
60 lines (59 loc) · 1.25 KB
/
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
/**
* @template {PropertyKey} T
* @template U
* @param {T[]} x
* @param {U[]} y
* @returns {Record<T, U>}
*/
function dictZip(x, y) {
const result = /** @type {Record<T, U>} */ ({});
x.forEach((k, i) => {
result[k] = y[i];
});
return result;
}
/**
* @param {number} x
* @param {number} y
* @returns {number[]}
*/
function range(x, y) {
return Array.from({ length: y - x }, (_, i) => x + i);
}
/**
* @param {string} x
* @returns {number}
*/
const getCharCode = (x) => x.charCodeAt(0);
/**
* @param {number} x
* @returns {string}
*/
const getCharFromCode = (x) => String.fromCharCode(x);
/**
*
* @param {string[]} word
* @returns {[string, string][]}
*/
function getPairs(word) {
/** @type {[string, string][]} */
const pairs = [];
let prev_char = word[0];
for (let i = 1; i < word.length; i++) {
const char = word[i];
pairs.push([prev_char, char]);
prev_char = char;
}
return pairs;
}
/**
* @param {string} token
* @returns {string[]}
*/
const splitToken = (token) => token.split("");
module.exports.dictZip = dictZip;
module.exports.range = range;
module.exports.getCharCode = getCharCode;
module.exports.getCharFromCode = getCharFromCode;
module.exports.getPairs = getPairs;
module.exports.splitToken = splitToken;