Skip to content

Commit

Permalink
added support for max_run_length and chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed May 21, 2023
1 parent e6e32a8 commit 17d97ac
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ dist

# TernJS port file
.tern-port

# Other
pnpm-lock.yaml
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion decode.js
Original file line number Diff line number Diff line change
@@ -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++) {
Expand Down
44 changes: 28 additions & 16 deletions encode.js
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;
4 changes: 2 additions & 2 deletions index.js
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")
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -24,6 +25,6 @@
},
"homepage": "https://github.com/DanielJDufour/fast-rle#readme",
"devDependencies": {
"ava": "^3.8.2"
"flug": "^2.6.0"
}
}
30 changes: 23 additions & 7 deletions test.js
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);
});

0 comments on commit 17d97ac

Please sign in to comment.