deno.land / std@0.224.0 / cli / _run_length.ts

_run_length.ts
View Documentation
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
34
35
36
37
38
39
40
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert } from "../assert/assert.ts";
export function runLengthEncode(arr: number[]) { const data: number[] = []; const runLengths: number[] = [];
let prev: symbol | number = Symbol("none");
for (const x of arr) { if (x === prev) { ++runLengths[runLengths.length - 1]; } else { prev = x; data.push(x); runLengths.push(1); } }
assert(runLengths.every((r) => r < 0x100));
return { d: btoa(String.fromCharCode(...data)), r: btoa(String.fromCharCode(...runLengths)), };}
export function runLengthDecode({ d, r }: { d: string; r: string }) { const data = atob(d); const runLengths = atob(r); let out = "";
for (const [i, ch] of [...runLengths].entries()) { out += data[i]!.repeat(ch.codePointAt(0)!); }
return Uint8Array.from([...out].map((x) => x.codePointAt(0)!));}
std

Version Info

Tagged at
6 months ago