lib0 Isomorphic Utility Functions

0.2.117 · active · verified Sun Apr 19

lib0 is a monorepo offering a comprehensive collection of isomorphic utility functions designed for both Node.js and browser environments. These utilities cover fundamental programming needs such as array manipulation, efficient binary encoding/decoding, cryptographic operations, assertions, and more. The package is currently at version `0.2.117` on npm, representing its stable release line. However, an active development branch is progressing towards `v1.0.0`, with frequent release candidates (e.g., `v1.0.0-rc.12`) introducing significant architectural changes, particularly within its `delta` and `schema` modules. It supports both CommonJS and ESM module formats. Its primary differentiator lies in providing a robust, performance-optimized, and isomorphic toolkit, minimizing the need for environment-specific code paths and enabling effective dead code elimination by bundlers. The project appears to have an active development cadence, pushing out release candidates regularly.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `lib0/error` for assertions, `lib0/encoding` and `lib0/decoding` for binary data manipulation, and `lib0/array` for common array utilities.

import { assert } from 'lib0/error';
import { createEncoder, writeVarUint, writeString } from 'lib0/encoding';
import { createDecoder, readVarUint, readString } from 'lib0/decoding';

// Basic assertion
assert(1 + 1 === 2, 'Arithmetic should work!');
console.log('Assertion passed: 1 + 1 === 2');

// Encoding and decoding a simple value
const encoder = createEncoder();
writeVarUint(encoder, 12345);
writeString(encoder, 'Hello, lib0!');
const encodedData = encoder.buf;

console.log(`Encoded data (Uint8Array): ${encodedData}`);

const decoder = createDecoder(encodedData);
const decodedNumber = readVarUint(decoder);
const decodedString = readString(decoder);

console.log(`Decoded number: ${decodedNumber}`);
console.log(`Decoded string: ${decodedString}`);

assert(decodedNumber === 12345, 'Decoded number mismatch');
assert(decodedString === 'Hello, lib0!', 'Decoded string mismatch');
console.log('Encoding and decoding successful!');

// Using an array utility
import { from } from 'lib0/array';
const iterable = new Set([1, 2, 3]);
const arr = from(iterable);
assert(arr.length === 3 && arr[0] === 1, 'Array conversion failed');
console.log(`Converted iterable to array: ${arr}`);

view raw JSON →