-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for max_run_length and chunk
- Loading branch information
1 parent
e6e32a8
commit 17d97ac
Showing
7 changed files
with
70 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,6 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
# Other | ||
pnpm-lock.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,33 @@ | ||
const encode = nums => { | ||
const encoded = []; | ||
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++; | ||
} else { | ||
encoded.push(run_length); | ||
encoded.push(current); | ||
current = num; | ||
run_length = 1; | ||
} | ||
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; | ||
}; | ||
} | ||
return encoded; | ||
} | ||
|
||
module.exports = encode; | ||
module.exports.default = encode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module.exports = { | ||
decode: require('./decode'), | ||
encode: require('./encode') | ||
decode: require("./decode"), | ||
encode: require("./encode") | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,32 @@ | ||
const test = require('ava'); | ||
const decode = require('./decode'); | ||
const encode = require('./encode'); | ||
const test = require("flug"); | ||
const decode = require("./decode"); | ||
const encode = require("./encode"); | ||
|
||
const decoded = [3, 3, 3, 3, 3, 8, 0, 0]; | ||
const encoded = [5, 3, 1, 8, 2, 0]; | ||
const encoded_2 = [2, 3, 2, 3, 1, 3, 1, 8, 2, 0]; | ||
const chunked = [ | ||
[3, 5], | ||
[8, 1], | ||
[0, 2] | ||
]; | ||
|
||
test('decode', t => { | ||
test("decode", ({ eq }) => { | ||
const actual = decode(encoded); | ||
t.deepEqual(actual, decoded); | ||
eq(actual, decoded); | ||
}); | ||
|
||
test('encoded', t => { | ||
test("encoded", ({ eq }) => { | ||
const actual = encode(decoded); | ||
t.deepEqual(actual, encoded); | ||
eq(actual, encoded); | ||
}); | ||
|
||
test("encoded with max run length", ({ eq }) => { | ||
const actual = encode(decoded, { max_run_length: 2 }); | ||
eq(actual, encoded_2); | ||
}); | ||
|
||
test("encoded with chunking", ({ eq }) => { | ||
const actual = encode(decoded, { chunk: true }); | ||
eq(actual, chunked); | ||
}); |