toosoon-utils 1.5.0 → 2.1.0
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/package.json +9 -6
- package/tsconfig.json +1 -3
- package/lib/classes/_pool.d.ts +0 -56
- package/lib/classes/_pool.js +0 -92
- package/lib/classes/color-scale.d.ts +0 -52
- package/lib/classes/color-scale.js +0 -160
- package/lib/classes/frame-rate.d.ts +0 -25
- package/lib/classes/frame-rate.js +0 -48
- package/lib/colors.d.ts +0 -155
- package/lib/colors.js +0 -367
- package/lib/constants.d.ts +0 -162
- package/lib/constants.js +0 -170
- package/lib/dom.d.ts +0 -25
- package/lib/dom.js +0 -47
- package/lib/files.d.ts +0 -14
- package/lib/files.js +0 -38
- package/lib/functions.d.ts +0 -22
- package/lib/functions.js +0 -53
- package/lib/geometry.d.ts +0 -89
- package/lib/geometry.js +0 -128
- package/lib/index.d.ts +0 -10
- package/lib/index.js +0 -39
- package/lib/maths.d.ts +0 -161
- package/lib/maths.js +0 -219
- package/lib/now.d.ts +0 -5
- package/lib/now.js +0 -28
- package/lib/prng.d.ts +0 -124
- package/lib/prng.js +0 -234
- package/lib/random.d.ts +0 -91
- package/lib/random.js +0 -162
- package/lib/strings.d.ts +0 -14
- package/lib/strings.js +0 -18
- package/lib/tsconfig.tsbuildinfo +0 -1
- package/lib/types.d.ts +0 -18
- package/lib/types.js +0 -1
- package/src/classes/_pool.ts +0 -92
- package/src/classes/color-scale.ts +0 -181
- package/src/classes/frame-rate.ts +0 -49
- package/src/colors.ts +0 -389
- package/src/constants.ts +0 -172
- package/src/dom.ts +0 -50
- package/src/files.ts +0 -42
- package/src/functions.ts +0 -56
- package/src/geometry.ts +0 -160
- package/src/maths.ts +0 -241
- package/src/prng.ts +0 -249
- package/src/random.ts +0 -162
- package/src/strings.ts +0 -19
- package/src/types.ts +0 -33
package/src/geometry.ts
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import { PI } from './constants';
|
|
2
|
-
import { Vector3 } from './types';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Convert a radians value into degrees
|
|
6
|
-
*
|
|
7
|
-
* @param {number} radians Angle in radians
|
|
8
|
-
* @returns {number} Angle in degrees
|
|
9
|
-
*/
|
|
10
|
-
export function toDegrees(radians: number): number {
|
|
11
|
-
return (radians * 180) / PI;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Convert a degrees value into radians
|
|
16
|
-
*
|
|
17
|
-
* @param {number} degrees Angle in degrees
|
|
18
|
-
* @returns {number} Angle in radians
|
|
19
|
-
*/
|
|
20
|
-
export function toRadians(degrees: number): number {
|
|
21
|
-
return (degrees * PI) / 180;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Calculate the angle from a point to another
|
|
26
|
-
*
|
|
27
|
-
* @param {number} x1 X value of the first point
|
|
28
|
-
* @param {number} y1 Y value of the first point
|
|
29
|
-
* @param {number} x2 X value of the second point
|
|
30
|
-
* @param {number} y2 Y value of the second point
|
|
31
|
-
* @returns {number} Angle
|
|
32
|
-
*/
|
|
33
|
-
export function angle(x1: number, y1: number, x2: number, y2: number): number {
|
|
34
|
-
return Math.atan2(y2 - y1, x2 - x1);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Find the closest angle between to angles
|
|
39
|
-
*
|
|
40
|
-
* @param {number} source Source angle in radians
|
|
41
|
-
* @param {number} target Target angle in radians
|
|
42
|
-
* @returns {number} Closest angle
|
|
43
|
-
*/
|
|
44
|
-
export function closestAngle(source: number, target: number): number {
|
|
45
|
-
const delta = target - source;
|
|
46
|
-
return delta > PI ? target - 2 * PI : target < -PI ? delta + 2 * PI : target;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Calculate the distance between two points
|
|
51
|
-
*
|
|
52
|
-
* @param {number} x1 X coord of the first point
|
|
53
|
-
* @param {number} y1 Y coord of the first point
|
|
54
|
-
* @param {number} x2 X coord of the second point
|
|
55
|
-
* @param {number} y2 Y coord of the second point
|
|
56
|
-
* @returns {number} Computed distance
|
|
57
|
-
*/
|
|
58
|
-
export function distance(x1: number, y1: number, x2: number, y2: number): number {
|
|
59
|
-
const dx = x1 - x2;
|
|
60
|
-
const dy = y1 - y2;
|
|
61
|
-
return Math.sqrt(dx * dx + dy * dy);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Calculate the length of the diagonal of a rectangle
|
|
66
|
-
*
|
|
67
|
-
* @param {number} width Width of the rectangle
|
|
68
|
-
* @param {number} height Height of the rectangle
|
|
69
|
-
* @returns {number} Diagonal length
|
|
70
|
-
*/
|
|
71
|
-
export function diagonal(width: number, height: number): number {
|
|
72
|
-
return Math.sqrt(width * width + height * height);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Convert radians to a 3D point on the surface of a unit sphere
|
|
77
|
-
*
|
|
78
|
-
* @param {number} radius Radius of the sphere
|
|
79
|
-
* @param {number} phi Polar angle from the y (up) axis : [0, PI]
|
|
80
|
-
* @param {number} theta Equator angle around the y (up) axis : [0, 2*PI]
|
|
81
|
-
* @param {Vector3} target Target vector
|
|
82
|
-
* @returns {Vector3}
|
|
83
|
-
*/
|
|
84
|
-
export function radToSphere(
|
|
85
|
-
radius: number,
|
|
86
|
-
phi: number,
|
|
87
|
-
theta: number,
|
|
88
|
-
target: Vector3 = { x: 0, y: 0, z: 0 }
|
|
89
|
-
): Vector3 {
|
|
90
|
-
target.x = radius * Math.sin(phi) * Math.sin(theta);
|
|
91
|
-
target.y = radius * Math.cos(phi);
|
|
92
|
-
target.z = radius * Math.sin(phi) * Math.cos(theta);
|
|
93
|
-
return target;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// *********************
|
|
97
|
-
// Fit
|
|
98
|
-
// *********************
|
|
99
|
-
interface FitInput {
|
|
100
|
-
width: number;
|
|
101
|
-
height: number;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
interface FitOutput {
|
|
105
|
-
left: number;
|
|
106
|
-
top: number;
|
|
107
|
-
width: number;
|
|
108
|
-
height: number;
|
|
109
|
-
scale: number;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Make a target fit a container
|
|
114
|
-
*
|
|
115
|
-
* @param {FitInput} target Dimension of the target
|
|
116
|
-
* @param {FitInput} container Dimension of the container
|
|
117
|
-
* @param {string} mode Can be 'contain' | 'cover'
|
|
118
|
-
* @returns {FitOutput}
|
|
119
|
-
*/
|
|
120
|
-
function fit(target: FitInput, container: FitInput, mode: 'contain' | 'cover'): FitOutput {
|
|
121
|
-
const ratioWidth = container.width / target.width;
|
|
122
|
-
const ratioHeight = container.height / target.height;
|
|
123
|
-
|
|
124
|
-
let scale: number;
|
|
125
|
-
if (mode === 'contain') {
|
|
126
|
-
scale = ratioWidth < ratioHeight ? ratioWidth : ratioHeight;
|
|
127
|
-
} else {
|
|
128
|
-
scale = ratioWidth > ratioHeight ? ratioWidth : ratioHeight;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return {
|
|
132
|
-
left: (container.width - target.width * scale) >> 1,
|
|
133
|
-
top: (container.height - target.height * scale) >> 1,
|
|
134
|
-
width: target.width * scale,
|
|
135
|
-
height: target.height * scale,
|
|
136
|
-
scale
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Make a target fit a container (cover mode)
|
|
142
|
-
*
|
|
143
|
-
* @param {FitInput} target Dimension of the target
|
|
144
|
-
* @param {FitInput} container Dimension of the container
|
|
145
|
-
* @returns {FitOutput}
|
|
146
|
-
*/
|
|
147
|
-
export function cover(target: FitInput, container: FitInput): FitOutput {
|
|
148
|
-
return fit(target, container, 'cover');
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Make a target fit a container (contain mode)
|
|
153
|
-
*
|
|
154
|
-
* @param {FitInput} target Dimension of the target
|
|
155
|
-
* @param {FitInput} container Dimension of the container
|
|
156
|
-
* @returns {FitOutput}
|
|
157
|
-
*/
|
|
158
|
-
export function contain(target: FitInput, container: FitInput): FitOutput {
|
|
159
|
-
return fit(target, container, 'contain');
|
|
160
|
-
}
|
package/src/maths.ts
DELETED
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Check if a number is even
|
|
3
|
-
*
|
|
4
|
-
* @param {number} value Value to check
|
|
5
|
-
* @returns {boolean} True if the given number is even, false otherwise
|
|
6
|
-
*/
|
|
7
|
-
export function isEven(value: number): boolean {
|
|
8
|
-
return !(value & 1);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Check if a number is odd
|
|
13
|
-
*
|
|
14
|
-
* @param {number} value Value to check
|
|
15
|
-
* @returns {boolean} True if the given number is odd, false otherwise
|
|
16
|
-
*/
|
|
17
|
-
export function isOdd(value: number): boolean {
|
|
18
|
-
return !!(value & 1);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Check if a number is a power of 2
|
|
23
|
-
*
|
|
24
|
-
* @param {number} value Value to check
|
|
25
|
-
* @returns {boolean} True if the given number is a power of 2, false otherwise
|
|
26
|
-
*/
|
|
27
|
-
export function isPowerOf2(value: number): boolean {
|
|
28
|
-
return (value & (value - 1)) === 0;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Find closest power of 2 that fits a number
|
|
33
|
-
*
|
|
34
|
-
* @param {number} value Incoming value
|
|
35
|
-
* @param {string} [mode='ceil'] Can be 'floor' | 'ceil' | 'round'
|
|
36
|
-
* @returns {number} Power of 2
|
|
37
|
-
*/
|
|
38
|
-
export function toPowerOf2(value: number, mode: 'floor' | 'ceil' | 'round' = 'ceil'): number {
|
|
39
|
-
return Math.pow(2, Math[mode](Math.log(value) / Math.log(2)));
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Return the sign (positive or negative) of a number
|
|
44
|
-
*
|
|
45
|
-
* @param {number} value Value to check
|
|
46
|
-
* @returns {number} 1 if the given number is positive, -1 if it is negative, otherwise 0
|
|
47
|
-
*/
|
|
48
|
-
export function sign(value: number): number {
|
|
49
|
-
if (value > 0) return 1;
|
|
50
|
-
else if (value < 0) return -1;
|
|
51
|
-
return 0;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Clamp a value between two bounds
|
|
56
|
-
*
|
|
57
|
-
* @param {number} value Value to clamp
|
|
58
|
-
* @param {number} [min=0] Minimum boundary
|
|
59
|
-
* @param {number} [max=1] Maximum boundary
|
|
60
|
-
* @returns {number} Clamped value
|
|
61
|
-
*/
|
|
62
|
-
export function clamp(value: number, min: number = 0, max: number = 1): number {
|
|
63
|
-
return Math.min(max, Math.max(min, value));
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Linear interpolation between two values (lerping)
|
|
68
|
-
*
|
|
69
|
-
* @param {number} value Normalized value to interpolate
|
|
70
|
-
* @param {number} min Minimum value
|
|
71
|
-
* @param {number} max Maximum value
|
|
72
|
-
* @returns {number} Lerped value
|
|
73
|
-
*/
|
|
74
|
-
export function lerp(value: number, min: number, max: number): number {
|
|
75
|
-
return min + (max - min) * value;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Triangular interpolation between two values
|
|
80
|
-
*
|
|
81
|
-
* @param {number} value Normalized value to interpolate
|
|
82
|
-
* @param {number} min Minimum value
|
|
83
|
-
* @param {number} max Maximum value
|
|
84
|
-
* @param {number} target Triangle target value
|
|
85
|
-
* @returns {number} Interpolated value
|
|
86
|
-
*/
|
|
87
|
-
export function triLerp(value: number, min: number, max: number, target: number): number {
|
|
88
|
-
const x = Math.pow(1 + Math.abs(target - max) / Math.abs(target - min), -1);
|
|
89
|
-
return value <= x ? min - (min - target) * (value / x) : target - (target - max) * ((value - x) / (1 - x));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Exponential interpolation between two values
|
|
94
|
-
*
|
|
95
|
-
* @param {number} value Value to interpolate
|
|
96
|
-
* @param {number} currentMin Lower bound of the value's current range
|
|
97
|
-
* @param {number} currentMax Upper bound of the value's current range
|
|
98
|
-
* @param {number} targetMin Lower bound of the value's target range
|
|
99
|
-
* @param {number} targetMax Upper bound of the value's target range
|
|
100
|
-
* @returns {number} Interpolated value
|
|
101
|
-
*/
|
|
102
|
-
export function expLerp(
|
|
103
|
-
value: number,
|
|
104
|
-
currentMin: number,
|
|
105
|
-
currentMax: number,
|
|
106
|
-
targetMin: number,
|
|
107
|
-
targetMax: number
|
|
108
|
-
): number {
|
|
109
|
-
return (
|
|
110
|
-
targetMin *
|
|
111
|
-
Math.pow(targetMax / targetMin, (clamp(value, currentMin, currentMax) - currentMin) / (currentMax - currentMin))
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Normalize a value between two bounds
|
|
117
|
-
*
|
|
118
|
-
* @param {number} value Value to normalize
|
|
119
|
-
* @param {number} min Minimum boundary
|
|
120
|
-
* @param {number} max Maximum boundary
|
|
121
|
-
* @returns {number} Normalized value
|
|
122
|
-
*/
|
|
123
|
-
export function normalize(value: number, min: number, max: number): number {
|
|
124
|
-
return (value - min) / (max - min);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Re-map a number from one range to another
|
|
129
|
-
*
|
|
130
|
-
* @param {number} value Value to re-map
|
|
131
|
-
* @param {number} currentMin Lower bound of the value's current range
|
|
132
|
-
* @param {number} currentMax Upper bound of the value's current range
|
|
133
|
-
* @param {number} targetMin Lower bound of the value's target range
|
|
134
|
-
* @param {number} targetMax Upper bound of the value's target range
|
|
135
|
-
* @returns {number} Re-mapped value
|
|
136
|
-
*/
|
|
137
|
-
export function map(
|
|
138
|
-
value: number,
|
|
139
|
-
currentMin: number,
|
|
140
|
-
currentMax: number,
|
|
141
|
-
targetMin: number,
|
|
142
|
-
targetMax: number
|
|
143
|
-
): number {
|
|
144
|
-
return ((value - currentMin) / (currentMax - currentMin)) * (targetMax - targetMin) + targetMin;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Round a number up to a nearest multiple
|
|
149
|
-
*
|
|
150
|
-
* @param {number} value Value to round
|
|
151
|
-
* @param {number} [multiple=1] Multiple to round to
|
|
152
|
-
* @returns {number} Closest multiple
|
|
153
|
-
*/
|
|
154
|
-
export function roundTo(value: number, multiple: number = 1): number {
|
|
155
|
-
if (multiple === 0) return value;
|
|
156
|
-
return Math.round(value / multiple) * multiple;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Modulo absolute a value based on a length
|
|
161
|
-
*
|
|
162
|
-
* @param {number} value Value to modulate
|
|
163
|
-
* @param {number} length Total length
|
|
164
|
-
* @returns {number} Modulated value
|
|
165
|
-
*/
|
|
166
|
-
export function modAbs(value: number, length: number): number {
|
|
167
|
-
if (value < 0) {
|
|
168
|
-
return length + (value % length);
|
|
169
|
-
}
|
|
170
|
-
return value % length;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Move back and forth a value between 0 and length, so that it is never larger than length and never smaller than 0
|
|
175
|
-
*
|
|
176
|
-
* @param {number} value Value to modulate
|
|
177
|
-
* @param {number} length Total length
|
|
178
|
-
* @returns {number} PingPonged value
|
|
179
|
-
*/
|
|
180
|
-
export function pingPong(value: number, length: number): number {
|
|
181
|
-
value = modAbs(value, length * 2);
|
|
182
|
-
return length - Math.abs(value - length);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Smooth a value using cubic Hermite interpolation
|
|
187
|
-
*
|
|
188
|
-
* @param {number} value Value to smooth
|
|
189
|
-
* @param {number} [min=0] Minimum boundary
|
|
190
|
-
* @param {number} [max=1] Maximum boundary
|
|
191
|
-
* @returns {number} Normalized smoothed value
|
|
192
|
-
*/
|
|
193
|
-
export function smoothstep(value: number, min: number = 0, max: number = 1): number {
|
|
194
|
-
const x = clamp(normalize(value, min, max));
|
|
195
|
-
return x * x * (3 - 2 * x);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Re-map the [0, 1] interval into [0, 1] parabola, such that corners are remaped to 0 and the center to 1
|
|
200
|
-
* -> parabola(0) = parabola(1) = 0, and parabola(0.5) = 1
|
|
201
|
-
*
|
|
202
|
-
* @param {number} x Normalized coordinate on X axis
|
|
203
|
-
* @param {number} [power=1] Parabola power
|
|
204
|
-
* @returns {number} Normalized re-mapped value
|
|
205
|
-
*/
|
|
206
|
-
export function parabola(x: number, power: number = 1): number {
|
|
207
|
-
return Math.pow(4 * x * (1 - x), power);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Return the sum of numbers
|
|
212
|
-
*
|
|
213
|
-
* @param {number[]} array Array of numbers
|
|
214
|
-
* @returns {number} Total sum
|
|
215
|
-
*/
|
|
216
|
-
export function sum(array: number[]): number {
|
|
217
|
-
return array.reduce((previous, current) => previous + current);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Return the average of numbers
|
|
222
|
-
*
|
|
223
|
-
* @param {number[]} array Array of numbers
|
|
224
|
-
* @returns {number} Total average
|
|
225
|
-
*/
|
|
226
|
-
export function average(array: number[]): number {
|
|
227
|
-
return sum(array) / array.length;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Smoothly interpolate a number toward another
|
|
232
|
-
*
|
|
233
|
-
* @param {number} value Value to interpolate
|
|
234
|
-
* @param {number} target Destination of the interpolation
|
|
235
|
-
* @param {number} damping A higher value will make the movement more sudden, and a lower value will make the movement more gradual
|
|
236
|
-
* @param {number} delta Delta time (in seconds)
|
|
237
|
-
* @returns {number} Interpolated number
|
|
238
|
-
*/
|
|
239
|
-
export function damp(value: number, target: number, damping: number, delta: number): number {
|
|
240
|
-
return lerp(1 - Math.exp(-damping * delta), value, target);
|
|
241
|
-
}
|
package/src/prng.ts
DELETED
|
@@ -1,249 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Produce a 128-bit hash value from a seed
|
|
3
|
-
*
|
|
4
|
-
* @param {string} seed Initial seed state
|
|
5
|
-
* @returns {[number, number, number, number]} Hash numbers
|
|
6
|
-
*/
|
|
7
|
-
export function cyrb128(seed: string): [number, number, number, number] {
|
|
8
|
-
let h1 = 1779033703;
|
|
9
|
-
let h2 = 3144134277;
|
|
10
|
-
let h3 = 1013904242;
|
|
11
|
-
let h4 = 2773480762;
|
|
12
|
-
for (let i = 0, k; i < seed.length; i++) {
|
|
13
|
-
k = seed.charCodeAt(i);
|
|
14
|
-
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
|
|
15
|
-
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
|
|
16
|
-
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
|
|
17
|
-
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
|
|
18
|
-
}
|
|
19
|
-
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
|
|
20
|
-
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
|
|
21
|
-
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
|
|
22
|
-
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
|
|
23
|
-
return [(h1 ^ h2 ^ h3 ^ h4) >>> 0, (h2 ^ h1) >>> 0, (h3 ^ h1) >>> 0, (h4 ^ h1) >>> 0];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// *********************
|
|
27
|
-
// PRNG Algorithms
|
|
28
|
-
// *********************
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Simple Fast Counter, Generator with a 128-bit state
|
|
32
|
-
*
|
|
33
|
-
* @param {number} a
|
|
34
|
-
* @param {number} b
|
|
35
|
-
* @param {number} c
|
|
36
|
-
* @param {number} d
|
|
37
|
-
* @returns {number} Pseudo-random number
|
|
38
|
-
*/
|
|
39
|
-
export function sfc32(a: number, b: number, c: number, d: number): number {
|
|
40
|
-
a >>>= 0;
|
|
41
|
-
b >>>= 0;
|
|
42
|
-
c >>>= 0;
|
|
43
|
-
d >>>= 0;
|
|
44
|
-
let t = (a + b) | 0;
|
|
45
|
-
a = b ^ (b >>> 9);
|
|
46
|
-
b = (c + (c << 3)) | 0;
|
|
47
|
-
c = (c << 21) | (c >>> 11);
|
|
48
|
-
d = (d + 1) | 0;
|
|
49
|
-
t = (t + d) | 0;
|
|
50
|
-
c = (c + t) | 0;
|
|
51
|
-
return (t >>> 0) / 4294967296;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* SplitMix32, Generator with a 32-bit state
|
|
56
|
-
*
|
|
57
|
-
* @param {number} a
|
|
58
|
-
* @returns {number} Pseudo-random number
|
|
59
|
-
*/
|
|
60
|
-
export function splitmix32(a: number): number {
|
|
61
|
-
a |= 0;
|
|
62
|
-
a = (a + 0x9e3779b9) | 0;
|
|
63
|
-
var t = a ^ (a >>> 16);
|
|
64
|
-
t = Math.imul(t, 0x21f0aaad);
|
|
65
|
-
t = t ^ (t >>> 15);
|
|
66
|
-
t = Math.imul(t, 0x735a2d97);
|
|
67
|
-
return ((t = t ^ (t >>> 15)) >>> 0) / 4294967296;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Mulberry32, Generator with a 32-bit state
|
|
72
|
-
*
|
|
73
|
-
* @param {number} a
|
|
74
|
-
* @returns {number} Pseudo-random number
|
|
75
|
-
*/
|
|
76
|
-
export function mulberry32(a: number): number {
|
|
77
|
-
let t = (a += 0x6d2b79f5);
|
|
78
|
-
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
79
|
-
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
80
|
-
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Jenkins' Small Fast, Generator with a 32-bit state
|
|
85
|
-
*
|
|
86
|
-
* @param {number} a
|
|
87
|
-
* @returns {number} Pseudo-random number
|
|
88
|
-
*/
|
|
89
|
-
export function jsf32(a: number, b: number, c: number, d: number): number {
|
|
90
|
-
a |= 0;
|
|
91
|
-
b |= 0;
|
|
92
|
-
c |= 0;
|
|
93
|
-
d |= 0;
|
|
94
|
-
let t = (a - ((b << 27) | (b >>> 5))) | 0;
|
|
95
|
-
a = b ^ ((c << 17) | (c >>> 15));
|
|
96
|
-
b = (c + d) | 0;
|
|
97
|
-
c = (d + t) | 0;
|
|
98
|
-
d = (a + t) | 0;
|
|
99
|
-
return (d >>> 0) / 4294967296;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* xoshiro128**, Generator with a 128-bit state
|
|
104
|
-
*
|
|
105
|
-
* @param {number} a
|
|
106
|
-
* @returns {number} Pseudo-random number
|
|
107
|
-
*/
|
|
108
|
-
export function xoshiro128ss(a: number, b: number, c: number, d: number): number {
|
|
109
|
-
let t = b << 9;
|
|
110
|
-
let r = a * 5;
|
|
111
|
-
r = ((r << 7) | (r >>> 25)) * 9;
|
|
112
|
-
c ^= a;
|
|
113
|
-
d ^= b;
|
|
114
|
-
b ^= c;
|
|
115
|
-
a ^= d;
|
|
116
|
-
c ^= t;
|
|
117
|
-
d = (d << 11) | (d >>> 21);
|
|
118
|
-
return (r >>> 0) / 4294967296;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// *********************
|
|
122
|
-
// PRNG Functions
|
|
123
|
-
// *********************
|
|
124
|
-
export type PRNGParameters = string | { seed: string; algorithm: (...args: number[]) => number };
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Generate a pseudo-random number in the interval [0, 1]
|
|
128
|
-
* PRNG equivalent of `Math.random()`
|
|
129
|
-
*
|
|
130
|
-
* @param {PRNGParameters} prng PRNG parameters
|
|
131
|
-
* @returns {number} Pseudo-random number
|
|
132
|
-
*/
|
|
133
|
-
export function random(prng: PRNGParameters): number {
|
|
134
|
-
const seed = typeof prng === 'string' ? prng : prng.seed;
|
|
135
|
-
const algorithm = typeof prng === 'string' ? splitmix32 : prng.algorithm;
|
|
136
|
-
|
|
137
|
-
const hashes = cyrb128(seed);
|
|
138
|
-
return algorithm(...hashes);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Generate a pseudo-random boolean (true or false)
|
|
143
|
-
*
|
|
144
|
-
* @param {PRNGParameters} prng PRNG parameters
|
|
145
|
-
* @param {number} [probability=0.5] Probability to get true
|
|
146
|
-
* @returns {boolean} Either `true` or `false`
|
|
147
|
-
*/
|
|
148
|
-
export function randomBoolean(prng: PRNGParameters, probability: number = 0.5): boolean {
|
|
149
|
-
return random(prng) < probability;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Generate a pseudo-random sign (1 or -1)
|
|
154
|
-
*
|
|
155
|
-
* @param {PRNGParameters} prng PRNG parameters
|
|
156
|
-
* @param {number} [probability=0.5] Probability to get 1
|
|
157
|
-
* @returns {number} Either 1 or -1
|
|
158
|
-
*/
|
|
159
|
-
export function randomSign(prng: PRNGParameters, probability: number = 0.5): number {
|
|
160
|
-
return randomBoolean(prng, probability) ? 1 : -1;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Generate a pseudo-random floating-point number within a specified range
|
|
165
|
-
*
|
|
166
|
-
* @param {PRNGParameters} prng PRNG parameters
|
|
167
|
-
* @param {number} [min=0] Minimum boundary
|
|
168
|
-
* @param {number} [max=1] Maximum boundary
|
|
169
|
-
* @param {number} [precision=2] Number of digits after the decimal point
|
|
170
|
-
* @returns {number} Generated float
|
|
171
|
-
*/
|
|
172
|
-
export function randomFloat(prng: PRNGParameters, min: number = 0, max: number = 1, precision: number = 2): number {
|
|
173
|
-
return parseFloat(Math.min(min + random(prng) * (max - min), max).toFixed(precision));
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Generate a pseudo-random integer number within a specified range
|
|
178
|
-
*
|
|
179
|
-
* @param {PRNGParameters} prng PRNG parameters
|
|
180
|
-
* @param {number} min Minimum boundary
|
|
181
|
-
* @param {number} max Maximum boundary
|
|
182
|
-
* @returns {number} Generated integer
|
|
183
|
-
*/
|
|
184
|
-
export function randomInt(prng: PRNGParameters, min: number, max: number): number {
|
|
185
|
-
return Math.floor(random(prng) * (max - min + 1) + min);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Generate a pseudo-random hexadecimal color
|
|
190
|
-
*
|
|
191
|
-
* @param {PRNGParameters} prng PRNG parameters
|
|
192
|
-
* @returns {string} Generated hexadecimal color
|
|
193
|
-
*/
|
|
194
|
-
export function randomHexColor(prng: PRNGParameters): string {
|
|
195
|
-
return '#' + ('00000' + ((random(prng) * (1 << 24)) | 0).toString(16)).slice(-6);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Pick a pseudo-random item from a given array
|
|
200
|
-
*
|
|
201
|
-
* @param {PRNGParameters} prng PRNG parameters
|
|
202
|
-
* @param {T[]} array Array to pick the item from
|
|
203
|
-
* @returns {T|undefined} Random item picked
|
|
204
|
-
*/
|
|
205
|
-
export function randomItem<T = unknown>(prng: PRNGParameters, array: T[]): T | undefined {
|
|
206
|
-
if (array.length === 0) return undefined;
|
|
207
|
-
return array[randomInt(prng, 0, array.length - 1)];
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Pick a pseudo-random property value from a given object
|
|
212
|
-
*
|
|
213
|
-
* @param {PRNGParameters} prng PRNG parameters
|
|
214
|
-
* @param {object} object Object to pick the property from
|
|
215
|
-
* @returns {T|undefined} Random item picked
|
|
216
|
-
*/
|
|
217
|
-
export function randomObjectProperty<T = unknown>(prng: PRNGParameters, object: Record<string, T>): T | undefined {
|
|
218
|
-
const keys = Object.keys(object);
|
|
219
|
-
const key = randomItem(prng, keys);
|
|
220
|
-
if (key && object.hasOwnProperty(key)) {
|
|
221
|
-
return object[key as keyof object];
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Select a pseudo-random index from an array of weighted items
|
|
227
|
-
*
|
|
228
|
-
* @param {PRNGParameters} prng PRNG parameters
|
|
229
|
-
* @param {number[]} weights Array of weights
|
|
230
|
-
* @returns {number} Random index based on weights
|
|
231
|
-
*/
|
|
232
|
-
export function randomIndex(prng: PRNGParameters, weights: number[]): number {
|
|
233
|
-
if (weights.length === 0) return -1;
|
|
234
|
-
|
|
235
|
-
let totalWeight = 0;
|
|
236
|
-
for (let weight of weights) {
|
|
237
|
-
totalWeight += weight;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
if (totalWeight <= 0) console.warn('PRNG randomIndex()', 'Weights must sum to > 0', totalWeight);
|
|
241
|
-
|
|
242
|
-
let weight = random(prng) * totalWeight;
|
|
243
|
-
for (let i = 0; i < weights.length; i++) {
|
|
244
|
-
if (weight < weights[i]) return i;
|
|
245
|
-
weight -= weights[i];
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return 0;
|
|
249
|
-
}
|