-
Notifications
You must be signed in to change notification settings - Fork 17
/
decompress.js
44 lines (34 loc) · 1.13 KB
/
decompress.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
const marker_regex = /\((\d+)x(\d+)\)/;
const L_PAREN = '(';
const decompress = str => {
let new_str = '';
let paren_index;
do {
paren_index = str.indexOf(L_PAREN);
if (paren_index > -1) {
// Remove part before first marker
let left = str.slice(0, paren_index);
str = str.slice(paren_index);
new_str += left;
// Get marker info
let [match, chars, repeat] = marker_regex.exec(str);
chars = parseInt(chars);
repeat = parseInt(repeat);
// Remove marker
str = str.slice(match.length);
// Create expansion string
let expansion_str = str.slice(0, chars);
let repeated_str = Array(repeat)
.fill(expansion_str)
.join('');
// Add expansion string
new_str += repeated_str;
// Remove expansion str from original str
str = str.slice(expansion_str.length);
} else {
new_str += str;
}
} while (paren_index > -1);
return new_str;
};
module.exports = decompress;