-
Notifications
You must be signed in to change notification settings - Fork 3
/
bidsId.js
43 lines (38 loc) · 1023 Bytes
/
bidsId.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
/**
* Hex
*
* Utils for dealing with hex encoding
*/
export default {
hexToASCII(hexString) {
var hex = hexString.toString();
var str = '';
for (var n = 0; n < hex.length; n += 2) {
str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
}
return str;
},
hexFromASCII(string) {
return string.split('').map((c) => {
return ('0' + c.charCodeAt(0).toString(16)).slice(-2);
}).join('');
},
encodeId (id) {
if (/ds\d{6}/.test(id)) {
return this.hexFromASCII(' ' + id);
} else if (/\d{6}-\d{5}/.test(id)) {
return this.hexFromASCII(id);
} else {
return id;
}
},
decodeId (id) {
let decodedId = this.hexToASCII(id);
if (/\s{4}ds\d{6}/.test(decodedId)) {
return decodedId.slice(4);
} else if (/\d{6}-\d{5}/.test(decodedId)) {
return decodedId.slice(7);
}
return id;
}
};