-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
145 lines (125 loc) · 4.19 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// @see https://github.com/tuupola/base62/blob/2.0.0/src/Base62.php#L38
const GMP = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
// @see https://github.com/tuupola/base62/blob/2.0.0/src/Base62.php#L39
const INVERTED = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
// @see https://github.com/tuupola/base62/blob/2.0.0/src/Base62/PhpEncoder.php#L47
const convert = (source, sourceBase, targetBase) => {
let s = [...source];
let result = [];
while (s.length) {
let quotient = [];
let remainder = 0;
for (let i = 0; i !== s.length; i += 1) {
const accumulator = s[i] + remainder * sourceBase;
const digit = (accumulator - (accumulator % targetBase)) / targetBase;
remainder = accumulator % targetBase;
if (quotient.length || digit) {
quotient = [
...quotient,
digit,
];
}
}
result = [
remainder,
...result,
];
s = quotient;
}
return result;
};
const getMaxChars = (encoding) => {
switch (encoding) {
case 'ascii':
return 128;
case 'latin1':
return 256;
default:
return 65536;
}
};
const createEncoder = (opt) => {
const options = {
encoding: 'utf16',
characters: GMP,
...opt,
};
if (!['utf16', 'latin1', 'ascii'].includes(options.encoding)) {
throw new Error('Only utf16, latin1, and ascii character encodings are supported');
}
if ([...new Set(options.characters)].length !== 62 || options.characters.length !== 62) {
throw new Error('Character set must contain 62 unique characters');
}
const maxChars = getMaxChars(options.encoding);
// @see https://github.com/tuupola/base62/blob/2.0.0/src/Base62/BaseEncoder.php#L110
const validate = (data) => {
const invalid = [...data].filter(c => !options.characters.includes(c));
if (invalid.length > 0) {
throw new Error(`Data contains invalid characters ${invalid}`);
}
};
return {
// @see https://github.com/tuupola/base62/blob/2.0.0/src/Base62/BaseEncoder.php#L58
encode: (value) => {
const data = [...value].map(c => c.charCodeAt(0));
if (!data.filter(c => c > maxChars)) {
throw new Error(`Data provided is not ${options.encoding}`);
}
let leadingZeroes = 0;
while (data && data[0] === 0) {
leadingZeroes += 1;
data.shift();
}
let converted = convert(data, maxChars, 62);
if (leadingZeroes > 0) {
converted = [
...Array(leadingZeroes).fill(0, 0, leadingZeroes),
...converted,
];
}
return converted.map(index => options.characters[index]).join('');
},
// @see https://github.com/tuupola/base62/blob/2.0.0/src/Base62/BaseEncoder.php#L127
encodeInt: (value) => {
const data = [value];
return convert(data, maxChars, 62).map(index => options.characters[index]).join('');
},
// @see https://github.com/tuupola/base62/blob/2.0.0/src/Base62/BaseEncoder.php#L83
decode: (value) => {
validate(value);
const data = [...value].map(character => options.characters.indexOf(character));
let leadingZeroes = 0;
while (data && data[0] === 0) {
leadingZeroes += 1;
data.shift();
}
let converted = convert(data, 62, maxChars);
if (leadingZeroes > 0) {
converted = [
...Array(leadingZeroes).fill(0, 0, leadingZeroes),
...converted,
];
}
return converted.map(code => String.fromCharCode(code)).join('');
},
// @see https://github.com/tuupola/base62/blob/2.0.0/src/Base62/BaseEncoder.php#L141
decodeInt: (value) => {
validate(value);
const data = [...value].map(character => options.characters.indexOf(character));
return parseInt(convert(data, 62, 10).join(''), 10);
},
};
};
const encode = value => createEncoder({ characters: GMP }).encode(value);
const encodeInt = value => createEncoder({ characters: GMP }).encodeInt(value);
const decode = value => createEncoder({ characters: GMP }).decode(value);
const decodeInt = value => createEncoder({ characters: GMP }).decodeInt(value);
module.exports = {
createEncoder,
encode,
encodeInt,
decode,
decodeInt,
GMP,
INVERTED,
};