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