-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.d.ts
448 lines (404 loc) · 11.6 KB
/
utils.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/// <reference types="typescript" />
/**
* Memoize a function
* @param {Function} f The function to memoize
* @returns {Function} A memoized version of the function
*/
declare function memoize(f: Function): Function;
/**
* Check if two numbers are approximately equal
* @param {number} a Number a
* @param {number} b Number b
* @param {number} [p=Number.EPSILON] The precision value
* @return {boolean} True if numbers a and b are approximately equal
*/
declare function floatEquals(a: number, b: number, p?: number): boolean;
/**
* Clamp a number between min and max
* @param {number} a The number to clamp
* @param {number} [min=0] The minimum value
* @param {number} [max=1] The maximum value
* @return {number} A clamped number
*/
declare function clamp(a: number, min?: number, max?: number): number;
/**
* Get the fractional part of a number
* @param {number} a The number from which to get the fractional part
* @return {number} The fractional part of the number
*/
declare function frac(a: number): number;
/**
* Round n to d decimal places
* @param {number} n The number to round
* @param {number} [d=0] The number of decimal places to round to
* @return {number} A rounded number
*/
declare function round(n: number, d?: number): number;
/**
* An interpolation function
* @callback InterpolationFunction
* @param {number} a The minimum number
* @param {number} b The maximum number
* @param {number} i The interpolation value, should be in the interval [0, 1]
* @return {number} The interpolated value in the interval [a, b]
*/
type InterpolationFunction = (a: number, b: number, i: number) => number;
/**
* Do a linear interpolation between a and b
* @param {number} a The minimum number
* @param {number} b The maximum number
* @param {number} i The interpolation value, should be in the interval [0, 1]
* @return {number} An interpolated value in the interval [a, b]
*/
declare function lerp(a: number, b: number, i: number): number;
/**
* Get the position of i between a and b
* @param {number} a The minimum number
* @param {number} b The maximum number
* @param {number} i The interpolated value in the interval [a, b]
* @return {number} The position of i between a and b
*/
declare function unlerp(a: number, b: number, i: number): number;
/**
* Do a bilinear interpolation
* @param {number} c00 Top-left value
* @param {number} c10 Top-right value
* @param {number} c01 Bottom-left value
* @param {number} c11 Bottom-right value
* @param {number} ix Interpolation value along x
* @param {number} iy Interpolation value along y
* @return {number} A bilinear interpolated value
*/
declare function blerp(c00: number, c10: number, c01: number, c11: number, ix: number, iy: number): number;
/**
* Re-map a number i from range a1...a2 to b1...b2
* @param {number} i The number to re-map
* @param {number} a1
* @param {number} a2
* @param {number} b1
* @param {number} b2
* @return {number}
*/
declare function remap(i: number, a1: number, a2: number, b1: number, b2: number): number;
/**
* Do a smooth interpolation between a and b
* @param {number} a The minimum number
* @param {number} b The maximum number
* @param {number} i The interpolation value
* @return {number} An interpolated value in the interval [a, b]
*/
declare function smoothstep(a: number, b: number, i: number): number;
/**
* Get an angle in radians
* @param {number} degrees The angle in degrees
* @return {number} The angle in radians
*/
declare function radians(degrees: number): number;
/**
* Get an angle in degrees
* @param {number} radians The angle in radians
* @return {number} The angle in degrees
*/
declare function degrees(radians: number): number;
/**
* Get a random float in the interval [min, max)
* @param {number} min Inclusive min
* @param {number} max Exclusive max
* @return {number} A random float in the interval [min, max)
*/
declare function randomBetween(min: number, max: number): number;
/**
* Get a random integer in the interval [min, max]
* @param {number} min Inclusive min
* @param {number} max Inclusive max
* @return {number} A random integer in the interval [min, max]
*/
declare function randomIntBetween(min: number, max: number): number;
/**
* Get a normally-distributed random number
* @param {number} [mu=0.5] The mean value
* @param {number} [sigma=0.5] The standard deviation
* @param {number} [samples=2] The number of samples
* @return {number} A normally-distributed random number
*/
declare function cltRandom(mu?: number, sigma?: number, samples?: number): number;
/**
* Get a normally-distributed random integer in the interval [min, max]
* @param {number} min Inclusive min
* @param {number} max Inclusive max
* @return {number} A normally-distributed random integer
*/
declare function cltRandomInt(min: number, max: number): number;
/**
* Return a weighted random integer
* @param {Array<number>} w An array of weights
* @return {number} An index from w
*/
declare function weightedRandom(w: Array<number>): number;
/**
* Return an interpolated value from an array
* @param {Array<number>} a An array of values interpolate
* @param {number} i A number in the interval [0, 1]
* @param {InterpolationFunction} [f=Math.lerp] The interpolation function to use
* @return {number} An interpolated value in the interval [min(a), max(a)]
*/
declare function lerpArray(a: Array<number>, i: number, f?: InterpolationFunction): number;
/**
* Get the dot product of two vectors
* @param {Array<number>} a Vector a
* @param {Array<number>} b Vector b
* @return {number} a ∙ b
*/
declare function dot(a: Array<number>, b: Array<number>): number;
/**
* Get the factorial of a number
* @param {number} a
* @return {number} a!
*/
declare function factorial(a: number): number;
/**
* Get the number of permutations of r elements from a set of n elements
* @param {number} n
* @param {number} r
* @return {number} nPr
*/
declare function npr(n: number, r: number): number;
/**
* Get the number of combinations of r elements from a set of n elements
* @param {number} n
* @param {number} r
* @return {number} nCr
*/
declare function ncr(n: number, r: number): number;
/**
* Generate all permutations of r elements from an array
*
* @example
* ```js
* permutations([1, 2, 3], 2);
* ```
*
* Output:
* ```json
* [
* [1, 2],
* [1, 3],
* [2, 1],
* [2, 3],
* [3, 1],
* [3, 2]
* ]
* ```
* @param {T[]} a
* @param {number} r The number of elements to choose in each permutation
* @return {T[][]} An array of permutation arrays
*/
declare function permutations<T>(a: T[], r: number): T[][];
/**
* Generate all combinations of r elements from an array
*
* @example
* ```js
* combinations([1, 2, 3], 2);
* ```
*
* Output:
* ```json
* [
* [1, 2],
* [1, 3],
* [2, 3]
* ]
* ```
* @param {T[]} a
* @param {number} r The number of elements to choose in each combination
* @return {T[][]} An array of combination arrays
*/
declare function combinations<T>(a: T[], r: number): T[][];
/**
* A cartesian product of arrays
* @template T
*/
type MapCartesian<T extends any[][]> = {
[P in keyof T]: T[P] extends Array<infer U> ? U : never;
};
/**
* Get a cartesian product of arrays
*
* @example
* ```ts
* cartesian([1, 2, 3], ['a', 'b']);
* ```
*
* Output:
* ```json
* [
* [1, "a"],
* [1, "b"],
* [2, "a"],
* [2, "b"],
* [3, "a"],
* [3, "b"]
* ]
* ```
*/
declare function cartesian<T extends any[][]>(...arr: T): MapCartesian<T>[];
/**
* A function for generating array values
* @callback TimesFunction
* @param {number} i The array index
* @return {*} The array value
*/
type TimesFunction = (i: number) => any;
/**
* Return a new array with length n by calling function f(i) on each element
* @param {TimesFunction} f
* @param {number} n The size of the array
* @return {Array<*>}
*/
declare function times(f: TimesFunction, n: number): Array<any>;
/**
* Return an array containing numbers 0->(n - 1)
* @param {number} n The size of the array
* @return {Array<number>} An array of integers 0->(n - 1)
*/
declare function range(n: number): Array<number>;
/**
* Zip 2 arrays together, i.e. ([1, 2, 3], [a, b, c]) => [[1, a], [2, b], [3, c]]
* @param {Array<*>} a
* @param {Array<*>} b
* @return {Array<*>}
*/
declare function zip(a: Array<any>, b: Array<any>): Array<Array<any>>;
/**
* Return array[i] with positive and negative wrapping
* @param {Array<*>} a
* @param {number} i The positively/negatively wrapped array index
* @return {*} An element from the array
*/
declare function at(a: Array<any>, i: number): any;
/**
* Return the last element of an array without removing it
* @param {T[]} a
* @return {T} The last element from the array
*/
declare function peek<T = any>(a: T[]): T | undefined;
/**
* Return the index for a given position in an unrolled 2d array
* @param {number} x The x position
* @param {number} y The y position
* @param {number} w The width of the 2d array
* @returns {number} The index in the unrolled array
*/
declare function ind(x: number, y: number, w: number): number;
/**
* Return the position for a given index in an unrolled 2d array
* @param {number} i The index
* @param {number} w The width of the 2d array
* @returns {Array<number>} The position as a 2-tuple
*/
declare function pos(i: number, w: number): [number, number];
/**
* Chop an array into chunks of size n
* @param {Array<*>} a
* @param {number} n The chunk size
* @return {Array<Array<*>>} An array of array chunks
*/
declare function chunk(a: Array<any>, n: number): Array<Array<any>>;
/**
* Randomly shuffle an array in-place
* @param {Array<*>} a
* @return {Array<*>} The shuffled array
*/
declare function shuffle(a: Array<any>): Array<any>;
/**
* Flatten an object
* @param {object} o
* @param {string} concatenator The string to use for concatenating keys
* @return {object} A flattened object
*/
declare function flat(o: object, concatenator?: string): object;
/**
* Unflatten an object
* @param {object} o
* @param {string} concatenator The string to check for in concatenated keys
* @return {object} An un-flattened object
*/
declare function unflat(o: object, concatenator?: string): object;
/**
* A split predicate
* @callback SplitPredicate
* @param {any} value The current value
* @return {boolean} True if the array should split at this index
*/
type SplitPredicate = (a: number, b: number, i: number) => number;
/**
* Split an array into sub-arrays based on a predicate
* @param {T[]} a
* @param {SplitPredicate} predicate
* @return {T[][]} An array of arrays
*/
declare function split<T>(a: T[], predicate: SplitPredicate): T[][];
/**
* Pluck keys from an object
* @param {object} o
* @param {...string} keys The keys to pluck from the object
* @return {object} An object containing the plucked keys
*/
declare function pluck<T extends object, K extends keyof T>(
o: T,
...keys: K[]
): Pick<T, K>;
/**
* Exclude keys from an object
* @param {object} o
* @param {...string} keys The keys to exclude from the object
* @return {object} An object containing all keys except excluded keys
*/
declare function exclude<T extends object, K extends [...(keyof T)[]]>(
o: T,
...keys: K
): {
[K2 in Exclude<keyof T, K[number]>]: T[K2];
};
export {
memoize,
floatEquals,
clamp,
frac,
round,
lerp,
unlerp,
blerp,
remap,
smoothstep,
radians,
degrees,
randomBetween,
randomIntBetween,
cltRandom,
cltRandomInt,
weightedRandom,
lerpArray,
dot,
factorial,
npr,
ncr,
permutations,
combinations,
cartesian,
times,
range,
zip,
at,
peek,
ind,
pos,
chunk,
shuffle,
flat,
unflat,
split,
pluck,
exclude,
};