@latticexyz/utils 2.0.0-main-1f3897bd → 2.0.0-main-59054203

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- ## 2.0.0-main-1f3897bd
3
+ ## 2.0.0-main-59054203
4
4
 
5
5
  ### Major Changes
6
6
 
@@ -25,6 +25,7 @@
25
25
 
26
26
  ### Patch Changes
27
27
 
28
+ - 59054203: TS packages now generate their respective `.d.ts` type definition files for better compatibility when using MUD with `moduleResolution` set to `bundler` or `node16` and fixes issues around missing type declarations for dependent packages.
28
29
  - 4e4a3415: bump to latest TS version (5.1.6)
29
30
 
30
31
  ## 2.0.0-next.14
@@ -0,0 +1,402 @@
1
+ import { IComputedValue, IObservableValue } from 'mobx';
2
+ import * as util from 'util';
3
+ import * as rxjs from 'rxjs';
4
+ import { OperatorFunction, Observable } from 'rxjs';
5
+
6
+ /**
7
+ * TypeScript type guard to assert the type of a non-empty array
8
+ * @param array Any array to check for non-emptiness
9
+ * @returns True if the empty is non-empty, else false. TypeScript accepts the array as non-empty after the assertion.
10
+ */
11
+ declare function isNotEmpty<T>(array: T[]): array is [T, ...T[]];
12
+ /**
13
+ * Filters undefined values from an array and lets TypeScript know the resulting array
14
+ * does not have undefined values
15
+ * @param array Array potentially including undefined values
16
+ * @returns Array without undefined values
17
+ */
18
+ declare function filterNullishValues<T>(array: (T | undefined)[]): T[];
19
+
20
+ /**
21
+ * A convenient way to create a promise with resolve and reject functions.
22
+ * @returns Tuple with resolve function, reject function and promise.
23
+ */
24
+ declare function deferred<T>(): [(t: T) => void, (t: Error) => void, Promise<T>];
25
+
26
+ /**
27
+ * @param comp Computed/Observable value that is either defined or undefined
28
+ * @returns promise that resolves with the first truthy computed value
29
+ */
30
+ declare function awaitValue<T>(comp: IComputedValue<T | undefined> | IObservableValue<T | undefined>): Promise<T>;
31
+
32
+ declare function enableLogger(): {
33
+ log: (...logs: string[]) => void;
34
+ logWithTopic: (topic: string, ...logs: string[]) => void;
35
+ enableFilters: () => void;
36
+ disableFilters: () => void;
37
+ addTopic: (topic: string) => void;
38
+ removeTopic: (topic: string) => void;
39
+ resetTopics: () => void;
40
+ assert(condition?: boolean | undefined, ...data: any[]): void;
41
+ assert(value: any, message?: string | undefined, ...optionalParams: any[]): void;
42
+ clear(): void;
43
+ clear(): void;
44
+ count(label?: string | undefined): void;
45
+ count(label?: string | undefined): void;
46
+ countReset(label?: string | undefined): void;
47
+ countReset(label?: string | undefined): void;
48
+ debug(...data: any[]): void;
49
+ debug(message?: any, ...optionalParams: any[]): void;
50
+ dir(item?: any, options?: any): void;
51
+ dir(obj: any, options?: util.InspectOptions | undefined): void;
52
+ dirxml(...data: any[]): void;
53
+ dirxml(...data: any[]): void;
54
+ error(...data: any[]): void;
55
+ error(message?: any, ...optionalParams: any[]): void;
56
+ group(...data: any[]): void;
57
+ group(...label: any[]): void;
58
+ groupCollapsed(...data: any[]): void;
59
+ groupCollapsed(...label: any[]): void;
60
+ groupEnd(): void;
61
+ groupEnd(): void;
62
+ info(...data: any[]): void;
63
+ info(message?: any, ...optionalParams: any[]): void;
64
+ table(tabularData?: any, properties?: string[] | undefined): void;
65
+ table(tabularData: any, properties?: readonly string[] | undefined): void;
66
+ time(label?: string | undefined): void;
67
+ time(label?: string | undefined): void;
68
+ timeEnd(label?: string | undefined): void;
69
+ timeEnd(label?: string | undefined): void;
70
+ timeLog(label?: string | undefined, ...data: any[]): void;
71
+ timeLog(label?: string | undefined, ...data: any[]): void;
72
+ timeStamp(label?: string | undefined): void;
73
+ timeStamp(label?: string | undefined): void;
74
+ trace(...data: any[]): void;
75
+ trace(message?: any, ...optionalParams: any[]): void;
76
+ warn(...data: any[]): void;
77
+ warn(message?: any, ...optionalParams: any[]): void;
78
+ Console: console.ConsoleConstructor;
79
+ profile(label?: string | undefined): void;
80
+ profileEnd(label?: string | undefined): void;
81
+ };
82
+
83
+ type Func<A extends any[], R> = (...args: A) => R;
84
+ type AsyncFunc<A extends any[], R> = R extends Promise<unknown> ? (...args: A) => R : (...args: A) => Promise<R>;
85
+ type CachedValue<V, Proxied extends boolean> = Proxied extends true ? V extends Func<infer A, infer B> ? AsyncFunc<A, B> : V extends Record<string, any> ? Cached<V> & {
86
+ proxied: true;
87
+ } : {
88
+ proxied: true;
89
+ } : V extends Func<infer A, infer B> ? Func<A, B> : V extends Record<string, any> ? V : V & {
90
+ proxied: false;
91
+ };
92
+ type Cached<C> = ({
93
+ proxied: false;
94
+ } & {
95
+ [key in keyof C]: CachedValue<C[key], false>;
96
+ }) | ({
97
+ proxied: true;
98
+ } & {
99
+ [key in keyof C]: CachedValue<C[key], true>;
100
+ });
101
+ /**
102
+ * @deprecated Use Awaited<T> instead
103
+ */
104
+ type PromiseValue<T> = Awaited<T>;
105
+ type ValueOf<T extends object> = T[keyof T];
106
+ type Area = {
107
+ x: number;
108
+ y: number;
109
+ width: number;
110
+ height: number;
111
+ };
112
+ type Coord = {
113
+ x: number;
114
+ y: number;
115
+ };
116
+ type VoxelCoord = {
117
+ x: number;
118
+ y: number;
119
+ z: number;
120
+ };
121
+ type Logger = ReturnType<typeof enableLogger>;
122
+
123
+ declare function isObject(c: unknown): c is Record<string, any>;
124
+ declare function isFunction(c: unknown): c is Func<any, any>;
125
+
126
+ /**
127
+ * Caches any function calls to the target until the target is ready.
128
+ * @param target T extends Cachable
129
+ * @returns Cached<T>
130
+ */
131
+ declare function cacheUntilReady<T extends Record<string, any>>(target: IObservableValue<T | undefined> | IComputedValue<T | undefined>): Cached<T>;
132
+
133
+ /**
134
+ * @param enm Numeric enum
135
+ * @returns Number array containing the enum values
136
+ */
137
+ declare function numValues(enm: object): number[];
138
+
139
+ /**
140
+ * Utility function to map a source object to an object with the same keys but mapped values
141
+ * @param source Source object to be mapped
142
+ * @param valueMap Mapping values of the source object to values of the target object
143
+ * @returns An object with the same keys as the source object but mapped values
144
+ */
145
+ declare function mapObject<S extends {
146
+ [key: string]: unknown;
147
+ }, T extends {
148
+ [key in keyof S]: unknown;
149
+ }>(source: S, valueMap: (value: S[keyof S], key: keyof S) => T[keyof S]): T;
150
+
151
+ /**
152
+ *
153
+ * @param to Upper bound (included)
154
+ * @param from Lower bound (included). Default 0.
155
+ * @returns A random integer between from and to.
156
+ */
157
+ declare function random(to: number, from?: number): number;
158
+ /**
159
+ * @param array Array to pick a random element from.
160
+ * @returns Random element from the given array.
161
+ */
162
+ declare function pickRandom<T>(array: [T, ...T[]]): T;
163
+
164
+ declare function filterNullish<T>(): OperatorFunction<T, NonNullable<T>>;
165
+ declare function awaitPromise<T extends Promise<unknown>>(): OperatorFunction<T, Awaited<T>>;
166
+ /**
167
+ * RxJS operator to stretch out an event stream by a given delay per event
168
+ * @param spacingDelayMs Delay between each event in ms
169
+ * @returns stream of events with at least spacingDelayMs spaceing between event
170
+ */
171
+ declare function stretch<T>(spacingDelayMs: number): rxjs.UnaryFunction<Observable<T>, Observable<T>>;
172
+ declare function observableToComputed<T>(obs: IObservableValue<T>): IComputedValue<T>;
173
+ declare function computedToStream<T>(comp: IComputedValue<T> | IObservableValue<T>): Observable<T>;
174
+ declare function observableToStream<T>(obs: T): Observable<T>;
175
+ declare function streamToComputed<T>(stream$: Observable<T>): IComputedValue<T | undefined>;
176
+ declare function streamToDefinedComputed<T>(stream$: Observable<T>): Promise<IComputedValue<T>>;
177
+ /**
178
+ *
179
+ * @param stream$ RxJS observable to check for the given value
180
+ * @param predicate Predicate to check
181
+ * @returns A promise that resolves with the requested value once the predicate is true
182
+ */
183
+ declare function awaitStreamValue<T>(stream$: Observable<T>, predicate?: (value: T) => boolean): Promise<T>;
184
+ /**
185
+ * Turns a stream into an updating object for easy access outside of rxjs
186
+ * @param stream$ Stream to turn into a wrapped value
187
+ * @returns Object with `current` key corresponding to last stream value
188
+ */
189
+ declare function streamToWrappedValue<T>(stream$: Observable<T>): Promise<{
190
+ current: T;
191
+ }>;
192
+
193
+ /**
194
+ * UUID.core.js - UUID.js for Minimalists
195
+ *
196
+ * @file
197
+ * @author LiosK
198
+ * @version v4.2.0
199
+ * @license Apache License 2.0: Copyright (c) 2010-2018 LiosK
200
+ * @url https://github.com/LiosK/UUID.js/blob/master/src/uuid.core.js
201
+ */
202
+ /**
203
+ * @class
204
+ * @classdesc {@link UUID} object.
205
+ * @hideconstructor
206
+ */
207
+ /**
208
+ * Generates a version 4 UUID as a hexadecimal string.
209
+ * @returns {string} Hexadecimal UUID string.
210
+ */
211
+ declare const uuid: () => string;
212
+
213
+ declare const range: (total?: number, step?: number, from?: number) => Generator<number, void, unknown>;
214
+ declare function rejectAfter<T>(ms: number, msg: string): Promise<T>;
215
+ declare const timeoutAfter: <T>(promise: Promise<T>, ms: number, timeoutMsg: string) => Promise<T>;
216
+ declare const callWithRetry: <T>(fn: (...args: any[]) => Promise<T>, args?: any[], maxRetries?: number, retryInterval?: number) => Promise<T>;
217
+
218
+ declare function sleep<T>(timeout: number, returns?: T): Promise<T>;
219
+
220
+ declare function makeIterable<T>(iterator: Iterator<T>): IterableIterator<T>;
221
+ declare function concatIterators<T>(first: Iterator<T>, second?: Iterator<T>): IterableIterator<T>;
222
+ declare function mergeIterators<A, B>(iteratorA: Iterator<A>, iteratorB: Iterator<B>): IterableIterator<[A, B]>;
223
+ declare function transformIterator<A, B>(iterator: Iterator<A>, transform: (value: A) => B): IterableIterator<B>;
224
+ /**
225
+ * Turns an array into an iterator. NOTE: an iterator can only be iterated once.
226
+ * @param array Array to be turned into an iterator
227
+ * @returns Iterator to iterate through the array
228
+ */
229
+ declare function arrayToIterator<T>(array: T[]): IterableIterator<T>;
230
+
231
+ declare function areaContains(area: Area, coord: Coord): boolean;
232
+ declare function coordsOf(area: Area): Coord[];
233
+
234
+ interface DoWork<In, Out> {
235
+ work(input$: Observable<In>): Observable<Out>;
236
+ }
237
+ declare function fromWorker<I, O>(worker: Worker, input$: Observable<I>): Observable<O>;
238
+ declare function runWorker<I, O>(worker: DoWork<I, O>): void;
239
+
240
+ /**
241
+ * Packs two unsigned integers in one 32 bit unsigned integer
242
+ * @param numbers Unsigned integers to be packed in 32 bit integer
243
+ * @param bitsPerNumber Bits for each number
244
+ * @returns Packed 32 bit unsigned integer
245
+ */
246
+ declare function pack(numbers: number[], bitsPerNumber: number[]): number;
247
+ /**
248
+ * Unpacks a packed 32 bit unsigned integer into the original unsigned integers
249
+ * @param packed Packed 32 bit unsigned integer
250
+ * @param bitsPerNumber Bits for each unsigned integer
251
+ * @returns Array of unpacked unsignd integers
252
+ */
253
+ declare function unpack(packed: number, bitsPerNumber: number[]): number[];
254
+ declare function packTuple(numbers: [number, number]): number;
255
+ declare function unpackTuple(packed: number): [number, number];
256
+
257
+ declare function subtract(from: CoordMap<boolean>, subtract: CoordMap<boolean>): CoordMap<boolean>;
258
+ declare function coordToKey(coord: Coord): number;
259
+ declare function keyToCoord(key: number): Coord;
260
+ declare class CoordMap<T> {
261
+ map: Map<number, T>;
262
+ defaultValue?: T;
263
+ constructor(props?: {
264
+ defaultValue?: T;
265
+ });
266
+ static from<T>(coordMapLike: {
267
+ map: Map<number, T>;
268
+ defaultValue?: T;
269
+ }): CoordMap<T>;
270
+ set(coord: Coord, value: T): Map<number, T>;
271
+ get(coord: Coord): T | undefined;
272
+ keys(): IterableIterator<number>;
273
+ coords(): IterableIterator<Coord>;
274
+ entries(): IterableIterator<[number, T]>;
275
+ toArray(): [Coord, T][];
276
+ values(): IterableIterator<T>;
277
+ delete(coord: Coord): boolean;
278
+ has(coord: Coord): boolean;
279
+ clear(): void;
280
+ get size(): number;
281
+ }
282
+
283
+ declare class VoxelCoordMap<T> {
284
+ map: Map<string, T>;
285
+ defaultValue?: T;
286
+ constructor(props?: {
287
+ defaultValue?: T;
288
+ });
289
+ static from<T>(coordMapLike: {
290
+ map: Map<string, T>;
291
+ defaultValue?: T;
292
+ }): VoxelCoordMap<T>;
293
+ set(coord: VoxelCoord, value: T): Map<string, T>;
294
+ get(coord: VoxelCoord): T | undefined;
295
+ keys(): IterableIterator<string>;
296
+ coords(): IterableIterator<VoxelCoord>;
297
+ entries(): IterableIterator<[string, T]>;
298
+ toArray(): [VoxelCoord, T][];
299
+ values(): IterableIterator<T>;
300
+ delete(coord: VoxelCoord): boolean;
301
+ has(coord: VoxelCoord): boolean;
302
+ clear(): void;
303
+ get size(): number;
304
+ }
305
+
306
+ /**
307
+ * Pads start of a hex string with 0 to create a bit string of the given length
308
+ * @param input Hex string
309
+ * @param bits Number of bits in the output hex string
310
+ * @returns Hex string of specified length
311
+ */
312
+ declare function padToBitLength(input: string, bits: number): string;
313
+ /**
314
+ * Pads start of a hex string with 0 to create a 160 bit hex string
315
+ * which can be used as an Ethereum address
316
+ * @param input Hex string
317
+ * @returns 160 bit hex string
318
+ */
319
+ declare function toEthAddress(input: string): string;
320
+ /**
321
+ * Pads start of a hex string with 0 to create a 256bit hex string
322
+ * which can be used as an Ethereum address
323
+ * @param input Hex string
324
+ * @returns 256 bit hex string
325
+ */
326
+ declare function to256BitString(input: string): string;
327
+ declare function extractEncodedArguments(input: string): string;
328
+
329
+ declare function randomize(seed: number, x: number, y: number): number;
330
+ declare function tile(coordinate: number, period: number): number;
331
+ declare function interpolate(a: number, b: number, c: number, d: number, x: number, s: number, scale: number): number;
332
+ /**
333
+ * Config a cubic noise.
334
+ * @param {Number} seed A seed in the range [0, 1].
335
+ * @param {Number} [periodX] The number of units after which the x coordinate repeats.
336
+ * @param {Number} [periodY] The number of units after which the y coordinate repeats.
337
+ * @returns {Object} A configuration object used by noise functions.
338
+ */
339
+ declare function cubicNoiseConfig(seed: number, octave: number, scale: number, periodX?: number, periodY?: number): {
340
+ seed: number;
341
+ periodX: number;
342
+ periodY: number;
343
+ octave: number;
344
+ scale: number;
345
+ };
346
+ /**
347
+ * Sample 1D cubic noise.
348
+ * @param {Object} config A valid noise configuration.
349
+ * @param {Number} x The X position to sample at.
350
+ * @returns {Number} A noise value in the range [0, 1].
351
+ */
352
+ declare function cubicNoiseSample1(config: ReturnType<typeof cubicNoiseConfig>, x: number): number;
353
+ /**
354
+ * Sample 2D cubic noise.
355
+ * @param {Object} config A valid noise configuration.
356
+ * @param {Number} x The X position to sample at.
357
+ * @param {Number} y The Y position to sample at.
358
+ * @returns {Number} A noise value in the range [0, 1].
359
+ */
360
+ declare function cubicNoiseSample2({ octave, periodX, periodY, seed, scale }: ReturnType<typeof cubicNoiseConfig>, x: number, y: number): number;
361
+
362
+ /**
363
+ * Compute the Euclidean distance between two points
364
+ * https://en.wikipedia.org/wiki/Euclidean_distance
365
+ * @param a
366
+ * @param b
367
+ * @returns Euclidian distance between a and b
368
+ */
369
+ declare function euclidean(a: number[], b: number[]): number;
370
+
371
+ /**
372
+ * For positive inputs: returns the greatest integer less than or equal to its numeric argument.
373
+ * For negative inputs: returns the smallest integer greater than or equal to its numeric argument.
374
+ *
375
+ * @param x A numeric expression.
376
+ * @returns Input rounded towards zero.
377
+ */
378
+ declare function roundTowardsZero(x: number): number;
379
+
380
+ declare function formatHex(hex: string): string;
381
+ declare function hexStringToUint8Array(hexString: string): Uint8Array;
382
+ declare function Uint8ArrayToHexString(data: Uint8Array): string;
383
+ declare function concatUint8Arrays(...arrays: Uint8Array[]): Uint8Array;
384
+ declare function splitUint8Arrays(data: Uint8Array, byteLengths: number[]): Uint8Array[];
385
+ declare function Int32ArrayToUint8Array(input: number[]): Uint8Array;
386
+ declare function Uint8ArrayToInt32Array(input: Uint8Array): number[];
387
+ declare function ethAddressToUint8Array(address: string): Uint8Array;
388
+ declare function createToInt(size: number): (value: number) => number;
389
+ declare const toInt32: (value: number) => number;
390
+
391
+ declare const arrayToHex: (array: Uint8Array | ArrayBuffer) => `0x${string}`;
392
+
393
+ declare const bytesToString: (bytes: Uint8Array) => string;
394
+
395
+ declare const hexToArray: (hex: string) => Uint8Array;
396
+
397
+ declare function isHex(hex: string): boolean;
398
+
399
+ declare const stringToBytes16: (str: string) => Uint8Array;
400
+ declare const stringToBytes32: (str: string) => Uint8Array;
401
+
402
+ export { Area, AsyncFunc, Cached, CachedValue, Coord, CoordMap, DoWork, Func, Int32ArrayToUint8Array, Logger, PromiseValue, Uint8ArrayToHexString, Uint8ArrayToInt32Array, ValueOf, VoxelCoord, VoxelCoordMap, areaContains, arrayToHex, arrayToIterator, awaitPromise, awaitStreamValue, awaitValue, bytesToString, cacheUntilReady, callWithRetry, computedToStream, concatIterators, concatUint8Arrays, coordToKey, coordsOf, createToInt, cubicNoiseConfig, cubicNoiseSample1, cubicNoiseSample2, deferred, enableLogger, ethAddressToUint8Array, euclidean, extractEncodedArguments, filterNullish, filterNullishValues, formatHex, fromWorker, hexStringToUint8Array, hexToArray, interpolate, isFunction, isHex, isNotEmpty, isObject, keyToCoord, makeIterable, mapObject, mergeIterators, numValues, observableToComputed, observableToStream, pack, packTuple, padToBitLength, pickRandom, random, randomize, range, rejectAfter, roundTowardsZero, runWorker, sleep, splitUint8Arrays, streamToComputed, streamToDefinedComputed, streamToWrappedValue, stretch, stringToBytes16, stringToBytes32, subtract, tile, timeoutAfter, to256BitString, toEthAddress, toInt32, transformIterator, unpack, unpackTuple, uuid };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@latticexyz/utils",
3
- "version": "2.0.0-main-1f3897bd",
3
+ "version": "2.0.0-main-59054203",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/latticexyz/mud.git",