@choksheak/ts-utils

Chok’s TypeScript Utilities

This package is a time-tested collection of Typescript utilities that I have personally needed in almost every project that I have worked in. Making this an open source, public npm package allows me to share this code freely with others as well as using it for my own personal and professional projects. Feel free to use it in your own projects without any restrictions.

The code supports full tree-shaking. Therefore your code will only grow in size by whatever you use. Any code that you are not using will not contribute to your code size.

πŸš€ @choksheak/ts-utils allows you to reuse common Typescript functions without reinventing the wheels, thus saving you time and reducing the size of your code.

Get Started ↑

Install the package using the standard package manager command:

npm i @choksheak/ts-utils

Examples ↑

arrayBuffer.ts

import { arrayBufferToHex, arrayBufferToBase64 } from "@choksheak/ts-utils/arrayBuffer";

const arrayBuffer = new ArrayBuffer(8); // 8 bytes
const intArray = new Uint16Array(arrayBuffer); // make writeable

intArray[0] = 100; // set first 4 bytes 
intArray[1] = 200; // set next 4 bytes

// Outputs "6400c80000000000".
console.log(arrayBufferToHex(arrayBuffer));

// Outputs "ZADIAAAAAAA=".
console.log(arrayBufferToBase64(arrayBuffer));

asNumber.ts

import { asNumber } from "@choksheak/ts-utils/asNumber";

// Outputs "0".
console.log(asNumber(undefined));

// Outputs "0".
console.log(asNumber(null));

// Outputs "0".
console.log(asNumber(""));

// Outputs "123".
console.log(asNumber("123"));

// Outputs "234.567".
console.log(asNumber(234.567));

// Outputs "0".
console.log(asNumber(NaN));

// Outputs "0".
console.log(asNumber(Infinity));

// Outputs "0".
console.log(asNumber({}));

assert.ts

import { assert } from "@choksheak/ts-utils/assert";

const s: string | null = ...

// Tells typechecker that s cannot be null. Else throws error.
assert(s, "value is falsy");

// Always outputs "string".
console.log(typeof s);

base64Url.ts

import { base64ToBase64URL, base64UrlToBase64 } from "@choksheak/ts-utils/base64Url";

// Outputs "A-B_".
console.log(base64ToBase64URL("A+B/"));

// Outputs "A+B/".
console.log(base64UrlToBase64("A-B_"));

capLength.ts

import { capLength } from "@choksheak/ts-utils/capLength";

// Outputs "01234 ... (5 more)".
console.log(capLength("0123456789", 5));

concatIterators.ts

import { concatIterators } from "@choksheak/ts-utils/concatIterators";

function* iterator1() {
    yield 1;
    yield 2;
}

function* iterator2() {
    yield 3;
    yield 4;
}

// Outputs "Array(4) [ 1, 2, 3, 4 ]".
console.log(Array.from(concatIterators(iterator1(), iterator2())));

duration.ts

import { readableDuration } from "@choksheak/ts-utils/duration";

// Outputs "14 days, 6 hrs, 56 mins, 7 secs, 890 ms".
console.log(readableDuration(1234567890));

isEmpty.ts

import { isEmpty } from "@choksheak/ts-utils/isEmpty";

// Outputs "true".
console.log(isEmpty({}));

isPromise.ts

import { isPromise } from "@choksheak/ts-utils/isPromise";

// Outputs "true".
console.log(isPromise(new Promise()));

kvStore.ts

import { kvStore } from "@choksheak/ts-utils/kvStore";

// Set key1="value1" in the indexed DB.
await kvStore.set("key1", "value1");

// Outputs "value1".
console.log(await kvStore.get("key1"));

// Delete key1 from the KV store.
await kvStore.delete("key1");

// Remove all items from the KV store.
await kvStore.clear();

localStore.ts

import { localStore } from "@choksheak/ts-utils/localStore";

// Set key1="value1" in the local storage.
localStore.set("key1", "value1");

// Outputs "value1".
console.log(localStore.get("key1"));

// Delete key1 from the local store.
localStore.delete("key1");

// Remove all items from the local store.
localStore.clear();

mean.ts

import { mean } from "@choksheak/ts-utils/mean";

// Outputs "2.5".
console.log(mean([1, 2, 3, 4]));

median.ts

import { median } from "@choksheak/ts-utils/median";

// Outputs "4.5".
console.log(median([1, 4, 5, 100]));

nonEmpty.ts

import { nonEmpty } from "@choksheak/ts-utils/nonEmpty";

const s: string | null = ...

// Tells typechecker that s cannot be null or empty. Else throws error "Missing s: null".
// Empty strings are not acceptable as well.
const t = nonEmpty(s, "s");

// Always outputs "string".
console.log(typeof t);

nonNil.ts

import { nonNil } from "@choksheak/ts-utils/nonNil";

const s: string | null = ...

// Tells typechecker that s cannot be null. Else throws error "Missing s: null".
// Empty strings are acceptable with nonNil.
const t = nonNil(s, "s");

// Always outputs "string".
console.log(typeof t);

round.ts

import { round } from "@choksheak/ts-utils/round";

// Outputs "1234.12". Note that the last zero is omitted.
console.log(round(1234.126, 3));

roundToString.ts

import { roundToString } from "@choksheak/ts-utils/roundToString";

// Outputs "1,234.120". Commas are added, and trailing zeros are preserved.
console.log(roundToString(1234.126, 3));

safeBtoa.ts

import { safeBtoa } from "@choksheak/ts-utils/safeBtoa";

const s = "Hello, world! ✨"; // ✨ is outside Latin-1

try { btoa(s) } catch { /* Will not work with "✨" */ }

// Outputs "SGVsbG8sIHdvcmxkISDinKg=".
console.log(safeBtoa(s));

sha256.ts

import { arrayBufferToBase64 } from "@choksheak/ts-utils/arrayBuffer";
import { sha256 } from "@choksheak/ts-utils/sha256";

const arrayBuffer = await sha256("abc123");

// Outputs "bKE9UspwyIPg8LsQHkJaiehiTeUdstI5JZOvaoQRgJA=".
console.log(arrayBufferToBase64(arrayBuffer));

sleep.ts

import { sleep } from "@choksheak/ts-utils/sleep";

// Sleep for 1 second, or 1000 milliseconds.
await sleep(1000);

sum.ts

import { sum } from "@choksheak/ts-utils/sum";

// Outputs "10".
console.log(sum([1, 2, 3, 4]));

timeConstants.ts

import { MS_PER_DAY, MS_PER_HOUR } from "@choksheak/ts-utils/timeConstants";

// Outputs "3600000".
console.log(MS_PER_HOUR);

// Outputs "86400000".
console.log(MS_PER_DAY);

timer.ts

import { sleep } from "@choksheak/ts-utils/sleep";
import { timer } from "@choksheak/ts-utils/timer";

// Create a new timer object and starts the timing.
const t = timer();

// Sleep for 2 seconds.
await sleep(2000);

// Outputs "Operation took 2.000s."
console.log(`Operation took ${t}.`);

toReadableString.ts

import { toReadableString } from "@choksheak/ts-utils/toReadableString";

// Outputs "Error: testing".
console.log(toReadableString(new Error("testing")));

Reference ↑

For more details about each module, please refer to the Reference Documentation