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