-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress.js
More file actions
97 lines (90 loc) · 1.98 KB
/
compress.js
File metadata and controls
97 lines (90 loc) · 1.98 KB
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
const compress = uncompressed => {
// Build the dictionary.
let i
let dictionary = {}
let c
let wc
let w = ''
let result = []
let dictSize = 256
// Array(256)
// .fill()
// .forEach((e, i) => (dictionary[String.fromCharCode(i)] = i))
for (i = 0; i < 256; i += 1) {
dictionary[String.fromCharCode(i)] = i
}
for (i = 0; i < uncompressed.length; i += 1) {
c = uncompressed.charAt(i)
wc = w + c
//Do not use dictionary[wc] because javascript arrays
//will return values for array['pop'], array['push'] etc
// if (dictionary[wc]) {
if (dictionary.hasOwnProperty(wc)) {
w = wc
} else {
result.push(dictionary[w])
// Add wc to the dictionary.
dictionary[wc] = dictSize++
w = String(c)
}
}
// Output the code for w.
if (w !== '') {
result.push(dictionary[w])
}
return result
}
const compressGolfed = u => {
let i, c, wc
let d = {}
let w = ''
let r = []
let s = 256
for (i = 0; i < s; i++) d[String.fromCharCode(i)] = i
for (i = 0; i < u.length; i++) {
c = u.charAt(i)
wc = w + c
if (d.hasOwnProperty(wc)) w = wc
else {
r.push(d[w])
d[wc] = s++
w = c
}
}
if (w !== '') r.push(d[w])
return r
}
const decompress = compressed => {
// Build the dictionary.
let i
let dictionary = {}
let k
let w = ''
let result
let dictSize = 256
let entry = ''
for (i = 0; i < 256; i += 1) {
dictionary[i] = String.fromCharCode(i)
}
w = String.fromCharCode(compressed[0])
result = w
for (i = 1; i < compressed.length; i += 1) {
k = compressed[i]
if (dictionary[k]) {
entry = dictionary[k]
} else {
if (k === dictSize) {
entry = w + w.charAt(0)
} else {
return null
}
}
result += entry
// Add w+entry[0] to the dictionary.
dictionary[dictSize++] = w + entry.charAt(0)
w = entry
}
return result
}
module.exports.compress = compressGolfed
module.exports.decompress = decompress