-
Notifications
You must be signed in to change notification settings - Fork 2
/
encode.js
33 lines (32 loc) · 826 Bytes
/
encode.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
function encode(nums, options) {
const encoded = [];
const max_run_length =
options && options.max_run_length ? options.max_run_length : Infinity;
const chunk = (options && options.chunk) || false;
let current = nums[0];
let run_length = 1;
for (let i = 1; i < nums.length; i++) {
const num = nums[i];
if (num === current && run_length != max_run_length) {
run_length++;
} else {
if (chunk) {
encoded.push([current, run_length]);
} else {
encoded.push(run_length);
encoded.push(current);
}
current = num;
run_length = 1;
}
}
if (chunk) {
encoded.push([current, run_length]);
} else {
encoded.push(run_length);
encoded.push(current);
}
return encoded;
}
module.exports = encode;
module.exports.default = encode;