@latticexyz/utils 2.2.18-9fa07c8489f1fbf167d0db01cd9aaa645a29c8e2 → 2.2.18-c2ad22c7feb566e1731ff16e8be291746bdffb3e
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/dist/index.js +1 -867
- package/dist/index.js.map +1 -1
- package/package.json +2 -11
- package/dist/index.cjs +0 -976
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -402
package/dist/index.cjs
DELETED
@@ -1,976 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __create = Object.create;
|
3
|
-
var __defProp = Object.defineProperty;
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
-
var __export = (target, all) => {
|
9
|
-
for (var name in all)
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
-
};
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
-
for (let key of __getOwnPropNames(from))
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
-
}
|
18
|
-
return to;
|
19
|
-
};
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
-
mod
|
27
|
-
));
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
-
|
30
|
-
// src/index.ts
|
31
|
-
var src_exports = {};
|
32
|
-
__export(src_exports, {
|
33
|
-
CoordMap: () => CoordMap,
|
34
|
-
Int32ArrayToUint8Array: () => Int32ArrayToUint8Array,
|
35
|
-
Uint8ArrayToHexString: () => Uint8ArrayToHexString,
|
36
|
-
Uint8ArrayToInt32Array: () => Uint8ArrayToInt32Array,
|
37
|
-
VoxelCoordMap: () => VoxelCoordMap,
|
38
|
-
areaContains: () => areaContains,
|
39
|
-
arrayToHex: () => arrayToHex,
|
40
|
-
arrayToIterator: () => arrayToIterator,
|
41
|
-
awaitPromise: () => awaitPromise,
|
42
|
-
awaitStreamValue: () => awaitStreamValue,
|
43
|
-
awaitValue: () => awaitValue,
|
44
|
-
bytesToString: () => bytesToString,
|
45
|
-
cacheUntilReady: () => cacheUntilReady,
|
46
|
-
callWithRetry: () => callWithRetry,
|
47
|
-
computedToStream: () => computedToStream,
|
48
|
-
concatIterators: () => concatIterators,
|
49
|
-
concatUint8Arrays: () => concatUint8Arrays,
|
50
|
-
coordToKey: () => coordToKey,
|
51
|
-
coordsOf: () => coordsOf,
|
52
|
-
createToInt: () => createToInt,
|
53
|
-
cubicNoiseConfig: () => cubicNoiseConfig,
|
54
|
-
cubicNoiseSample1: () => cubicNoiseSample1,
|
55
|
-
cubicNoiseSample2: () => cubicNoiseSample2,
|
56
|
-
deferred: () => deferred,
|
57
|
-
enableLogger: () => enableLogger,
|
58
|
-
ethAddressToUint8Array: () => ethAddressToUint8Array,
|
59
|
-
euclidean: () => euclidean,
|
60
|
-
extractEncodedArguments: () => extractEncodedArguments,
|
61
|
-
filterNullish: () => filterNullish,
|
62
|
-
filterNullishValues: () => filterNullishValues,
|
63
|
-
formatHex: () => formatHex,
|
64
|
-
fromWorker: () => fromWorker,
|
65
|
-
hexStringToUint8Array: () => hexStringToUint8Array,
|
66
|
-
hexToArray: () => hexToArray,
|
67
|
-
interpolate: () => interpolate,
|
68
|
-
isFunction: () => isFunction,
|
69
|
-
isHex: () => isHex,
|
70
|
-
isNotEmpty: () => isNotEmpty,
|
71
|
-
isObject: () => isObject,
|
72
|
-
keyToCoord: () => keyToCoord,
|
73
|
-
makeIterable: () => makeIterable,
|
74
|
-
mapObject: () => mapObject,
|
75
|
-
mergeIterators: () => mergeIterators,
|
76
|
-
numValues: () => numValues,
|
77
|
-
observableToComputed: () => observableToComputed,
|
78
|
-
observableToStream: () => observableToStream,
|
79
|
-
pack: () => pack,
|
80
|
-
packTuple: () => packTuple,
|
81
|
-
padToBitLength: () => padToBitLength,
|
82
|
-
pickRandom: () => pickRandom,
|
83
|
-
random: () => random,
|
84
|
-
randomize: () => randomize,
|
85
|
-
range: () => range,
|
86
|
-
rejectAfter: () => rejectAfter,
|
87
|
-
roundTowardsZero: () => roundTowardsZero,
|
88
|
-
runWorker: () => runWorker,
|
89
|
-
sleep: () => sleep,
|
90
|
-
splitUint8Arrays: () => splitUint8Arrays,
|
91
|
-
streamToComputed: () => streamToComputed,
|
92
|
-
streamToDefinedComputed: () => streamToDefinedComputed,
|
93
|
-
streamToWrappedValue: () => streamToWrappedValue,
|
94
|
-
stretch: () => stretch,
|
95
|
-
stringToBytes16: () => stringToBytes16,
|
96
|
-
stringToBytes32: () => stringToBytes32,
|
97
|
-
subtract: () => subtract,
|
98
|
-
tile: () => tile,
|
99
|
-
timeoutAfter: () => timeoutAfter,
|
100
|
-
to256BitString: () => to256BitString,
|
101
|
-
toEthAddress: () => toEthAddress,
|
102
|
-
toInt32: () => toInt32,
|
103
|
-
transformIterator: () => transformIterator,
|
104
|
-
unpack: () => unpack,
|
105
|
-
unpackTuple: () => unpackTuple,
|
106
|
-
uuid: () => uuid
|
107
|
-
});
|
108
|
-
module.exports = __toCommonJS(src_exports);
|
109
|
-
|
110
|
-
// src/arrays.ts
|
111
|
-
function isNotEmpty(array) {
|
112
|
-
if (array.length === 0) return false;
|
113
|
-
return true;
|
114
|
-
}
|
115
|
-
function filterNullishValues(array) {
|
116
|
-
return array.filter((value) => value != null);
|
117
|
-
}
|
118
|
-
|
119
|
-
// src/deferred.ts
|
120
|
-
function deferred() {
|
121
|
-
let resolve = null;
|
122
|
-
let reject = null;
|
123
|
-
const promise = new Promise((r, rj) => {
|
124
|
-
resolve = (t) => r(t);
|
125
|
-
reject = (e) => rj(e);
|
126
|
-
});
|
127
|
-
return [resolve, reject, promise];
|
128
|
-
}
|
129
|
-
|
130
|
-
// src/mobx.ts
|
131
|
-
var import_mobx = require("mobx");
|
132
|
-
async function awaitValue(comp) {
|
133
|
-
const [resolve, , promise] = deferred();
|
134
|
-
const dispose = (0, import_mobx.reaction)(
|
135
|
-
() => comp.get(),
|
136
|
-
(value2) => {
|
137
|
-
if (value2) {
|
138
|
-
resolve(value2);
|
139
|
-
}
|
140
|
-
},
|
141
|
-
{ fireImmediately: true }
|
142
|
-
);
|
143
|
-
const value = await promise;
|
144
|
-
dispose();
|
145
|
-
return value;
|
146
|
-
}
|
147
|
-
|
148
|
-
// src/guards.ts
|
149
|
-
function isObject(c) {
|
150
|
-
return typeof c === "object" && !Array.isArray(c) && c !== null;
|
151
|
-
}
|
152
|
-
function isFunction(c) {
|
153
|
-
return c instanceof Function;
|
154
|
-
}
|
155
|
-
|
156
|
-
// src/proxy.ts
|
157
|
-
var import_mobx2 = require("mobx");
|
158
|
-
var import_proxy_deep = __toESM(require("proxy-deep"), 1);
|
159
|
-
function deepAccess(target, path) {
|
160
|
-
if (path.length === 0) return target;
|
161
|
-
if (path.length === 1) return target[path[0]];
|
162
|
-
const [next, ...rest] = path;
|
163
|
-
const nextTarget = target[next];
|
164
|
-
if (!isObject(nextTarget)) throw new Error("Path does not exist on the target");
|
165
|
-
return deepAccess(nextTarget, rest);
|
166
|
-
}
|
167
|
-
function cacheUntilReady(target) {
|
168
|
-
const callQueue = [];
|
169
|
-
const proxiedTarget = new import_proxy_deep.default(
|
170
|
-
{},
|
171
|
-
{
|
172
|
-
get(_t, prop) {
|
173
|
-
const targetReady = target.get();
|
174
|
-
if (targetReady) {
|
175
|
-
if (prop === "proxied") return false;
|
176
|
-
return Reflect.get(targetReady, prop);
|
177
|
-
} else {
|
178
|
-
if (prop === "proxied") return true;
|
179
|
-
if (prop === "name") return "ProxiedTarget";
|
180
|
-
if (prop === "toJSON") return () => ({ proxied: true });
|
181
|
-
return this.nest(() => void 0);
|
182
|
-
}
|
183
|
-
},
|
184
|
-
apply(_, thisArg, args) {
|
185
|
-
const targetReady = target.get();
|
186
|
-
if (targetReady) {
|
187
|
-
const targetFunc = deepAccess(targetReady, this.path);
|
188
|
-
if (!isFunction(targetFunc)) throw new Error("Target is not callable");
|
189
|
-
return Reflect.apply(targetFunc, thisArg, args);
|
190
|
-
} else {
|
191
|
-
const [resolve, reject, promise] = deferred();
|
192
|
-
callQueue.push({ path: this.path, args, resolve, reject });
|
193
|
-
return promise;
|
194
|
-
}
|
195
|
-
}
|
196
|
-
}
|
197
|
-
);
|
198
|
-
(0, import_mobx2.reaction)(
|
199
|
-
() => target.get(),
|
200
|
-
(targetReady) => {
|
201
|
-
if (!targetReady) return;
|
202
|
-
const queuedCalls = callQueue.splice(0);
|
203
|
-
for (const { path, args, resolve, reject } of queuedCalls) {
|
204
|
-
const target2 = deepAccess(targetReady, path);
|
205
|
-
if (args && isFunction(target2)) {
|
206
|
-
(async () => {
|
207
|
-
try {
|
208
|
-
resolve(await target2(...args));
|
209
|
-
} catch (e) {
|
210
|
-
reject(e);
|
211
|
-
}
|
212
|
-
})();
|
213
|
-
} else {
|
214
|
-
resolve(target2);
|
215
|
-
}
|
216
|
-
}
|
217
|
-
}
|
218
|
-
);
|
219
|
-
return proxiedTarget;
|
220
|
-
}
|
221
|
-
|
222
|
-
// src/enums.ts
|
223
|
-
function numValues(enm) {
|
224
|
-
const nums = [];
|
225
|
-
for (const val of Object.values(enm)) {
|
226
|
-
if (!isNaN(Number(val))) {
|
227
|
-
nums.push(Number(val));
|
228
|
-
}
|
229
|
-
}
|
230
|
-
return nums;
|
231
|
-
}
|
232
|
-
|
233
|
-
// src/objects.ts
|
234
|
-
function mapObject(source, valueMap) {
|
235
|
-
const target = {};
|
236
|
-
for (const key in source) {
|
237
|
-
target[key] = valueMap(source[key], key);
|
238
|
-
}
|
239
|
-
return target;
|
240
|
-
}
|
241
|
-
|
242
|
-
// src/random.ts
|
243
|
-
function random(to, from = 0) {
|
244
|
-
return Math.floor(Math.random() * (to - from + 1)) + from;
|
245
|
-
}
|
246
|
-
function pickRandom(array) {
|
247
|
-
return array[random(array.length - 1)];
|
248
|
-
}
|
249
|
-
|
250
|
-
// src/rx.ts
|
251
|
-
var import_rxjs = require("rxjs");
|
252
|
-
var import_mobx3 = require("mobx");
|
253
|
-
function filterNullish() {
|
254
|
-
return (0, import_rxjs.pipe)(
|
255
|
-
(0, import_rxjs.filter)((x) => x != null)
|
256
|
-
);
|
257
|
-
}
|
258
|
-
function awaitPromise() {
|
259
|
-
return (0, import_rxjs.pipe)((0, import_rxjs.concatMap)((x) => x));
|
260
|
-
}
|
261
|
-
function stretch(spacingDelayMs) {
|
262
|
-
return (0, import_rxjs.pipe)(
|
263
|
-
(0, import_rxjs.timestamp)(),
|
264
|
-
(0, import_rxjs.scan)((acc, curr) => {
|
265
|
-
let delay2 = 0;
|
266
|
-
if (acc !== null) {
|
267
|
-
const timeDelta = curr.timestamp - acc.timestamp;
|
268
|
-
delay2 = timeDelta > spacingDelayMs ? 0 : spacingDelayMs - timeDelta;
|
269
|
-
}
|
270
|
-
return {
|
271
|
-
timestamp: curr.timestamp,
|
272
|
-
delay: delay2,
|
273
|
-
value: curr.value
|
274
|
-
};
|
275
|
-
}, null),
|
276
|
-
filterNullish(),
|
277
|
-
(0, import_rxjs.mergeMap)((i) => (0, import_rxjs.of)(i.value).pipe((0, import_rxjs.delay)(i.delay)), 1)
|
278
|
-
);
|
279
|
-
}
|
280
|
-
function observableToComputed(obs) {
|
281
|
-
return (0, import_mobx3.computed)(() => obs.get());
|
282
|
-
}
|
283
|
-
function computedToStream(comp) {
|
284
|
-
const stream = new import_rxjs.ReplaySubject(1);
|
285
|
-
(0, import_mobx3.reaction)(
|
286
|
-
() => comp.get(),
|
287
|
-
(value) => {
|
288
|
-
if (value != null) stream.next(value);
|
289
|
-
},
|
290
|
-
{ fireImmediately: true }
|
291
|
-
);
|
292
|
-
return stream;
|
293
|
-
}
|
294
|
-
function observableToStream(obs) {
|
295
|
-
const stream = new import_rxjs.ReplaySubject(1);
|
296
|
-
(0, import_mobx3.reaction)(
|
297
|
-
() => (0, import_mobx3.toJS)(obs),
|
298
|
-
(value) => {
|
299
|
-
if (value != null) stream.next(value);
|
300
|
-
},
|
301
|
-
{ fireImmediately: true }
|
302
|
-
);
|
303
|
-
return stream;
|
304
|
-
}
|
305
|
-
function streamToComputed(stream$) {
|
306
|
-
const value = import_mobx3.observable.box();
|
307
|
-
stream$.subscribe((val) => (0, import_mobx3.runInAction)(() => value.set(val)));
|
308
|
-
return (0, import_mobx3.computed)(() => value.get());
|
309
|
-
}
|
310
|
-
async function streamToDefinedComputed(stream$) {
|
311
|
-
const value = import_mobx3.observable.box();
|
312
|
-
stream$.subscribe((val) => (0, import_mobx3.runInAction)(() => value.set(val)));
|
313
|
-
const computedValue = (0, import_mobx3.computed)(() => value.get());
|
314
|
-
await awaitValue(computedValue);
|
315
|
-
return computedValue;
|
316
|
-
}
|
317
|
-
async function awaitStreamValue(stream$, predicate = (value) => value != null) {
|
318
|
-
const [resolve, , promise] = deferred();
|
319
|
-
stream$.pipe((0, import_rxjs.first)(predicate)).subscribe(resolve);
|
320
|
-
return promise;
|
321
|
-
}
|
322
|
-
async function streamToWrappedValue(stream$) {
|
323
|
-
const value = {};
|
324
|
-
stream$.subscribe((v) => value.current = v);
|
325
|
-
value.current = await awaitStreamValue(stream$);
|
326
|
-
return value;
|
327
|
-
}
|
328
|
-
|
329
|
-
// src/uuid.ts
|
330
|
-
var uuid = function() {
|
331
|
-
const rand = _getRandomInt, hex = _hexAligner;
|
332
|
-
return hex(rand(32), 8) + // time_low
|
333
|
-
"-" + hex(rand(16), 4) + // time_mid
|
334
|
-
"-" + hex(16384 | rand(12), 4) + // time_hi_and_version
|
335
|
-
"-" + hex(32768 | rand(14), 4) + // clock_seq_hi_and_reserved clock_seq_low
|
336
|
-
"-" + hex(rand(48), 12);
|
337
|
-
};
|
338
|
-
var _getRandomInt = function(x) {
|
339
|
-
if (x < 0 || x > 53) {
|
340
|
-
return NaN;
|
341
|
-
}
|
342
|
-
const n = 0 | Math.random() * 1073741824;
|
343
|
-
return x > 30 ? n + (0 | Math.random() * (1 << x - 30)) * 1073741824 : n >>> 30 - x;
|
344
|
-
};
|
345
|
-
var _hexAligner = function(num, length) {
|
346
|
-
let str = num.toString(16), i = length - str.length, z = "0";
|
347
|
-
for (; i > 0; i >>>= 1, z += z) {
|
348
|
-
if (i & 1) {
|
349
|
-
str = z + str;
|
350
|
-
}
|
351
|
-
}
|
352
|
-
return str;
|
353
|
-
};
|
354
|
-
|
355
|
-
// src/sleep.ts
|
356
|
-
function sleep(timeout, returns) {
|
357
|
-
return new Promise((resolve) => setTimeout(() => resolve(returns), timeout));
|
358
|
-
}
|
359
|
-
|
360
|
-
// src/promise.ts
|
361
|
-
var range = function* (total = 0, step = 1, from = 0) {
|
362
|
-
for (let i = 0; i < total; yield from + i++ * step) {
|
363
|
-
}
|
364
|
-
};
|
365
|
-
async function rejectAfter(ms, msg) {
|
366
|
-
await sleep(ms);
|
367
|
-
throw new Error(msg);
|
368
|
-
}
|
369
|
-
var timeoutAfter = async (promise, ms, timeoutMsg) => {
|
370
|
-
return Promise.race([promise, rejectAfter(ms, timeoutMsg)]);
|
371
|
-
};
|
372
|
-
var callWithRetry = (fn, args = [], maxRetries = 10, retryInterval = 1e3) => {
|
373
|
-
const [resolve, reject, promise] = deferred();
|
374
|
-
const process = async () => {
|
375
|
-
let res;
|
376
|
-
for (let i = 0; i < maxRetries; i++) {
|
377
|
-
try {
|
378
|
-
res = await fn(...args);
|
379
|
-
resolve(res);
|
380
|
-
break;
|
381
|
-
} catch (e) {
|
382
|
-
if (i < maxRetries - 1) {
|
383
|
-
console.info("[CallWithRetry Failed] attempt number=" + i, fn);
|
384
|
-
console.error(e);
|
385
|
-
await sleep(Math.min(retryInterval * 2 ** i + Math.random() * 100, 15e3));
|
386
|
-
} else {
|
387
|
-
reject(e);
|
388
|
-
}
|
389
|
-
}
|
390
|
-
}
|
391
|
-
};
|
392
|
-
process();
|
393
|
-
return promise;
|
394
|
-
};
|
395
|
-
|
396
|
-
// src/iterable.ts
|
397
|
-
function makeIterable(iterator) {
|
398
|
-
const iterable = {
|
399
|
-
...iterator,
|
400
|
-
[Symbol.iterator]() {
|
401
|
-
return this;
|
402
|
-
}
|
403
|
-
};
|
404
|
-
return iterable;
|
405
|
-
}
|
406
|
-
function concatIterators(first2, second) {
|
407
|
-
if (!second) return makeIterable(first2);
|
408
|
-
return makeIterable({
|
409
|
-
next() {
|
410
|
-
const next = first2.next();
|
411
|
-
if (!next.done) return next;
|
412
|
-
return second.next();
|
413
|
-
}
|
414
|
-
});
|
415
|
-
}
|
416
|
-
function mergeIterators(iteratorA, iteratorB) {
|
417
|
-
const iterator = {
|
418
|
-
next() {
|
419
|
-
const nextA = iteratorA.next();
|
420
|
-
const nextB = iteratorB.next();
|
421
|
-
if (nextA.done && nextB.done) return { done: true, value: null };
|
422
|
-
return { value: [nextA.value, nextB.value] };
|
423
|
-
}
|
424
|
-
};
|
425
|
-
return makeIterable(iterator);
|
426
|
-
}
|
427
|
-
function transformIterator(iterator, transform) {
|
428
|
-
return makeIterable({
|
429
|
-
next() {
|
430
|
-
const { done, value } = iterator.next();
|
431
|
-
return { done, value: done ? value : transform(value) };
|
432
|
-
}
|
433
|
-
});
|
434
|
-
}
|
435
|
-
function arrayToIterator(array) {
|
436
|
-
let i = 0;
|
437
|
-
const iterator = {
|
438
|
-
next() {
|
439
|
-
const done = i >= array.length;
|
440
|
-
if (done) return { done, value: null };
|
441
|
-
return { value: array[i++] };
|
442
|
-
}
|
443
|
-
};
|
444
|
-
return makeIterable(iterator);
|
445
|
-
}
|
446
|
-
|
447
|
-
// src/area.ts
|
448
|
-
function areaContains(area, coord) {
|
449
|
-
return coord.x >= area.x && coord.y >= area.y && coord.x < area.x + area.width && coord.y < area.y + area.height;
|
450
|
-
}
|
451
|
-
function coordsOf(area) {
|
452
|
-
const coords = [];
|
453
|
-
for (let dx = 0; dx < area.width; dx++) {
|
454
|
-
for (let dy = 0; dy < area.height; dy++) {
|
455
|
-
coords.push({ x: area.x + dx, y: area.y + dy });
|
456
|
-
}
|
457
|
-
}
|
458
|
-
return coords;
|
459
|
-
}
|
460
|
-
|
461
|
-
// src/worker.ts
|
462
|
-
var import_rxjs2 = require("rxjs");
|
463
|
-
function fromWorker(worker, input$) {
|
464
|
-
input$.subscribe((event) => worker.postMessage(event));
|
465
|
-
return (0, import_rxjs2.fromEvent)(worker, "message").pipe((0, import_rxjs2.map)((e) => e.data));
|
466
|
-
}
|
467
|
-
function runWorker(worker) {
|
468
|
-
const input$ = (0, import_rxjs2.fromEvent)(self, "message");
|
469
|
-
const output$ = worker.work(input$.pipe((0, import_rxjs2.map)((event) => event.data)));
|
470
|
-
output$.subscribe((event) => self.postMessage(event));
|
471
|
-
}
|
472
|
-
|
473
|
-
// src/pack.ts
|
474
|
-
function rightMask(input, keep) {
|
475
|
-
return input & 2 ** keep - 1;
|
476
|
-
}
|
477
|
-
function pack(numbers, bitsPerNumber) {
|
478
|
-
if (bitsPerNumber.reduce((acc, curr) => acc + curr, 0) > 32) {
|
479
|
-
throw new Error("JS pretends integers are 32 bit when bitshifts are involved");
|
480
|
-
}
|
481
|
-
if (numbers.length !== bitsPerNumber.length) throw new Error("Arrays' lengths must match");
|
482
|
-
for (let i = 0; i < numbers.length; i++) {
|
483
|
-
if (numbers[i] < 0) {
|
484
|
-
throw new Error("Underflow: can only pack unsigned integer");
|
485
|
-
}
|
486
|
-
if (numbers[i] > 2 ** bitsPerNumber[i] - 1) {
|
487
|
-
const error = `Overflow: ${numbers[i]} does not fit in ${bitsPerNumber[i]} bits`;
|
488
|
-
throw new Error(error);
|
489
|
-
}
|
490
|
-
}
|
491
|
-
let packed = 0;
|
492
|
-
for (let i = 0; i < numbers.length; i++) {
|
493
|
-
packed = packed << bitsPerNumber[i] | numbers[i];
|
494
|
-
}
|
495
|
-
return packed;
|
496
|
-
}
|
497
|
-
function unpack(packed, bitsPerNumber) {
|
498
|
-
const numbers = [];
|
499
|
-
let shiftedPacked = packed;
|
500
|
-
for (let i = bitsPerNumber.length - 1; i >= 0; i--) {
|
501
|
-
numbers.unshift(rightMask(shiftedPacked, bitsPerNumber[i]));
|
502
|
-
shiftedPacked = shiftedPacked >>> bitsPerNumber[i];
|
503
|
-
}
|
504
|
-
return numbers;
|
505
|
-
}
|
506
|
-
function packTuple(numbers) {
|
507
|
-
return pack(numbers, [8, 24]);
|
508
|
-
}
|
509
|
-
function unpackTuple(packed) {
|
510
|
-
return unpack(packed, [8, 24]);
|
511
|
-
}
|
512
|
-
|
513
|
-
// src/CoordMap.ts
|
514
|
-
var LOWER_HALF_MASK = 2 ** 16 - 1;
|
515
|
-
var MAX_SUPPORTED = 2 ** 15 - 1;
|
516
|
-
function subtract(from, subtract2) {
|
517
|
-
const result = new CoordMap();
|
518
|
-
for (const coord of from.coords()) {
|
519
|
-
if (subtract2.get(coord)) continue;
|
520
|
-
result.set(coord, true);
|
521
|
-
}
|
522
|
-
return result;
|
523
|
-
}
|
524
|
-
function coordToKey(coord) {
|
525
|
-
const key = coord.x << 16 | coord.y & LOWER_HALF_MASK;
|
526
|
-
return key;
|
527
|
-
}
|
528
|
-
function keyToCoord(key) {
|
529
|
-
const x = key >> 16;
|
530
|
-
const y = key << 16 >> 16;
|
531
|
-
return { x, y };
|
532
|
-
}
|
533
|
-
var CoordMap = class _CoordMap {
|
534
|
-
constructor(props) {
|
535
|
-
this.map = /* @__PURE__ */ new Map();
|
536
|
-
this.defaultValue = props?.defaultValue;
|
537
|
-
}
|
538
|
-
static from(coordMapLike) {
|
539
|
-
const coordMap = new _CoordMap();
|
540
|
-
coordMap.map = coordMapLike.map;
|
541
|
-
coordMap.defaultValue = coordMapLike.defaultValue;
|
542
|
-
return coordMap;
|
543
|
-
}
|
544
|
-
set(coord, value) {
|
545
|
-
if (coord.x > MAX_SUPPORTED || coord.x < -1 * MAX_SUPPORTED || coord.y > MAX_SUPPORTED || coord.y < -1 * MAX_SUPPORTED) {
|
546
|
-
throw new Error(`CoordMap only supports coords up to ${MAX_SUPPORTED}`);
|
547
|
-
}
|
548
|
-
return this.map.set(coordToKey(coord), value);
|
549
|
-
}
|
550
|
-
get(coord) {
|
551
|
-
return this.map.get(coordToKey(coord)) ?? this.defaultValue;
|
552
|
-
}
|
553
|
-
keys() {
|
554
|
-
return this.map.keys();
|
555
|
-
}
|
556
|
-
coords() {
|
557
|
-
return transformIterator(this.map.keys(), (key) => keyToCoord(key));
|
558
|
-
}
|
559
|
-
entries() {
|
560
|
-
return this.map.entries();
|
561
|
-
}
|
562
|
-
toArray() {
|
563
|
-
const entries = Array.from(this.map.entries());
|
564
|
-
return entries.map(([key, value]) => [keyToCoord(key), value]);
|
565
|
-
}
|
566
|
-
values() {
|
567
|
-
return this.map.values();
|
568
|
-
}
|
569
|
-
delete(coord) {
|
570
|
-
return this.map.delete(coordToKey(coord));
|
571
|
-
}
|
572
|
-
has(coord) {
|
573
|
-
return this.map.has(coordToKey(coord));
|
574
|
-
}
|
575
|
-
clear() {
|
576
|
-
for (const key of this.map.keys()) {
|
577
|
-
this.map.delete(key);
|
578
|
-
}
|
579
|
-
}
|
580
|
-
get size() {
|
581
|
-
return this.map.size;
|
582
|
-
}
|
583
|
-
};
|
584
|
-
|
585
|
-
// src/VoxelCoordMap.ts
|
586
|
-
function coordToKey2(coord) {
|
587
|
-
return `${coord.x}/${coord.y}/${coord.z}`;
|
588
|
-
}
|
589
|
-
function keyToCoord2(key) {
|
590
|
-
const fragments = key.split("/");
|
591
|
-
return { x: Number(fragments[0]), y: Number(fragments[1]), z: Number(fragments[2]) };
|
592
|
-
}
|
593
|
-
var VoxelCoordMap = class _VoxelCoordMap {
|
594
|
-
constructor(props) {
|
595
|
-
this.map = /* @__PURE__ */ new Map();
|
596
|
-
this.defaultValue = props?.defaultValue;
|
597
|
-
}
|
598
|
-
static from(coordMapLike) {
|
599
|
-
const coordMap = new _VoxelCoordMap();
|
600
|
-
coordMap.map = coordMapLike.map;
|
601
|
-
coordMap.defaultValue = coordMapLike.defaultValue;
|
602
|
-
return coordMap;
|
603
|
-
}
|
604
|
-
set(coord, value) {
|
605
|
-
return this.map.set(coordToKey2(coord), value);
|
606
|
-
}
|
607
|
-
get(coord) {
|
608
|
-
return this.map.get(coordToKey2(coord)) ?? this.defaultValue;
|
609
|
-
}
|
610
|
-
keys() {
|
611
|
-
return this.map.keys();
|
612
|
-
}
|
613
|
-
coords() {
|
614
|
-
return transformIterator(this.map.keys(), (key) => keyToCoord2(key));
|
615
|
-
}
|
616
|
-
entries() {
|
617
|
-
return this.map.entries();
|
618
|
-
}
|
619
|
-
toArray() {
|
620
|
-
const entries = Array.from(this.map.entries());
|
621
|
-
return entries.map(([key, value]) => [keyToCoord2(key), value]);
|
622
|
-
}
|
623
|
-
values() {
|
624
|
-
return this.map.values();
|
625
|
-
}
|
626
|
-
delete(coord) {
|
627
|
-
return this.map.delete(coordToKey2(coord));
|
628
|
-
}
|
629
|
-
has(coord) {
|
630
|
-
return this.map.has(coordToKey2(coord));
|
631
|
-
}
|
632
|
-
clear() {
|
633
|
-
for (const key of this.map.keys()) {
|
634
|
-
this.map.delete(key);
|
635
|
-
}
|
636
|
-
}
|
637
|
-
get size() {
|
638
|
-
return this.map.size;
|
639
|
-
}
|
640
|
-
};
|
641
|
-
|
642
|
-
// src/eth.ts
|
643
|
-
function padToBitLength(input, bits) {
|
644
|
-
if (input.substring(0, 2) == "0x") input = input.substring(2);
|
645
|
-
const length = bits / 4;
|
646
|
-
input = input.padStart(length, "0");
|
647
|
-
input = input.substring(input.length - length);
|
648
|
-
return `0x${input}`;
|
649
|
-
}
|
650
|
-
function toEthAddress(input) {
|
651
|
-
return padToBitLength(input, 160);
|
652
|
-
}
|
653
|
-
function to256BitString(input) {
|
654
|
-
return padToBitLength(input, 256);
|
655
|
-
}
|
656
|
-
function extractEncodedArguments(input) {
|
657
|
-
if (input[0] !== "0" && input[1] !== "x") throw new Error("Invalid hex string");
|
658
|
-
return "0x" + input.substring(10);
|
659
|
-
}
|
660
|
-
|
661
|
-
// src/cubic.ts
|
662
|
-
var RND_A = 134775813;
|
663
|
-
var RND_B = 1103515245;
|
664
|
-
var ACCURACY = 1e3;
|
665
|
-
function randomize(seed, x, y) {
|
666
|
-
return (((x ^ y) * RND_A ^ seed + x) * (RND_B * x << 16 ^ RND_B * y - RND_A) >>> 0) / 4294967295;
|
667
|
-
}
|
668
|
-
function tile(coordinate, period) {
|
669
|
-
if (coordinate < 0) while (coordinate < 0) coordinate += period;
|
670
|
-
return coordinate % period;
|
671
|
-
}
|
672
|
-
function interpolate(a, b, c, d, x, s, scale) {
|
673
|
-
const p = d - c - (a - b);
|
674
|
-
return (b * Math.pow(s, 3) + x * (c * Math.pow(s, 2) + a * s * (-s + x) + x * (-(b + p) * s + p * x))) * scale;
|
675
|
-
}
|
676
|
-
function cubicNoiseConfig(seed, octave, scale, periodX = Number.MAX_SAFE_INTEGER, periodY = Number.MAX_SAFE_INTEGER) {
|
677
|
-
return {
|
678
|
-
seed: Math.floor(seed * Number.MAX_SAFE_INTEGER),
|
679
|
-
periodX,
|
680
|
-
periodY,
|
681
|
-
octave,
|
682
|
-
scale
|
683
|
-
};
|
684
|
-
}
|
685
|
-
function cubicNoiseSample1(config, x) {
|
686
|
-
const xi = Math.floor(x);
|
687
|
-
const lerp = x - xi;
|
688
|
-
return interpolate(
|
689
|
-
randomize(config.seed, tile(xi - 1, config.periodX), 0),
|
690
|
-
randomize(config.seed, tile(xi, config.periodX), 0),
|
691
|
-
randomize(config.seed, tile(xi + 1, config.periodX), 0),
|
692
|
-
randomize(config.seed, tile(xi + 2, config.periodX), 0),
|
693
|
-
lerp,
|
694
|
-
1,
|
695
|
-
1
|
696
|
-
) * 0.666666 + 0.166666;
|
697
|
-
}
|
698
|
-
function cubicNoiseSample2({ octave, periodX, periodY, seed, scale }, x, y) {
|
699
|
-
const xi = Math.floor(x / octave);
|
700
|
-
const lerpX = Math.floor(x * ACCURACY / octave) - xi * ACCURACY;
|
701
|
-
const yi = Math.floor(y / octave);
|
702
|
-
const lerpY = Math.floor(y * ACCURACY / octave) - yi * ACCURACY;
|
703
|
-
const x0 = tile(xi - 1, periodX);
|
704
|
-
const x1 = tile(xi, periodX);
|
705
|
-
const x2 = tile(xi + 1, periodX);
|
706
|
-
const x3 = tile(xi + 2, periodX);
|
707
|
-
const xSamples = new Array(4);
|
708
|
-
for (let i = 0; i < 4; ++i) {
|
709
|
-
const y2 = tile(yi - 1 + i, periodY);
|
710
|
-
xSamples[i] = interpolate(
|
711
|
-
randomize(seed, x0, y2),
|
712
|
-
randomize(seed, x1, y2),
|
713
|
-
randomize(seed, x2, y2),
|
714
|
-
randomize(seed, x3, y2),
|
715
|
-
lerpX,
|
716
|
-
ACCURACY,
|
717
|
-
1
|
718
|
-
);
|
719
|
-
}
|
720
|
-
return Math.floor(
|
721
|
-
interpolate(xSamples[0], xSamples[1], xSamples[2], xSamples[3], lerpY, ACCURACY, scale) / Math.pow(ACCURACY, 6)
|
722
|
-
);
|
723
|
-
}
|
724
|
-
|
725
|
-
// src/console.ts
|
726
|
-
var TOPICS_KEY = "mud-logger-topics";
|
727
|
-
function enableLogger() {
|
728
|
-
const windowConsole = window.console;
|
729
|
-
let filtersActive = Boolean(localStorage.getItem(TOPICS_KEY));
|
730
|
-
const topicsString = localStorage.getItem(TOPICS_KEY);
|
731
|
-
let topics = topicsString ? JSON.parse(topicsString) : [];
|
732
|
-
function log(...logs) {
|
733
|
-
if (filtersActive) return;
|
734
|
-
windowConsole.log(...logs);
|
735
|
-
}
|
736
|
-
function logWithTopic(topic, ...logs) {
|
737
|
-
if (!filtersActive || topics.includes(topic)) {
|
738
|
-
windowConsole.log(`--- BETTER CONSOLE / TOPIC ${topic} ---`);
|
739
|
-
windowConsole.log(...logs);
|
740
|
-
}
|
741
|
-
}
|
742
|
-
function enableFilters() {
|
743
|
-
localStorage.setItem(TOPICS_KEY, JSON.stringify([]));
|
744
|
-
filtersActive = true;
|
745
|
-
}
|
746
|
-
function disableFilters() {
|
747
|
-
localStorage.removeItem(TOPICS_KEY);
|
748
|
-
filtersActive = false;
|
749
|
-
}
|
750
|
-
function addTopic(topic) {
|
751
|
-
topics.push(topic);
|
752
|
-
localStorage.setItem(TOPICS_KEY, JSON.stringify(topics));
|
753
|
-
}
|
754
|
-
function removeTopic(topic) {
|
755
|
-
topics = topics.filter((t) => t !== topic);
|
756
|
-
localStorage.setItem(TOPICS_KEY, JSON.stringify(topics));
|
757
|
-
}
|
758
|
-
function resetTopics() {
|
759
|
-
topics = [];
|
760
|
-
localStorage.setItem(TOPICS_KEY, JSON.stringify(topics));
|
761
|
-
}
|
762
|
-
const logger = {
|
763
|
-
...windowConsole,
|
764
|
-
log,
|
765
|
-
logWithTopic,
|
766
|
-
enableFilters,
|
767
|
-
disableFilters,
|
768
|
-
addTopic,
|
769
|
-
removeTopic,
|
770
|
-
resetTopics
|
771
|
-
};
|
772
|
-
window.logger = logger;
|
773
|
-
window.console = logger;
|
774
|
-
return logger;
|
775
|
-
}
|
776
|
-
|
777
|
-
// src/distance.ts
|
778
|
-
function euclidean(a, b) {
|
779
|
-
if (a.length !== b.length) throw new Error("points must have same dimension");
|
780
|
-
return Math.sqrt(a.reduce((acc, _, i) => acc + Math.pow(a[i] - b[i], 2), 0));
|
781
|
-
}
|
782
|
-
|
783
|
-
// src/math.ts
|
784
|
-
function roundTowardsZero(x) {
|
785
|
-
const sign = x < 0 ? -1 : 1;
|
786
|
-
return sign * Math.floor(Math.abs(x));
|
787
|
-
}
|
788
|
-
|
789
|
-
// src/v2/arrayToHex.ts
|
790
|
-
var arrayToHex = (array) => `0x${[...new Uint8Array(array)].map((x) => x.toString(16).padStart(2, "0")).join("")}`;
|
791
|
-
|
792
|
-
// src/v2/bytesToString.ts
|
793
|
-
var bytesToString = (bytes) => [...bytes].map((x) => String.fromCharCode(x)).join("");
|
794
|
-
|
795
|
-
// src/v2/isHex.ts
|
796
|
-
function isHex(hex) {
|
797
|
-
return /^(0x)?([\da-f]{2})*$/i.test(hex);
|
798
|
-
}
|
799
|
-
|
800
|
-
// src/v2/hexToArray.ts
|
801
|
-
var hexToArray = (hex) => {
|
802
|
-
if (!isHex(hex)) {
|
803
|
-
console.error("Invalid hex string", hex);
|
804
|
-
throw new Error("Invalid hex string");
|
805
|
-
}
|
806
|
-
const bytes = hex.match(/[\da-f]{2}/gi);
|
807
|
-
if (!bytes) return new Uint8Array([]);
|
808
|
-
return new Uint8Array(bytes.map((byte) => parseInt(byte, 16)));
|
809
|
-
};
|
810
|
-
|
811
|
-
// src/v2/stringToBytes.ts
|
812
|
-
var stringToBytes16 = (str) => {
|
813
|
-
if (str.length > 16) throw new Error("string too long");
|
814
|
-
return new Uint8Array(16).map((v, i) => str.charCodeAt(i));
|
815
|
-
};
|
816
|
-
var stringToBytes32 = (str) => {
|
817
|
-
if (str.length > 32) throw new Error("string too long");
|
818
|
-
return new Uint8Array(32).map((v, i) => str.charCodeAt(i));
|
819
|
-
};
|
820
|
-
|
821
|
-
// src/bytes.ts
|
822
|
-
function formatHex(hex) {
|
823
|
-
if (hex.substring(0, 2) == "0x") hex = hex.substring(2);
|
824
|
-
const prefix = hex.length % 2 !== 0 ? "0x0" : "0x";
|
825
|
-
return prefix + hex;
|
826
|
-
}
|
827
|
-
function hexStringToUint8Array(hexString) {
|
828
|
-
return hexToArray(hexString);
|
829
|
-
}
|
830
|
-
function Uint8ArrayToHexString(data) {
|
831
|
-
if (data.length === 0) return "0x00";
|
832
|
-
return formatHex(data.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), ""));
|
833
|
-
}
|
834
|
-
function concatUint8Arrays(...arrays) {
|
835
|
-
return Uint8Array.from(
|
836
|
-
arrays.reduce((acc, curr) => {
|
837
|
-
return [...acc, ...curr];
|
838
|
-
}, [])
|
839
|
-
);
|
840
|
-
}
|
841
|
-
function splitUint8Arrays(data, byteLengths) {
|
842
|
-
const arrays = [];
|
843
|
-
let i = 0;
|
844
|
-
for (const length of byteLengths) {
|
845
|
-
const array = new Uint8Array(length);
|
846
|
-
arrays.push(array);
|
847
|
-
for (let j = 0; j < length; j++) {
|
848
|
-
array[j] = data[i];
|
849
|
-
i++;
|
850
|
-
}
|
851
|
-
}
|
852
|
-
return arrays;
|
853
|
-
}
|
854
|
-
function Int32ArrayToUint8Array(input) {
|
855
|
-
const buffer = new ArrayBuffer(input.length * 4);
|
856
|
-
const int32arr = new Int32Array(buffer);
|
857
|
-
for (let i = 0; i < input.length; i++) {
|
858
|
-
int32arr[i] = input[i];
|
859
|
-
}
|
860
|
-
return new Uint8Array(buffer);
|
861
|
-
}
|
862
|
-
function Uint8ArrayToInt32Array(input) {
|
863
|
-
return [...new Int32Array(input.buffer)];
|
864
|
-
}
|
865
|
-
function ethAddressToUint8Array(address) {
|
866
|
-
return hexStringToUint8Array(toEthAddress(address));
|
867
|
-
}
|
868
|
-
function createToInt(size) {
|
869
|
-
if (size < 2) {
|
870
|
-
throw new Error("Minimum size is 2");
|
871
|
-
} else if (size > 64) {
|
872
|
-
throw new Error("Maximum size is 64");
|
873
|
-
}
|
874
|
-
const maxValue = 2 ** (size - 1) - 1;
|
875
|
-
const minValue = -maxValue - 1;
|
876
|
-
return (value) => {
|
877
|
-
value = value << 0;
|
878
|
-
if (value > maxValue || value < minValue) {
|
879
|
-
console.log("value", value, maxValue, minValue, value > maxValue, value < minValue);
|
880
|
-
throw new Error(`Int${size} overflow`);
|
881
|
-
}
|
882
|
-
if (value < 0) {
|
883
|
-
return 2 ** size + value;
|
884
|
-
} else {
|
885
|
-
return value;
|
886
|
-
}
|
887
|
-
};
|
888
|
-
}
|
889
|
-
var toInt32 = createToInt(32);
|
890
|
-
// Annotate the CommonJS export names for ESM import in node:
|
891
|
-
0 && (module.exports = {
|
892
|
-
CoordMap,
|
893
|
-
Int32ArrayToUint8Array,
|
894
|
-
Uint8ArrayToHexString,
|
895
|
-
Uint8ArrayToInt32Array,
|
896
|
-
VoxelCoordMap,
|
897
|
-
areaContains,
|
898
|
-
arrayToHex,
|
899
|
-
arrayToIterator,
|
900
|
-
awaitPromise,
|
901
|
-
awaitStreamValue,
|
902
|
-
awaitValue,
|
903
|
-
bytesToString,
|
904
|
-
cacheUntilReady,
|
905
|
-
callWithRetry,
|
906
|
-
computedToStream,
|
907
|
-
concatIterators,
|
908
|
-
concatUint8Arrays,
|
909
|
-
coordToKey,
|
910
|
-
coordsOf,
|
911
|
-
createToInt,
|
912
|
-
cubicNoiseConfig,
|
913
|
-
cubicNoiseSample1,
|
914
|
-
cubicNoiseSample2,
|
915
|
-
deferred,
|
916
|
-
enableLogger,
|
917
|
-
ethAddressToUint8Array,
|
918
|
-
euclidean,
|
919
|
-
extractEncodedArguments,
|
920
|
-
filterNullish,
|
921
|
-
filterNullishValues,
|
922
|
-
formatHex,
|
923
|
-
fromWorker,
|
924
|
-
hexStringToUint8Array,
|
925
|
-
hexToArray,
|
926
|
-
interpolate,
|
927
|
-
isFunction,
|
928
|
-
isHex,
|
929
|
-
isNotEmpty,
|
930
|
-
isObject,
|
931
|
-
keyToCoord,
|
932
|
-
makeIterable,
|
933
|
-
mapObject,
|
934
|
-
mergeIterators,
|
935
|
-
numValues,
|
936
|
-
observableToComputed,
|
937
|
-
observableToStream,
|
938
|
-
pack,
|
939
|
-
packTuple,
|
940
|
-
padToBitLength,
|
941
|
-
pickRandom,
|
942
|
-
random,
|
943
|
-
randomize,
|
944
|
-
range,
|
945
|
-
rejectAfter,
|
946
|
-
roundTowardsZero,
|
947
|
-
runWorker,
|
948
|
-
sleep,
|
949
|
-
splitUint8Arrays,
|
950
|
-
streamToComputed,
|
951
|
-
streamToDefinedComputed,
|
952
|
-
streamToWrappedValue,
|
953
|
-
stretch,
|
954
|
-
stringToBytes16,
|
955
|
-
stringToBytes32,
|
956
|
-
subtract,
|
957
|
-
tile,
|
958
|
-
timeoutAfter,
|
959
|
-
to256BitString,
|
960
|
-
toEthAddress,
|
961
|
-
toInt32,
|
962
|
-
transformIterator,
|
963
|
-
unpack,
|
964
|
-
unpackTuple,
|
965
|
-
uuid
|
966
|
-
});
|
967
|
-
/**
|
968
|
-
* UUID.core.js - UUID.js for Minimalists
|
969
|
-
*
|
970
|
-
* @file
|
971
|
-
* @author LiosK
|
972
|
-
* @version v4.2.0
|
973
|
-
* @license Apache License 2.0: Copyright (c) 2010-2018 LiosK
|
974
|
-
* @url https://github.com/LiosK/UUID.js/blob/master/src/uuid.core.js
|
975
|
-
*/
|
976
|
-
//# sourceMappingURL=index.cjs.map
|