Skip to content

canvasxyz/utils

Repository files navigation

@canvas-js/utils

standard-readme compliant license NPM version TypeScript types

This repo is for utility methods and types related to JavaScript programming in general.

Anything that is only used by a single package should go in that package's src/utils.ts file; and anything that is related to a specific feature or domain should go in its own utility package.

Table of contents

API

assert

export function assert(condition: unknown, message?: string): asserts condition

Awaitable

export type Awaitable<T> = T | Promise<T>

JSONValue

export type JSONValue =
  | null
  | boolean
  | number
  | string
  | JSONArray
  | JSONObject

export interface JSONArray extends Array<JSONValue> {}
export interface JSONObject {
  [key: string]: JSONValue
}

JSValue

export type JSValue =
  | undefined
  | null
  | boolean
  | number
  | string
  | Uint8Array
  | JSArray
  | JSObject

export interface JSArray extends Array<JSValue> {}
export interface JSObject {
  [key: string]: JSValue
}

export function isArray(value: JSValue): value is JSArray
export function isObject(value: JSValue): value is JSObject

export type JSType =
  | "undefined"
  | "null"
  | "boolean"
  | "number"
  | "string"
  | "Uint8Array"
  | "Array"
  | "Object"

export function typeOf(value: JSValue): JSType

deepEqual

export function deepEqual(a: JSValue, b: JSValue): boolean

mapEntries

export function mapEntries<K extends string, S, T>(
  object: Record<K, S>,
  map: (entry: [key: K, value: S]) => T,
): Record<K, T>

mapKeys

export function mapKeys<K extends string, S, T>(
  object: Record<K, S>,
  map: (key: K) => T,
): Record<K, T>

mapValues

export function mapValues<K extends string, S, T>(
  object: Record<K, S>,
  map: (value: S) => T,
): Record<K, T>

replaceUndefined

/** Recursively replace every `undefined` with `null` in ararys and objects */
export function replaceUndefined(value: JSValue, inPlace = false): JSValue

stripUndefined

/** Recursively remove every object entry with value `undefined` */
export function stripUndefined(value: JSValue, inPlace = false): JSValue

signalInvalidType

export function signalInvalidType(type: never): never

zip

export type Zip<E> = E extends Iterable<any>[]
  ? { [k in keyof E]: E[k] extends Iterable<infer T> ? T : E[k] }
  : never

export function zip<E extends Iterable<any>[]>(
  ...args: E
): Iterable<[...Zip<E>, number]>

License

MIT © Canvas Technologies, Inc.

About

JavaScript utility methods

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published