# @latticexyz/utils

# Classes

# CoordMap

CoordMap: Class CoordMap<T>

# Type parameters

Name
T

# Defined in

packages/utils/src/CoordMap.ts:36


# VoxelCoordMap

VoxelCoordMap: Class VoxelCoordMap<T>

# Type parameters

Name
T

# Defined in

packages/utils/src/VoxelCoordMap.ts:14

# Interfaces

# DoWork

DoWork: Interface DoWork<In, Out>

# Type parameters

Name
In
Out

# Defined in

packages/utils/src/worker.ts:3

# Type Aliases

# Area

Ƭ Area: Object

# Type declaration

Name Type
height number
width number
x number
y number

# Defined in

packages/utils/src/types.ts:30


# AsyncFunc

Ƭ AsyncFunc: R extends Promise<unknown> ? Function : Function

# Type parameters

Name Type
A extends any[]
R R

# Defined in

packages/utils/src/types.ts:5


# Cached

Ƭ Cached: Object & { [key in keyof C]: CachedValue<C[key], false> } | Object & { [key in keyof C]: CachedValue<C[key], true> }

# Type parameters

Name
C

# Defined in

packages/utils/src/types.ts:19


# CachedValue

Ƭ CachedValue: Proxied extends true ? V extends Func<infer A, infer B> ? AsyncFunc<A, B> : V extends Record<string, any> ? Cached<V> & Object : Object : V extends Func<infer A, infer B> ? Func<A, B> : V extends Record<string, any> ? V : V & Object

# Type parameters

Name Type
V V
Proxied extends boolean

# Defined in

packages/utils/src/types.ts:7


# Coord

Ƭ Coord: Object

# Type declaration

Name Type
x number
y number

# Defined in

packages/utils/src/types.ts:37


# Func

Ƭ Func: Function

# Type parameters

Name Type
A extends any[]
R R

# Type declaration

▸ (...args): R

# Parameters
Name Type
...args A
# Returns

R

# Defined in

packages/utils/src/types.ts:4


# Logger

Ƭ Logger: ReturnType<typeof enableLogger>

# Defined in

packages/utils/src/types.ts:48


# PromiseValue

Ƭ PromiseValue: Awaited<T>

Deprecated

Use Awaited instead

# Type parameters

Name
T

# Defined in

packages/utils/src/types.ts:26


# ValueOf

Ƭ ValueOf: T[keyof T]

# Type parameters

Name Type
T extends object

# Defined in

packages/utils/src/types.ts:28


# VoxelCoord

Ƭ VoxelCoord: Object

# Type declaration

Name Type
x number
y number
z number

# Defined in

packages/utils/src/types.ts:42

# Functions

# Int32ArrayToUint8Array

Int32ArrayToUint8Array(input): Uint8Array

# Parameters

Name Type
input number[]

# Returns

Uint8Array

# Defined in

packages/utils/src/bytes.ts:43


# Uint8ArrayToHexString

Uint8ArrayToHexString(data): string

# Parameters

Name Type
data Uint8Array

# Returns

string

# Defined in

packages/utils/src/bytes.ts:16


# Uint8ArrayToInt32Array

Uint8ArrayToInt32Array(input): number[]

# Parameters

Name Type
input Uint8Array

# Returns

number[]

# Defined in

packages/utils/src/bytes.ts:52


# areaContains

areaContains(area, coord): boolean

# Parameters

Name Type
area Area
coord Coord

# Returns

boolean

# Defined in

packages/utils/src/area.ts:3


# arrayToIterator

arrayToIterator<T>(array): IterableIterator<T>

Turns an array into an iterator. NOTE: an iterator can only be iterated once.

# Type parameters

Name
T

# Parameters

Name Type Description
array T[] Array to be turned into an iterator

# Returns

IterableIterator<T>

Iterator to iterate through the array

# Defined in

packages/utils/src/iterable.ts:49


# awaitPromise

awaitPromise<T>(): OperatorFunction<T, Awaited<T>>

# Type parameters

Name Type
T extends Promise<unknown, T>

# Returns

OperatorFunction<T, Awaited<T>>

# Defined in

packages/utils/src/rx.ts:26


# awaitStreamValue

awaitStreamValue<T>(stream$, predicate?): Promise<T>

# Type parameters

Name
T

# Parameters

Name Type Description
stream$ Observable<T> RxJS observable to check for the given value
predicate Function Predicate to check

# Returns

Promise<T>

A promise that resolves with the requested value once the predicate is true

# Defined in

packages/utils/src/rx.ts:105


# awaitValue

awaitValue<T>(comp): Promise<T>

# Type parameters

Name
T

# Parameters

Name Type Description
comp IComputedValue<undefined | T> | IObservableValue<undefined | T> Computed/Observable value that is either defined or undefined

# Returns

Promise<T>

promise that resolves with the first truthy computed value

# Defined in

packages/utils/src/mobx.ts:8


# cacheUntilReady

cacheUntilReady<T>(target): Cached<T>

Caches any function calls to the target until the target is ready.

# Type parameters

Name Type
T extends Record<string, any>

# Parameters

Name Type Description
target IObservableValue<undefined | T> | IComputedValue<undefined | T> T extends Cachable

# Returns

Cached<T>

Cached

# Defined in

packages/utils/src/proxy.ts:22


# callWithRetry

callWithRetry<T>(fn, args?, maxRetries?, retryInterval?): Promise<T>

# Type parameters

Name
T

# Parameters

Name Type Default value
fn Function undefined
args any[] []
maxRetries number 10
retryInterval number 1000

# Returns

Promise<T>

# Defined in

packages/utils/src/promise.ts:19


# computedToStream

computedToStream<T>(comp): Observable<T>

# Type parameters

Name
T

# Parameters

Name Type
comp IComputedValue<T> | IObservableValue<T>

# Returns

Observable<T>

# Defined in

packages/utils/src/rx.ts:61


# concatIterators

concatIterators<T>(first, second?): IterableIterator<T>

# Type parameters

Name
T

# Parameters

Name Type
first Iterator<T, any, undefined>
second? Iterator<T, any, undefined>

# Returns

IterableIterator<T>

# Defined in

packages/utils/src/iterable.ts:12


# concatUint8Arrays

concatUint8Arrays(...arrays): Uint8Array

# Parameters

Name Type
...arrays Uint8Array[]

# Returns

Uint8Array

# Defined in

packages/utils/src/bytes.ts:21


# coordToKey

coordToKey(coord): number

# Parameters

Name Type
coord Coord

# Returns

number

# Defined in

packages/utils/src/CoordMap.ts:18


# coordsOf

coordsOf(area): Coord[]

# Parameters

Name Type
area Area

# Returns

Coord[]

# Defined in

packages/utils/src/area.ts:7


# createToInt

createToInt(size): Function

# Parameters

Name Type
size number

# Returns

Function

▸ (value): number

# Parameters
Name Type
value number
# Returns

number

# Defined in

packages/utils/src/bytes.ts:61


# cubicNoiseConfig

cubicNoiseConfig(seed, octave, scale, periodX?, periodY?): Object

Config a cubic noise.

# Parameters

Name Type Default value Description
seed number undefined A seed in the range [0, 1].
octave number undefined -
scale number undefined -
periodX? number Number.MAX_SAFE_INTEGER The number of units after which the x coordinate repeats.
periodY? number Number.MAX_SAFE_INTEGER The number of units after which the y coordinate repeats.

# Returns

Object

A configuration object used by noise functions.

Name Type
octave number
periodX number
periodY number
scale number
seed number

# Defined in

packages/utils/src/cubic.ts:28


# cubicNoiseSample1

cubicNoiseSample1(config, x): number

Sample 1D cubic noise.

# Parameters

Name Type Default value Description
config Object undefined A valid noise configuration.
config.octave number undefined -
config.periodX number periodX -
config.periodY number periodY -
config.scale number undefined -
config.seed number undefined -
x number undefined The X position to sample at.

# Returns

number

A noise value in the range [0, 1].

# Defined in

packages/utils/src/cubic.ts:50


# cubicNoiseSample2

cubicNoiseSample2(config, x, y): number

Sample 2D cubic noise.

# Parameters

Name Type Default value Description
config Object undefined A valid noise configuration.
config.octave number undefined -
config.periodX number periodX -
config.periodY number periodY -
config.scale number undefined -
config.seed number undefined -
x number undefined The X position to sample at.
y number undefined The Y position to sample at.

# Returns

number

A noise value in the range [0, 1].

# Defined in

packages/utils/src/cubic.ts:76


# deferred

deferred<T>(): [Function, Function, Promise<T>]

A convenient way to create a promise with resolve and reject functions.

# Type parameters

Name
T

# Returns

[Function, Function, Promise<T>]

Tuple with resolve function, reject function and promise.

# Defined in

packages/utils/src/deferred.ts:5


# enableLogger

enableLogger(): Object

# Returns

Object

Name Type
Console ConsoleConstructor
addTopic Function
disableFilters Function
enableFilters Function
log Function
logWithTopic Function
removeTopic Function
resetTopics Function
assert Method assert
clear Method clear
count Method count
countReset Method countReset
debug Method debug
dir Method dir
dirxml Method dirxml
error Method error
group Method group
groupCollapsed Method groupCollapsed
groupEnd Method groupEnd
info Method info
profile Method profile
profileEnd Method profileEnd
table Method table
time Method time
timeEnd Method timeEnd
timeLog Method timeLog
timeStamp Method timeStamp
trace Method trace
warn Method warn

# Defined in

packages/utils/src/console.ts:3


# ethAddressToUint8Array

ethAddressToUint8Array(address): Uint8Array

# Parameters

Name Type
address string

# Returns

Uint8Array

# Defined in

packages/utils/src/bytes.ts:56


# euclidean

euclidean(a, b): number

Compute the Euclidean distance between two points https://en.wikipedia.org/wiki/Euclidean_distance

# Parameters

Name Type
a number[]
b number[]

# Returns

number

Euclidian distance between a and b

# Defined in

packages/utils/src/distance.ts:8


# extractEncodedArguments

extractEncodedArguments(input): string

# Parameters

Name Type
input string

# Returns

string

# Defined in

packages/utils/src/eth.ts:38


# filterNullish

filterNullish<T>(): OperatorFunction<T, NonNullable<T>>

# Type parameters

Name
T

# Returns

OperatorFunction<T, NonNullable<T>>

# Defined in

packages/utils/src/rx.ts:20


# filterNullishValues

filterNullishValues<T>(array): T[]

Filters undefined values from an array and lets TypeScript know the resulting array does not have undefined values

# Type parameters

Name
T

# Parameters

Name Type Description
array (undefined | T)[] Array potentially including undefined values

# Returns

T[]

Array without undefined values

# Defined in

packages/utils/src/arrays.ts:17


# formatHex

formatHex(hex): string

# Parameters

Name Type
hex string

# Returns

string

# Defined in

packages/utils/src/bytes.ts:3


# fromWorker

fromWorker<I, O>(worker, input$): Observable<O>

# Type parameters

Name
I
O

# Parameters

Name Type
worker Worker
input$ Observable<I>

# Returns

Observable<O>

# Defined in

packages/utils/src/worker.ts:7


# hexStringToUint8Array

hexStringToUint8Array(hexString): Uint8Array

# Parameters

Name Type
hexString string

# Returns

Uint8Array

# Defined in

packages/utils/src/bytes.ts:9


# interpolate

interpolate(a, b, c, d, x, s, scale): number

# Parameters

Name Type
a number
b number
c number
d number
x number
s number
scale number

# Returns

number

# Defined in

packages/utils/src/cubic.ts:14


# isFunction

isFunction(c): c is Func<any, any>

# Parameters

Name Type
c unknown

# Returns

c is Func<any, any>

# Defined in

packages/utils/src/guards.ts:8


# isNotEmpty

isNotEmpty<T>(array): array is [T, ...T[]]

TypeScript type guard to assert the type of a non-empty array

# Type parameters

Name
T

# Parameters

Name Type Description
array T[] Any array to check for non-emptiness

# Returns

array is [T, ...T[]]

True if the empty is non-empty, else false. TypeScript accepts the array as non-empty after the assertion.

# Defined in

packages/utils/src/arrays.ts:6


# isObject

isObject(c): c is Record<string, any>

# Parameters

Name Type
c unknown

# Returns

c is Record<string, any>

# Defined in

packages/utils/src/guards.ts:4


# keccak256

keccak256(data): string

Compute keccak256 hash from given string and remove padding from the resulting hex string

# Parameters

Name Type Description
data string String to be hashed

# Returns

string

Hash of the given string as hex string without padding

# Defined in

packages/utils/src/hash.ts:12


# keccak256Coord

keccak256Coord(coord): string

# Parameters

Name Type
coord Coord

# Returns

string

# Defined in

packages/utils/src/hash.ts:16


# keyToCoord

keyToCoord(key): Coord

# Parameters

Name Type
key number

# Returns

Coord

# Defined in

packages/utils/src/CoordMap.ts:26


# makeIterable

makeIterable<T>(iterator): IterableIterator<T>

# Type parameters

Name
T

# Parameters

Name Type
iterator Iterator<T, any, undefined>

# Returns

IterableIterator<T>

# Defined in

packages/utils/src/iterable.ts:1


# mapObject

mapObject<S, T>(source, valueMap): T

Utility function to map a source object to an object with the same keys but mapped values

# Type parameters

Name Type
S extends Object
T extends { [key in string | number | symbol]: unknown }

# Parameters

Name Type Description
source S Source object to be mapped
valueMap Function Mapping values of the source object to values of the target object

# Returns

T

An object with the same keys as the source object but mapped values

# Defined in

packages/utils/src/objects.ts:7


# mergeIterators

mergeIterators<A, B>(iteratorA, iteratorB): IterableIterator<[A, B]>

# Type parameters

Name
A
B

# Parameters

Name Type
iteratorA Iterator<A, any, undefined>
iteratorB Iterator<B, any, undefined>

# Returns

IterableIterator<[A, B]>

# Defined in

packages/utils/src/iterable.ts:23


# numValues

numValues(enm): number[]

# Parameters

Name Type Description
enm object Numeric enum

# Returns

number[]

Number array containing the enum values

# Defined in

packages/utils/src/enums.ts:5


# observableToComputed

observableToComputed<T>(obs): IComputedValue<T>

# Type parameters

Name
T

# Parameters

Name Type
obs IObservableValue<T>

# Returns

IComputedValue<T>

# Defined in

packages/utils/src/rx.ts:57


# observableToStream

observableToStream<T>(obs): Observable<T>

# Type parameters

Name
T

# Parameters

Name Type
obs T

# Returns

Observable<T>

# Defined in

packages/utils/src/rx.ts:73


# pack

pack(numbers, bitsPerNumber): number

Packs two unsigned integers in one 32 bit unsigned integer

# Parameters

Name Type Description
numbers number[] Unsigned integers to be packed in 32 bit integer
bitsPerNumber number[] Bits for each number

# Returns

number

Packed 32 bit unsigned integer

# Defined in

packages/utils/src/pack.ts:11


# packTuple

packTuple(numbers): number

# Parameters

Name Type
numbers [number, number]

# Returns

number

# Defined in

packages/utils/src/pack.ts:55


# padToBitLength

padToBitLength(input, bits): string

Pads start of a hex string with 0 to create a bit string of the given length

# Parameters

Name Type Description
input string Hex string
bits number Number of bits in the output hex string

# Returns

string

Hex string of specified length

# Defined in

packages/utils/src/eth.ts:7


# pickRandom

pickRandom<T>(array): T

# Type parameters

Name
T

# Parameters

Name Type Description
array [T, ...T[]] Array to pick a random element from.

# Returns

T

Random element from the given array.

# Defined in

packages/utils/src/random.ts:15


# random

random(to, from?): number

# Parameters

Name Type Default value Description
to number undefined Upper bound (included)
from number 0 Lower bound (included). Default 0.

# Returns

number

A random integer between from and to.

# Defined in

packages/utils/src/random.ts:7


# randomize

randomize(seed, x, y): number

# Parameters

Name Type
seed number
x number
y number

# Returns

number

# Defined in

packages/utils/src/cubic.ts:5


# range

range(total?, step?, from?): Generator<number, void, unknown>

# Parameters

Name Type Default value
total number 0
step number 1
from number 0

# Returns

Generator<number, void, unknown>

# Defined in

packages/utils/src/promise.ts:5


# rejectAfter

rejectAfter<T>(ms, msg): Promise<T>

# Type parameters

Name
T

# Parameters

Name Type
ms number
msg string

# Returns

Promise<T>

# Defined in

packages/utils/src/promise.ts:10


# roundTowardsZero

roundTowardsZero(x): number

For positive inputs: returns the greatest integer less than or equal to its numeric argument. For negative inputs: returns the smallest integer greater than or equal to its numeric argument.

# Parameters

Name Type Description
x number A numeric expression.

# Returns

number

Input rounded towards zero.

# Defined in

packages/utils/src/math.ts:8


# runWorker

runWorker<I, O>(worker): void

# Type parameters

Name
I
O

# Parameters

Name Type
worker DoWork<I, O>

# Returns

void

# Defined in

packages/utils/src/worker.ts:12


# sleep

sleep<T>(timeout, returns?): Promise<T>

# Type parameters

Name
T

# Parameters

Name Type
timeout number
returns? T

# Returns

Promise<T>

# Defined in

packages/utils/src/sleep.ts:1


# splitUint8Arrays

splitUint8Arrays(data, byteLengths): Uint8Array[]

# Parameters

Name Type
data Uint8Array
byteLengths number[]

# Returns

Uint8Array[]

# Defined in

packages/utils/src/bytes.ts:29


# streamToComputed

streamToComputed<T>(stream$): IComputedValue<T | undefined>

# Type parameters

Name
T

# Parameters

Name Type
stream$ Observable<T>

# Returns

IComputedValue<T | undefined>

# Defined in

packages/utils/src/rx.ts:85


# streamToDefinedComputed

streamToDefinedComputed<T>(stream$): Promise<IComputedValue<T>>

# Type parameters

Name
T

# Parameters

Name Type
stream$ Observable<T>

# Returns

Promise<IComputedValue<T>>

# Defined in

packages/utils/src/rx.ts:91


# streamToWrappedValue

streamToWrappedValue<T>(stream$): Promise<Object>

Turns a stream into an updating object for easy access outside of rxjs

# Type parameters

Name
T

# Parameters

Name Type Description
stream$ Observable<T> Stream to turn into a wrapped value

# Returns

Promise<Object>

Object with current key corresponding to last stream value

# Defined in

packages/utils/src/rx.ts:119


# stretch

stretch<T>(spacingDelayMs): UnaryFunction<Observable<T>, Observable<T>>

RxJS operator to stretch out an event stream by a given delay per event

# Type parameters

Name
T

# Parameters

Name Type Description
spacingDelayMs number Delay between each event in ms

# Returns

UnaryFunction<Observable<T>, Observable<T>>

stream of events with at least spacingDelayMs spaceing between event

# Defined in

packages/utils/src/rx.ts:35


# subtract

subtract(from, subtract): CoordMap<boolean>

# Parameters

Name Type
from CoordMap<boolean>
subtract CoordMap<boolean>

# Returns

CoordMap<boolean>

# Defined in

packages/utils/src/CoordMap.ts:7


# tile

tile(coordinate, period): number

# Parameters

Name Type
coordinate number
period number

# Returns

number

# Defined in

packages/utils/src/cubic.ts:9


# timeoutAfter

timeoutAfter<T>(promise, ms, timeoutMsg): Promise<T>

# Type parameters

Name
T

# Parameters

Name Type
promise Promise<T>
ms number
timeoutMsg string

# Returns

Promise<T>

# Defined in

packages/utils/src/promise.ts:15


# to256BitString

to256BitString(input): string

Pads start of a hex string with 0 to create a 256bit hex string which can be used as an Ethereum address

# Parameters

Name Type Description
input string Hex string

# Returns

string

256 bit hex string

# Defined in

packages/utils/src/eth.ts:34


# toEthAddress

toEthAddress(input): string

Pads start of a hex string with 0 to create a 160 bit hex string which can be used as an Ethereum address

# Parameters

Name Type Description
input string Hex string

# Returns

string

160 bit hex string

# Defined in

packages/utils/src/eth.ts:24


# toInt32

toInt32(value): number

# Parameters

Name Type
value number

# Returns

number

# Defined in

packages/utils/src/bytes.ts:72


# transformIterator

transformIterator<A, B>(iterator, transform): IterableIterator<B>

# Type parameters

Name
A
B

# Parameters

Name Type
iterator Iterator<A, any, undefined>
transform Function

# Returns

IterableIterator<B>

# Defined in

packages/utils/src/iterable.ts:35


# unpack

unpack(packed, bitsPerNumber): number[]

Unpacks a packed 32 bit unsigned integer into the original unsigned integers

# Parameters

Name Type Description
packed number Packed 32 bit unsigned integer
bitsPerNumber number[] Bits for each unsigned integer

# Returns

number[]

Array of unpacked unsignd integers

# Defined in

packages/utils/src/pack.ts:45


# unpackTuple

unpackTuple(packed): [number, number]

# Parameters

Name Type
packed number

# Returns

[number, number]

# Defined in

packages/utils/src/pack.ts:59


# uuid

uuid(): string

Generates a version 4 UUID as a hexadecimal string.

# Returns

string

Hexadecimal UUID string.

# Defined in

packages/utils/src/uuid.ts:23