From 17d97ace8a55b72fad4ca1378420fefbf859d4f6 Mon Sep 17 00:00:00 2001 From: DanielJDufour Date: Sun, 21 May 2023 08:26:19 -0400 Subject: [PATCH] added support for max_run_length and chunk --- .gitignore | 3 +++ README.md | 14 ++++++++++---- decode.js | 2 +- encode.js | 44 ++++++++++++++++++++++++++++---------------- index.js | 4 ++-- package.json | 5 +++-- test.js | 30 +++++++++++++++++++++++------- 7 files changed, 70 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 6704566..2a3f12e 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,6 @@ dist # TernJS port file .tern-port + +# Other +pnpm-lock.yaml diff --git a/README.md b/README.md index c3e316d..cb95089 100644 --- a/README.md +++ b/README.md @@ -7,16 +7,22 @@ Fast Run Length Encoder and Decoder import decode from 'fast-rle/decode'; const encoded = [5, 3, 1, 8, 2, 0]; -const decoded = decode(encoded); -// [3, 3, 3, 3, 3, 8, 0, 0] +decode(encoded); +[3, 3, 3, 3, 3, 8, 0, 0] ``` ## encoding ```javascript import encode from 'fast-rle/decode'; const numbers = [3, 3, 3, 3, 3, 8, 0, 0]; -const encoded = decode(numbers); -// [5, 3, 1, 8, 2, 0] +encode(numbers); +[5, 3, 1, 8, 2, 0] + +encode(decoded, { max_run_length: 2 }); +[2, 3, 2, 3, 1, 3, 1, 8, 2, 0] + +encode(decoded, { chunk: true }); +[2, 3, 2, 3, 1, 3, 1, 8, 2, 0] ``` # support diff --git a/decode.js b/decode.js index 82afa5e..0cf4e66 100644 --- a/decode.js +++ b/decode.js @@ -1,6 +1,6 @@ const decode = nums => { const decoded = []; - for (let i = 0; i < nums.length; i+=2) { + for (let i = 0; i < nums.length; i += 2) { const run_length = nums[i]; const value = nums[i + 1]; for (let ii = 0; ii < run_length; ii++) { diff --git a/encode.js b/encode.js index c7efcac..ae42db1 100644 --- a/encode.js +++ b/encode.js @@ -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; diff --git a/index.js b/index.js index 47e18d6..67e089b 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ module.exports = { - decode: require('./decode'), - encode: require('./encode') + decode: require("./decode"), + encode: require("./encode") }; diff --git a/package.json b/package.json index 7811829..a7c9cbe 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Fast Run Length Encoder and Decoder", "main": "index.js", "scripts": { - "test": "ava" + "format": "npx prettier --arrow-parens=avoid --trailing-comma=none --write *.js", + "test": "node test" }, "repository": { "type": "git", @@ -24,6 +25,6 @@ }, "homepage": "https://github.com/DanielJDufour/fast-rle#readme", "devDependencies": { - "ava": "^3.8.2" + "flug": "^2.6.0" } } diff --git a/test.js b/test.js index b13cfef..58fbb46 100644 --- a/test.js +++ b/test.js @@ -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); });