@thi.ng/rle-pack 3.1.42 → 3.1.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-03T12:13:31Z
3
+ - **Last updated**: 2023-12-11T10:07:09Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
package/README.md CHANGED
@@ -82,7 +82,7 @@ For Node.js REPL:
82
82
  const rlePack = await import("@thi.ng/rle-pack");
83
83
  ```
84
84
 
85
- Package sizes (brotli'd, pre-treeshake): ESM: 636 bytes
85
+ Package sizes (brotli'd, pre-treeshake): ESM: 637 bytes
86
86
 
87
87
  ## Dependencies
88
88
 
package/index.js CHANGED
@@ -1,111 +1,98 @@
1
1
  import { BitInputStream } from "@thi.ng/bitstream/input";
2
2
  import { BitOutputStream } from "@thi.ng/bitstream/output";
3
3
  import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
4
- /**
5
- * Compresses input using dynamically sized RLE compression and returns
6
- * result as `Uint8Array`.
7
- *
8
- * @param src -
9
- * @param num - number of input words
10
- * @param wordSize - in bits, range 1 - 32
11
- * @param rleSizes - run-length group sizes (in bits, max. 16)
12
- */
13
- export const encode = (src, num, wordSize = 8, rleSizes = [3, 4, 8, 16]) => {
14
- (wordSize < 1 || wordSize > 32) &&
15
- illegalArgs("word size (1-32 bits only)");
16
- const out = new BitOutputStream(Math.ceil((num * wordSize) / 8) + 4 + 2 + 1)
17
- .write(num, 32)
18
- .write(wordSize - 1, 5);
19
- rleSizes.forEach((x) => {
20
- (x < 1 || x > 16) && illegalArgs("RLE repeat size (1-16 bits only)");
21
- out.write(x - 1, 4);
22
- });
23
- const [rle0, rle1, rle2, rle3] = rleSizes.map((x) => 1 << x);
24
- const chunk = [];
25
- const n1 = num - 1;
26
- let val;
27
- let tail = true;
28
- let n = 0;
29
- let i = 0;
30
- const writeRLE = () => {
31
- const t = n < rle0 ? 0 : n < rle1 ? 1 : n < rle2 ? 2 : 3;
32
- out.writeBit(1);
33
- out.write(t, 2);
34
- out.write(n, rleSizes[t]);
35
- out.write(val, wordSize);
36
- n = 0;
37
- };
38
- const writeChunk = () => {
39
- const m = chunk.length - 1;
40
- const t = m < rle0 ? 0 : m < rle1 ? 1 : m < rle2 ? 2 : 3;
41
- out.writeBit(0);
42
- out.write(t, 2);
43
- out.write(m, rleSizes[t]);
44
- out.writeWords(chunk, wordSize);
45
- chunk.length = 0;
46
- };
47
- for (let x of src) {
48
- if (val === undefined) {
49
- val = x;
50
- }
51
- else if (x !== val) {
52
- if (n > 0) {
53
- writeRLE();
54
- }
55
- else {
56
- chunk.push(val);
57
- if (chunk.length === rle3) {
58
- writeChunk();
59
- }
60
- }
61
- val = x;
62
- }
63
- else {
64
- if (chunk.length) {
65
- writeChunk();
66
- }
67
- if (++n === rle3) {
68
- n--;
69
- writeRLE();
70
- tail = i < n1;
71
- }
72
- }
73
- if (i === n1) {
74
- break;
75
- }
76
- i++;
77
- }
78
- if (chunk.length) {
4
+ const encode = (src, num, wordSize = 8, rleSizes = [3, 4, 8, 16]) => {
5
+ (wordSize < 1 || wordSize > 32) && illegalArgs("word size (1-32 bits only)");
6
+ const out = new BitOutputStream(Math.ceil(num * wordSize / 8) + 4 + 2 + 1).write(num, 32).write(wordSize - 1, 5);
7
+ rleSizes.forEach((x) => {
8
+ (x < 1 || x > 16) && illegalArgs("RLE repeat size (1-16 bits only)");
9
+ out.write(x - 1, 4);
10
+ });
11
+ const [rle0, rle1, rle2, rle3] = rleSizes.map((x) => 1 << x);
12
+ const chunk = [];
13
+ const n1 = num - 1;
14
+ let val;
15
+ let tail = true;
16
+ let n = 0;
17
+ let i = 0;
18
+ const writeRLE = () => {
19
+ const t = n < rle0 ? 0 : n < rle1 ? 1 : n < rle2 ? 2 : 3;
20
+ out.writeBit(1);
21
+ out.write(t, 2);
22
+ out.write(n, rleSizes[t]);
23
+ out.write(val, wordSize);
24
+ n = 0;
25
+ };
26
+ const writeChunk = () => {
27
+ const m = chunk.length - 1;
28
+ const t = m < rle0 ? 0 : m < rle1 ? 1 : m < rle2 ? 2 : 3;
29
+ out.writeBit(0);
30
+ out.write(t, 2);
31
+ out.write(m, rleSizes[t]);
32
+ out.writeWords(chunk, wordSize);
33
+ chunk.length = 0;
34
+ };
35
+ for (let x of src) {
36
+ if (val === void 0) {
37
+ val = x;
38
+ } else if (x !== val) {
39
+ if (n > 0) {
40
+ writeRLE();
41
+ } else {
79
42
  chunk.push(val);
43
+ if (chunk.length === rle3) {
44
+ writeChunk();
45
+ }
46
+ }
47
+ val = x;
48
+ } else {
49
+ if (chunk.length) {
80
50
  writeChunk();
81
- }
82
- else if (tail) {
51
+ }
52
+ if (++n === rle3) {
53
+ n--;
83
54
  writeRLE();
55
+ tail = i < n1;
56
+ }
57
+ }
58
+ if (i === n1) {
59
+ break;
84
60
  }
85
- return out.bytes();
61
+ i++;
62
+ }
63
+ if (chunk.length) {
64
+ chunk.push(val);
65
+ writeChunk();
66
+ } else if (tail) {
67
+ writeRLE();
68
+ }
69
+ return out.bytes();
86
70
  };
87
- export const decode = (src) => {
88
- const input = new BitInputStream(src);
89
- const num = input.read(32);
90
- const wordSize = input.read(5) + 1;
91
- const rleSizes = [0, 0, 0, 0].map(() => input.read(4) + 1);
92
- const out = arrayForWordSize(wordSize, num);
93
- let x, j;
94
- for (let i = 0; i < num;) {
95
- x = input.readBit();
96
- j = i + 1 + input.read(rleSizes[input.read(2)]);
97
- if (x) {
98
- out.fill(input.read(wordSize), i, j);
99
- i = j;
100
- }
101
- else {
102
- for (; i < j; i++) {
103
- out[i] = input.read(wordSize);
104
- }
105
- }
71
+ const decode = (src) => {
72
+ const input = new BitInputStream(src);
73
+ const num = input.read(32);
74
+ const wordSize = input.read(5) + 1;
75
+ const rleSizes = [0, 0, 0, 0].map(() => input.read(4) + 1);
76
+ const out = arrayForWordSize(wordSize, num);
77
+ let x, j;
78
+ for (let i = 0; i < num; ) {
79
+ x = input.readBit();
80
+ j = i + 1 + input.read(rleSizes[input.read(2)]);
81
+ if (x) {
82
+ out.fill(input.read(wordSize), i, j);
83
+ i = j;
84
+ } else {
85
+ for (; i < j; i++) {
86
+ out[i] = input.read(wordSize);
87
+ }
106
88
  }
107
- return out;
89
+ }
90
+ return out;
108
91
  };
109
92
  const arrayForWordSize = (ws, n) => {
110
- return new (ws < 9 ? Uint8Array : ws < 17 ? Uint16Array : Uint32Array)(n);
93
+ return new (ws < 9 ? Uint8Array : ws < 17 ? Uint16Array : Uint32Array)(n);
94
+ };
95
+ export {
96
+ decode,
97
+ encode
111
98
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/rle-pack",
3
- "version": "3.1.42",
3
+ "version": "3.1.44",
4
4
  "description": "Binary run-length encoding packer w/ flexible repeat bit widths",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -24,7 +24,9 @@
24
24
  "author": "Karsten Schmidt (https://thi.ng)",
25
25
  "license": "Apache-2.0",
26
26
  "scripts": {
27
- "build": "yarn clean && tsc --declaration",
27
+ "build": "yarn build:esbuild && yarn build:decl",
28
+ "build:decl": "tsc --declaration --emitDeclarationOnly",
29
+ "build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
28
30
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
29
31
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
32
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
@@ -33,12 +35,12 @@
33
35
  "test": "bun test"
34
36
  },
35
37
  "dependencies": {
36
- "@thi.ng/bitstream": "^2.2.34",
37
- "@thi.ng/errors": "^2.4.4"
38
+ "@thi.ng/bitstream": "^2.2.36",
39
+ "@thi.ng/errors": "^2.4.6"
38
40
  },
39
41
  "devDependencies": {
40
42
  "@microsoft/api-extractor": "^7.38.3",
41
- "@thi.ng/testament": "^0.4.3",
43
+ "esbuild": "^0.19.8",
42
44
  "rimraf": "^5.0.5",
43
45
  "tools": "^0.0.1",
44
46
  "typedoc": "^0.25.4",
@@ -77,5 +79,5 @@
77
79
  ],
78
80
  "year": 2017
79
81
  },
80
- "gitHead": "04d1de79f256d7a53c6b5fd157b37f49bc88e11d\n"
82
+ "gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
81
83
  }