Skip to content

Latest commit

 

History

History
46 lines (34 loc) · 1.23 KB

README.md

File metadata and controls

46 lines (34 loc) · 1.23 KB

Compact · JavaScript

A dependency-free ES module with encoding, section decoding, and schema helpers.

Use from source

Copy src/compact.mjs into your project or import it from this checkout. No transpilation, dependency installation, or application build is needed.

Run this example as an ES module from this directory:

import {
  decodeStream, encodeBitfield, encodeSmallInts,
  encodeText, stream, toBase64Url,
} from "./src/compact.mjs";

const code = toBase64Url(stream([
  encodeText("NODE-A7F3"),
  encodeSmallInts([5, 2, 1], 3),
  encodeBitfield([true, false, true, true]),
]));

console.log(code); // A_oE8QnJ6IilqCboxmjHUUuw
const [name, choices, flags] = decodeStream(code);
console.log(name.asText());     // NODE-A7F3
console.log(choices.asInts());  // [5, 2, 1]
console.log(flags.asBitfield()); // [true, false, true, true]

Tests

From this directory, with Node 18 or newer:

npm test

The suite uses Node's built-in test runner. The root-level tools/check_vectors.py --all also compares the shared vectors and their decoded values.

For the current numeric ranges and decoder behavior, see the input contract. The bit-level rules are in SPEC.md.