toosoon-utils 2.4.0 → 2.4.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/lib/classes/_pool.js +7 -4
- package/lib/classes/color-scale.js +40 -37
- package/lib/classes/frame-rate.js +5 -3
- package/lib/colors.js +57 -35
- package/lib/constants.js +9 -6
- package/lib/dom.js +9 -3
- package/lib/files.js +7 -2
- package/lib/functions.js +13 -11
- package/lib/geometry.js +25 -13
- package/lib/maths.js +41 -19
- package/lib/prng.js +33 -15
- package/lib/random.js +29 -14
- 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 +3 -4
package/lib/functions.js
CHANGED
|
@@ -1,23 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.now = exports.defer = exports.wait = exports.noop = void 0;
|
|
1
4
|
/**
|
|
2
5
|
* No-op function
|
|
3
6
|
*/
|
|
4
|
-
|
|
7
|
+
var noop = function () { };
|
|
8
|
+
exports.noop = noop;
|
|
5
9
|
/**
|
|
6
10
|
* Promise wrapped setTimeout
|
|
7
11
|
*
|
|
8
12
|
* @param {number} [timeout=0] Time to wait (in milliseconds)
|
|
9
13
|
* @returns {Promise}
|
|
10
14
|
*/
|
|
11
|
-
|
|
15
|
+
function wait(timeout) {
|
|
12
16
|
if (timeout === void 0) { timeout = 0; }
|
|
13
17
|
return new Promise(function (resolve) { return setTimeout(resolve, timeout); });
|
|
14
18
|
}
|
|
19
|
+
exports.wait = wait;
|
|
15
20
|
/**
|
|
16
21
|
* Deferred promise implementation
|
|
17
22
|
*
|
|
18
23
|
* @returns {Deferred}
|
|
19
24
|
*/
|
|
20
|
-
|
|
25
|
+
function defer() {
|
|
21
26
|
var resolve;
|
|
22
27
|
var reject;
|
|
23
28
|
var promise = new Promise(function (_resolve, _reject) {
|
|
@@ -26,13 +31,10 @@ export function defer() {
|
|
|
26
31
|
});
|
|
27
32
|
return { promise: promise, resolve: resolve, reject: reject };
|
|
28
33
|
}
|
|
29
|
-
|
|
30
|
-
* Polyfill for `now()` functions
|
|
31
|
-
*/
|
|
32
|
-
export var now;
|
|
34
|
+
exports.defer = defer;
|
|
33
35
|
// In node.js, use `process.hrtime`
|
|
34
36
|
if (typeof process !== 'undefined' && process.hrtime) {
|
|
35
|
-
now = function () {
|
|
37
|
+
exports.now = function () {
|
|
36
38
|
// Convert [seconds, nanoseconds] to milliseconds
|
|
37
39
|
var time = process.hrtime();
|
|
38
40
|
return time[0] * 1000 + time[1] / 1000000;
|
|
@@ -41,13 +43,13 @@ if (typeof process !== 'undefined' && process.hrtime) {
|
|
|
41
43
|
// In a browser use `performance` or `Date`
|
|
42
44
|
else if (typeof performance !== 'undefined') {
|
|
43
45
|
// This must be bound, because directly assigning this function leads to an invocation exception in Chrome
|
|
44
|
-
now = performance.now.bind(performance);
|
|
46
|
+
exports.now = performance.now.bind(performance);
|
|
45
47
|
}
|
|
46
48
|
else if (typeof Date.now !== 'undefined') {
|
|
47
|
-
now = Date.now;
|
|
49
|
+
exports.now = Date.now;
|
|
48
50
|
}
|
|
49
51
|
else {
|
|
50
|
-
now = function () {
|
|
52
|
+
exports.now = function () {
|
|
51
53
|
return new Date().getTime();
|
|
52
54
|
};
|
|
53
55
|
}
|
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
|
-
|
|
45
|
+
function closestAngle(source, target) {
|
|
40
46
|
var delta = target - source;
|
|
41
|
-
return delta > PI ? target - 2 * PI : target < -PI ? delta + 2 * PI : target;
|
|
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
|
-
|
|
59
|
+
function distance(x1, y1, x2, y2) {
|
|
53
60
|
var dx = x1 - x2;
|
|
54
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,13 +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) {
|
|
77
86
|
if (target === void 0) { target = { x: 0, y: 0, z: 0 }; }
|
|
78
87
|
target.x = radius * Math.sin(phi) * Math.sin(theta);
|
|
79
88
|
target.y = radius * Math.cos(phi);
|
|
80
89
|
target.z = radius * Math.sin(phi) * Math.cos(theta);
|
|
81
90
|
return target;
|
|
82
91
|
}
|
|
92
|
+
exports.radToSphere = radToSphere;
|
|
83
93
|
/**
|
|
84
94
|
* Make a target fit a container
|
|
85
95
|
*
|
|
@@ -113,9 +123,10 @@ function fit(target, container, mode) {
|
|
|
113
123
|
* @param {FitInput} container Dimension of the container
|
|
114
124
|
* @returns {FitOutput}
|
|
115
125
|
*/
|
|
116
|
-
|
|
126
|
+
function cover(target, container) {
|
|
117
127
|
return fit(target, container, 'cover');
|
|
118
128
|
}
|
|
129
|
+
exports.cover = cover;
|
|
119
130
|
/**
|
|
120
131
|
* Make a target fit a container (contain mode)
|
|
121
132
|
*
|
|
@@ -123,6 +134,7 @@ export function cover(target, container) {
|
|
|
123
134
|
* @param {FitInput} container Dimension of the container
|
|
124
135
|
* @returns {FitOutput}
|
|
125
136
|
*/
|
|
126
|
-
|
|
137
|
+
function contain(target, container) {
|
|
127
138
|
return fit(target, container, 'contain');
|
|
128
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,23 +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) {
|
|
36
42
|
if (mode === void 0) { mode = 'ceil'; }
|
|
37
43
|
return Math.pow(2, Math[mode](Math.log(value) / Math.log(2)));
|
|
38
44
|
}
|
|
45
|
+
exports.toPowerOf2 = toPowerOf2;
|
|
39
46
|
/**
|
|
40
47
|
* Return the sign (positive or negative) of a number
|
|
41
48
|
*
|
|
42
49
|
* @param {number} value Value to check
|
|
43
50
|
* @returns {number} 1 if the given number is positive, -1 if it is negative, otherwise 0
|
|
44
51
|
*/
|
|
45
|
-
|
|
52
|
+
function sign(value) {
|
|
46
53
|
if (value > 0)
|
|
47
54
|
return 1;
|
|
48
55
|
else if (value < 0)
|
|
49
56
|
return -1;
|
|
50
57
|
return 0;
|
|
51
58
|
}
|
|
59
|
+
exports.sign = sign;
|
|
52
60
|
/**
|
|
53
61
|
* Clamp a value between two bounds
|
|
54
62
|
*
|
|
@@ -57,11 +65,12 @@ export function sign(value) {
|
|
|
57
65
|
* @param {number} [max=1] Maximum boundary
|
|
58
66
|
* @returns {number} Clamped value
|
|
59
67
|
*/
|
|
60
|
-
|
|
68
|
+
function clamp(value, min, max) {
|
|
61
69
|
if (min === void 0) { min = 0; }
|
|
62
70
|
if (max === void 0) { max = 1; }
|
|
63
71
|
return Math.min(max, Math.max(min, value));
|
|
64
72
|
}
|
|
73
|
+
exports.clamp = clamp;
|
|
65
74
|
/**
|
|
66
75
|
* Linear interpolation between two values (lerping)
|
|
67
76
|
*
|
|
@@ -70,9 +79,10 @@ export function clamp(value, min, max) {
|
|
|
70
79
|
* @param {number} max Maximum value
|
|
71
80
|
* @returns {number} Lerped value
|
|
72
81
|
*/
|
|
73
|
-
|
|
82
|
+
function lerp(value, min, max) {
|
|
74
83
|
return min + (max - min) * value;
|
|
75
84
|
}
|
|
85
|
+
exports.lerp = lerp;
|
|
76
86
|
/**
|
|
77
87
|
* Triangular interpolation between two values
|
|
78
88
|
*
|
|
@@ -82,10 +92,11 @@ export function lerp(value, min, max) {
|
|
|
82
92
|
* @param {number} target Triangle target value
|
|
83
93
|
* @returns {number} Interpolated value
|
|
84
94
|
*/
|
|
85
|
-
|
|
95
|
+
function triLerp(value, min, max, target) {
|
|
86
96
|
var x = Math.pow(1 + Math.abs(target - max) / Math.abs(target - min), -1);
|
|
87
97
|
return value <= x ? min - (min - target) * (value / x) : target - (target - max) * ((value - x) / (1 - x));
|
|
88
98
|
}
|
|
99
|
+
exports.triLerp = triLerp;
|
|
89
100
|
/**
|
|
90
101
|
* Exponential interpolation between two values
|
|
91
102
|
*
|
|
@@ -96,10 +107,11 @@ export function triLerp(value, min, max, target) {
|
|
|
96
107
|
* @param {number} targetMax Upper bound of the value's target range
|
|
97
108
|
* @returns {number} Interpolated value
|
|
98
109
|
*/
|
|
99
|
-
|
|
110
|
+
function expLerp(value, currentMin, currentMax, targetMin, targetMax) {
|
|
100
111
|
return (targetMin *
|
|
101
112
|
Math.pow(targetMax / targetMin, (clamp(value, currentMin, currentMax) - currentMin) / (currentMax - currentMin)));
|
|
102
113
|
}
|
|
114
|
+
exports.expLerp = expLerp;
|
|
103
115
|
/**
|
|
104
116
|
* Normalize a value between two bounds
|
|
105
117
|
*
|
|
@@ -108,9 +120,10 @@ export function expLerp(value, currentMin, currentMax, targetMin, targetMax) {
|
|
|
108
120
|
* @param {number} max Maximum boundary
|
|
109
121
|
* @returns {number} Normalized value
|
|
110
122
|
*/
|
|
111
|
-
|
|
123
|
+
function normalize(value, min, max) {
|
|
112
124
|
return (value - min) / (max - min);
|
|
113
125
|
}
|
|
126
|
+
exports.normalize = normalize;
|
|
114
127
|
/**
|
|
115
128
|
* Re-map a number from one range to another
|
|
116
129
|
*
|
|
@@ -121,9 +134,10 @@ export function normalize(value, min, max) {
|
|
|
121
134
|
* @param {number} targetMax Upper bound of the value's target range
|
|
122
135
|
* @returns {number} Re-mapped value
|
|
123
136
|
*/
|
|
124
|
-
|
|
137
|
+
function map(value, currentMin, currentMax, targetMin, targetMax) {
|
|
125
138
|
return ((value - currentMin) / (currentMax - currentMin)) * (targetMax - targetMin) + targetMin;
|
|
126
139
|
}
|
|
140
|
+
exports.map = map;
|
|
127
141
|
/**
|
|
128
142
|
* Round a number up to a nearest multiple
|
|
129
143
|
*
|
|
@@ -131,12 +145,13 @@ export function map(value, currentMin, currentMax, targetMin, targetMax) {
|
|
|
131
145
|
* @param {number} [multiple=1] Multiple to round to
|
|
132
146
|
* @returns {number} Closest multiple
|
|
133
147
|
*/
|
|
134
|
-
|
|
148
|
+
function roundTo(value, multiple) {
|
|
135
149
|
if (multiple === void 0) { multiple = 1; }
|
|
136
150
|
if (multiple === 0)
|
|
137
151
|
return value;
|
|
138
152
|
return Math.round(value / multiple) * multiple;
|
|
139
153
|
}
|
|
154
|
+
exports.roundTo = roundTo;
|
|
140
155
|
/**
|
|
141
156
|
* Modulo absolute a value based on a length
|
|
142
157
|
*
|
|
@@ -144,12 +159,13 @@ export function roundTo(value, multiple) {
|
|
|
144
159
|
* @param {number} length Total length
|
|
145
160
|
* @returns {number} Modulated value
|
|
146
161
|
*/
|
|
147
|
-
|
|
162
|
+
function modAbs(value, length) {
|
|
148
163
|
if (value < 0) {
|
|
149
164
|
return length + (value % length);
|
|
150
165
|
}
|
|
151
166
|
return value % length;
|
|
152
167
|
}
|
|
168
|
+
exports.modAbs = modAbs;
|
|
153
169
|
/**
|
|
154
170
|
* Move back and forth a value between 0 and length, so that it is never larger than length and never smaller than 0
|
|
155
171
|
*
|
|
@@ -157,10 +173,11 @@ export function modAbs(value, length) {
|
|
|
157
173
|
* @param {number} length Total length
|
|
158
174
|
* @returns {number} PingPonged value
|
|
159
175
|
*/
|
|
160
|
-
|
|
176
|
+
function pingPong(value, length) {
|
|
161
177
|
value = modAbs(value, length * 2);
|
|
162
178
|
return length - Math.abs(value - length);
|
|
163
179
|
}
|
|
180
|
+
exports.pingPong = pingPong;
|
|
164
181
|
/**
|
|
165
182
|
* Smooth a value using cubic Hermite interpolation
|
|
166
183
|
*
|
|
@@ -169,12 +186,13 @@ export function pingPong(value, length) {
|
|
|
169
186
|
* @param {number} [max=1] Maximum boundary
|
|
170
187
|
* @returns {number} Normalized smoothed value
|
|
171
188
|
*/
|
|
172
|
-
|
|
189
|
+
function smoothstep(value, min, max) {
|
|
173
190
|
if (min === void 0) { min = 0; }
|
|
174
191
|
if (max === void 0) { max = 1; }
|
|
175
192
|
var x = clamp(normalize(value, min, max));
|
|
176
193
|
return x * x * (3 - 2 * x);
|
|
177
194
|
}
|
|
195
|
+
exports.smoothstep = smoothstep;
|
|
178
196
|
/**
|
|
179
197
|
* Re-map the [0, 1] interval into [0, 1] parabola, such that corners are remaped to 0 and the center to 1
|
|
180
198
|
* -> parabola(0) = parabola(1) = 0, and parabola(0.5) = 1
|
|
@@ -183,28 +201,31 @@ export function smoothstep(value, min, max) {
|
|
|
183
201
|
* @param {number} [power=1] Parabola power
|
|
184
202
|
* @returns {number} Normalized re-mapped value
|
|
185
203
|
*/
|
|
186
|
-
|
|
204
|
+
function parabola(x, power) {
|
|
187
205
|
if (power === void 0) { power = 1; }
|
|
188
206
|
return Math.pow(4 * x * (1 - x), power);
|
|
189
207
|
}
|
|
208
|
+
exports.parabola = parabola;
|
|
190
209
|
/**
|
|
191
210
|
* Return the sum of numbers
|
|
192
211
|
*
|
|
193
212
|
* @param {number[]} array Array of numbers
|
|
194
213
|
* @returns {number} Total sum
|
|
195
214
|
*/
|
|
196
|
-
|
|
215
|
+
function sum(array) {
|
|
197
216
|
return array.reduce(function (previous, current) { return previous + current; });
|
|
198
217
|
}
|
|
218
|
+
exports.sum = sum;
|
|
199
219
|
/**
|
|
200
220
|
* Return the average of numbers
|
|
201
221
|
*
|
|
202
222
|
* @param {number[]} array Array of numbers
|
|
203
223
|
* @returns {number} Total average
|
|
204
224
|
*/
|
|
205
|
-
|
|
225
|
+
function average(array) {
|
|
206
226
|
return sum(array) / array.length;
|
|
207
227
|
}
|
|
228
|
+
exports.average = average;
|
|
208
229
|
/**
|
|
209
230
|
* Smoothly interpolate a number toward another
|
|
210
231
|
*
|
|
@@ -214,6 +235,7 @@ export function average(array) {
|
|
|
214
235
|
* @param {number} delta Delta time (in seconds)
|
|
215
236
|
* @returns {number} Interpolated number
|
|
216
237
|
*/
|
|
217
|
-
|
|
238
|
+
function damp(value, target, damping, delta) {
|
|
218
239
|
return lerp(1 - Math.exp(-damping * delta), value, target);
|
|
219
240
|
}
|
|
241
|
+
exports.damp = damp;
|
package/lib/prng.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
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
|
-
|
|
10
|
+
function cyrb128(seed) {
|
|
8
11
|
var h1 = 1779033703;
|
|
9
12
|
var h2 = 3144134277;
|
|
10
13
|
var h3 = 1013904242;
|
|
@@ -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,7 +38,7 @@ 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;
|
|
@@ -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,25 +68,27 @@ 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
|
-
|
|
78
|
+
function mulberry32(a) {
|
|
73
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;
|
|
@@ -93,13 +100,14 @@ export function jsf32(a, b, c, d) {
|
|
|
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
|
-
|
|
110
|
+
function xoshiro128ss(a, b, c, d) {
|
|
103
111
|
var t = b << 9;
|
|
104
112
|
var r = a * 5;
|
|
105
113
|
r = ((r << 7) | (r >>> 25)) * 9;
|
|
@@ -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
|
-
|
|
130
|
+
function random(prng) {
|
|
122
131
|
var seed = typeof prng === 'string' ? prng : prng.seed;
|
|
123
132
|
var algorithm = typeof prng === 'string' ? splitmix32 : prng.algorithm;
|
|
124
133
|
var hashes = cyrb128(seed);
|
|
125
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,10 +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) {
|
|
135
145
|
if (probability === void 0) { probability = 0.5; }
|
|
136
146
|
return random(prng) < probability;
|
|
137
147
|
}
|
|
148
|
+
exports.randomBoolean = randomBoolean;
|
|
138
149
|
/**
|
|
139
150
|
* Generate a pseudo-random sign (1 or -1)
|
|
140
151
|
*
|
|
@@ -142,10 +153,11 @@ export function randomBoolean(prng, probability) {
|
|
|
142
153
|
* @param {number} [probability=0.5] Probability to get 1
|
|
143
154
|
* @returns {number} Either 1 or -1
|
|
144
155
|
*/
|
|
145
|
-
|
|
156
|
+
function randomSign(prng, probability) {
|
|
146
157
|
if (probability === void 0) { probability = 0.5; }
|
|
147
158
|
return randomBoolean(prng, probability) ? 1 : -1;
|
|
148
159
|
}
|
|
160
|
+
exports.randomSign = randomSign;
|
|
149
161
|
/**
|
|
150
162
|
* Generate a pseudo-random floating-point number within a specified range
|
|
151
163
|
*
|
|
@@ -155,12 +167,13 @@ export function randomSign(prng, probability) {
|
|
|
155
167
|
* @param {number} [precision=2] Number of digits after the decimal point
|
|
156
168
|
* @returns {number} Generated float
|
|
157
169
|
*/
|
|
158
|
-
|
|
170
|
+
function randomFloat(prng, min, max, precision) {
|
|
159
171
|
if (min === void 0) { min = 0; }
|
|
160
172
|
if (max === void 0) { max = 1; }
|
|
161
173
|
if (precision === void 0) { precision = 2; }
|
|
162
174
|
return parseFloat(Math.min(min + random(prng) * (max - min), max).toFixed(precision));
|
|
163
175
|
}
|
|
176
|
+
exports.randomFloat = randomFloat;
|
|
164
177
|
/**
|
|
165
178
|
* Generate a pseudo-random integer number within a specified range
|
|
166
179
|
*
|
|
@@ -169,18 +182,20 @@ export function randomFloat(prng, min, max, precision) {
|
|
|
169
182
|
* @param {number} max Maximum boundary
|
|
170
183
|
* @returns {number} Generated integer
|
|
171
184
|
*/
|
|
172
|
-
|
|
185
|
+
function randomInt(prng, min, max) {
|
|
173
186
|
return Math.floor(random(prng) * (max - min + 1) + min);
|
|
174
187
|
}
|
|
188
|
+
exports.randomInt = randomInt;
|
|
175
189
|
/**
|
|
176
190
|
* Generate a pseudo-random hexadecimal color
|
|
177
191
|
*
|
|
178
192
|
* @param {PRNGParameters} prng PRNG parameters
|
|
179
193
|
* @returns {string} Generated hexadecimal color
|
|
180
194
|
*/
|
|
181
|
-
|
|
195
|
+
function randomHexColor(prng) {
|
|
182
196
|
return '#' + ('00000' + ((random(prng) * (1 << 24)) | 0).toString(16)).slice(-6);
|
|
183
197
|
}
|
|
198
|
+
exports.randomHexColor = randomHexColor;
|
|
184
199
|
/**
|
|
185
200
|
* Pick a pseudo-random item from a given array
|
|
186
201
|
*
|
|
@@ -188,11 +203,12 @@ export function randomHexColor(prng) {
|
|
|
188
203
|
* @param {T[]} array Array to pick the item from
|
|
189
204
|
* @returns {T|undefined} Random item picked
|
|
190
205
|
*/
|
|
191
|
-
|
|
206
|
+
function randomItem(prng, array) {
|
|
192
207
|
if (array.length === 0)
|
|
193
208
|
return undefined;
|
|
194
209
|
return array[randomInt(prng, 0, array.length - 1)];
|
|
195
210
|
}
|
|
211
|
+
exports.randomItem = randomItem;
|
|
196
212
|
/**
|
|
197
213
|
* Pick a pseudo-random property value from a given object
|
|
198
214
|
*
|
|
@@ -200,13 +216,14 @@ export function randomItem(prng, array) {
|
|
|
200
216
|
* @param {object} object Object to pick the property from
|
|
201
217
|
* @returns {T|undefined} Random item picked
|
|
202
218
|
*/
|
|
203
|
-
|
|
219
|
+
function randomObjectProperty(prng, object) {
|
|
204
220
|
var keys = Object.keys(object);
|
|
205
221
|
var key = randomItem(prng, keys);
|
|
206
222
|
if (key && object.hasOwnProperty(key)) {
|
|
207
223
|
return object[key];
|
|
208
224
|
}
|
|
209
225
|
}
|
|
226
|
+
exports.randomObjectProperty = randomObjectProperty;
|
|
210
227
|
/**
|
|
211
228
|
* Select a pseudo-random index from an array of weighted items
|
|
212
229
|
*
|
|
@@ -214,7 +231,7 @@ export function randomObjectProperty(prng, object) {
|
|
|
214
231
|
* @param {number[]} weights Array of weights
|
|
215
232
|
* @returns {number} Random index based on weights
|
|
216
233
|
*/
|
|
217
|
-
|
|
234
|
+
function randomIndex(prng, weights) {
|
|
218
235
|
if (weights.length === 0)
|
|
219
236
|
return -1;
|
|
220
237
|
var totalWeight = 0;
|
|
@@ -232,3 +249,4 @@ export function randomIndex(prng, weights) {
|
|
|
232
249
|
}
|
|
233
250
|
return 0;
|
|
234
251
|
}
|
|
252
|
+
exports.randomIndex = randomIndex;
|