-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathunpack.js
78 lines (69 loc) · 1.88 KB
/
unpack.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
function charCodeToInt (charCode) {
if (charCode > 96) {
return charCode - 87
} else if (charCode > 64) {
return charCode - 29
}
return charCode - 48
}
function unpackBase60 (string) {
const parts = string.split('.')
const whole = parts[0]
const fractional = parts[1] || ''
let multiplier = 1
let start = 0
let out = 0
let sign = 1
// handle negative numbers
if (string.charCodeAt(0) === 45) {
start = 1
sign = -1
}
// handle digits before the decimal
for (let i = start, length = whole.length; i < length; ++i) {
const num = charCodeToInt(whole.charCodeAt(i))
out = (60 * out) + num
}
// handle digits after the decimal
// istanbul ignore next
for (let i = 0, length = fractional.length; i < length; ++i) {
const num = charCodeToInt(fractional.charCodeAt(i))
multiplier = multiplier / 60
out += num * multiplier
}
return out * sign
}
function arrayToInt (array) {
for (let i = 0, length = array.length; i < length; ++i) {
array[i] = unpackBase60(array[i])
}
}
function intToUntil (array, length) {
for (let i = 0; i < length; ++i) {
array[i] = Math.round((array[i - 1] || 0) + (array[i] * 60000))
}
array[length - 1] = Infinity
}
function mapIndices (source, indices) {
const out = []
for (let i = 0, length = indices.length; i < length; ++i) {
out[i] = source[indices[i]]
}
return out
}
function unpack (string) {
const data = string.split('|')
let offsets = data[2].split(' ')
const indices = data[3].split('')
const untils = data[4].split(' ')
arrayToInt(offsets)
arrayToInt(indices)
arrayToInt(untils)
intToUntil(untils, indices.length)
const name = data[0]
const abbreviations = mapIndices((data[1].split(' ')), indices)
const population = data[5] | 0
offsets = mapIndices(offsets, indices)
return { name, abbreviations, offsets, untils, population }
}
export { unpack }