toosoon-utils 4.2.2 → 4.3.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 +499 -575
- package/lib/colors.d.ts +147 -66
- package/lib/colors.js +149 -63
- package/lib/constants.js +1 -1
- package/lib/dom.d.ts +1 -1
- package/lib/dom.js +1 -1
- package/lib/extras/colors/Color.d.ts +406 -0
- package/lib/extras/colors/Color.js +546 -0
- package/lib/extras/colors/ColorPalette.d.ts +105 -0
- package/lib/extras/colors/ColorPalette.js +124 -0
- package/lib/extras/colors/ColorScale.d.ts +257 -0
- package/lib/extras/colors/ColorScale.js +347 -0
- package/lib/extras/colors/_ColorScale.d.ts +62 -0
- package/lib/extras/colors/_ColorScale.js +156 -0
- package/lib/extras/colors/index.d.ts +3 -0
- package/lib/extras/colors/index.js +3 -0
- package/lib/extras/frame-rate/FrameRate.d.ts +1 -1
- package/lib/extras/frame-rate/FrameRate.js +2 -2
- package/lib/extras/geometry/Vector.d.ts +1 -1
- package/lib/extras/geometry/Vector2.d.ts +24 -11
- package/lib/extras/geometry/Vector2.js +41 -23
- package/lib/extras/geometry/Vector3.d.ts +12 -5
- package/lib/extras/geometry/Vector3.js +23 -10
- package/lib/extras/paths/Path.d.ts +3 -3
- package/lib/extras/paths/Path.js +10 -10
- package/lib/extras/paths/PathContext.d.ts +7 -17
- package/lib/extras/paths/PathContext.js +122 -144
- package/lib/extras/paths/PathSVG.d.ts +31 -25
- package/lib/extras/paths/PathSVG.js +36 -39
- package/lib/extras/paths/index.d.ts +1 -1
- package/lib/geometry.js +1 -1
- package/lib/maths.d.ts +19 -13
- package/lib/maths.js +23 -17
- package/lib/prng.d.ts +4 -4
- package/lib/prng.js +4 -4
- package/lib/random.d.ts +4 -4
- package/lib/random.js +4 -4
- package/lib/strings.d.ts +14 -8
- package/lib/strings.js +14 -8
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.d.ts +15 -8
- package/package.json +14 -14
|
@@ -16,10 +16,10 @@ export default class PathSVG extends PathContext {
|
|
|
16
16
|
/**
|
|
17
17
|
* Serialize this path into a SVG path string
|
|
18
18
|
*
|
|
19
|
-
* @param {
|
|
19
|
+
* @param {PathSVGSerializationParameters} [params] Serialization parameters
|
|
20
20
|
* @returns {string}
|
|
21
21
|
*/
|
|
22
|
-
toString(params
|
|
22
|
+
toString(params) {
|
|
23
23
|
return PathSVG.serialize(this, params);
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
@@ -37,42 +37,40 @@ export default class PathSVG extends PathContext {
|
|
|
37
37
|
* Serialize a {@link Curve}
|
|
38
38
|
*
|
|
39
39
|
* @param {Curve} curve Curve to serialize
|
|
40
|
-
* @param {
|
|
41
|
-
* @
|
|
42
|
-
* @param {number} [params.curveResolution] Resolution used for curve approximations
|
|
43
|
-
* @returns string
|
|
40
|
+
* @param {PathSVGSerializationParameters} [params] Serialization parameters
|
|
41
|
+
* @returns {string}
|
|
44
42
|
*/
|
|
45
|
-
static serialize(curve, { approximate,
|
|
43
|
+
static serialize(curve, { approximate, resolution } = {}) {
|
|
46
44
|
if (curve instanceof Path) {
|
|
47
|
-
return
|
|
45
|
+
return this.serializePath(curve, { approximate, resolution });
|
|
48
46
|
}
|
|
49
47
|
if (approximate === true) {
|
|
50
|
-
const points =
|
|
48
|
+
const points = this.approximate(curve, resolution);
|
|
51
49
|
return points.map(([x, y]) => `L${x},${y}`).join(' ');
|
|
52
50
|
}
|
|
53
51
|
if (curve instanceof LineCurve) {
|
|
54
|
-
return
|
|
52
|
+
return this.serializeLineCurve(curve);
|
|
55
53
|
}
|
|
56
54
|
if (curve instanceof PolylineCurve) {
|
|
57
|
-
return
|
|
55
|
+
return this.serializePolylineCurve(curve);
|
|
58
56
|
}
|
|
59
57
|
if (curve instanceof QuadraticBezierCurve) {
|
|
60
|
-
return
|
|
58
|
+
return this.serializeQuadraticBezierCurve(curve);
|
|
61
59
|
}
|
|
62
60
|
if (curve instanceof CubicBezierCurve) {
|
|
63
|
-
return
|
|
61
|
+
return this.serializeCubicBezierCurve(curve);
|
|
64
62
|
}
|
|
65
63
|
if (curve instanceof CatmullRomCurve) {
|
|
66
|
-
return
|
|
64
|
+
return this.serializeCatmullRomCurve(curve, resolution);
|
|
67
65
|
}
|
|
68
66
|
if (curve instanceof SplineCurve) {
|
|
69
|
-
return
|
|
67
|
+
return this.serializeSplineCurve(curve, resolution);
|
|
70
68
|
}
|
|
71
69
|
if (curve instanceof EllipseCurve) {
|
|
72
|
-
return
|
|
70
|
+
return this.serializeEllipseCurve(curve);
|
|
73
71
|
}
|
|
74
72
|
if (curve instanceof ArcCurve) {
|
|
75
|
-
return
|
|
73
|
+
return this.serializeArcCurve(curve);
|
|
76
74
|
}
|
|
77
75
|
return '';
|
|
78
76
|
}
|
|
@@ -80,7 +78,7 @@ export default class PathSVG extends PathContext {
|
|
|
80
78
|
* Serialize a {@link LineCurve}
|
|
81
79
|
*
|
|
82
80
|
* @param {LineCurve} curve LineCurve to serialize
|
|
83
|
-
* @returns string
|
|
81
|
+
* @returns {string}
|
|
84
82
|
*/
|
|
85
83
|
static serializeLineCurve(curve) {
|
|
86
84
|
const { x2, y2 } = curve;
|
|
@@ -90,7 +88,7 @@ export default class PathSVG extends PathContext {
|
|
|
90
88
|
* Serialize a {@link PolylineCurve}
|
|
91
89
|
*
|
|
92
90
|
* @param {PolylineCurve} curve PolylineCurve to serialize
|
|
93
|
-
* @returns string
|
|
91
|
+
* @returns {string}
|
|
94
92
|
*/
|
|
95
93
|
static serializePolylineCurve(curve) {
|
|
96
94
|
const { points } = curve;
|
|
@@ -100,7 +98,7 @@ export default class PathSVG extends PathContext {
|
|
|
100
98
|
* Serialize a {@link QuadraticBezierCurve}
|
|
101
99
|
*
|
|
102
100
|
* @param {QuadraticBezierCurve} curve QuadraticBezierCurve to serialize
|
|
103
|
-
* @returns string
|
|
101
|
+
* @returns {string}
|
|
104
102
|
*/
|
|
105
103
|
static serializeQuadraticBezierCurve(curve) {
|
|
106
104
|
const { cpx, cpy, x2, y2 } = curve;
|
|
@@ -110,7 +108,7 @@ export default class PathSVG extends PathContext {
|
|
|
110
108
|
* Serialize a {@link CubicBezierCurve}
|
|
111
109
|
*
|
|
112
110
|
* @param {CubicBezierCurve} curve CubicBezierCurve to serialize
|
|
113
|
-
* @returns string
|
|
111
|
+
* @returns {string}
|
|
114
112
|
*/
|
|
115
113
|
static serializeCubicBezierCurve(curve) {
|
|
116
114
|
const { cp1x, cp1y, cp2x, cp2y, x2, y2 } = curve;
|
|
@@ -120,29 +118,29 @@ export default class PathSVG extends PathContext {
|
|
|
120
118
|
* Serialize a {@link CatmullRomCurve} by approximating it into straight lines
|
|
121
119
|
*
|
|
122
120
|
* @param {CatmullRomCurve} curve CatmullRomCurve to serialize
|
|
123
|
-
* @param {number} [
|
|
124
|
-
* @returns string
|
|
121
|
+
* @param {number} [resolution] Approximation resolution
|
|
122
|
+
* @returns {string}
|
|
125
123
|
*/
|
|
126
|
-
static serializeCatmullRomCurve(curve,
|
|
127
|
-
const points =
|
|
124
|
+
static serializeCatmullRomCurve(curve, resolution) {
|
|
125
|
+
const points = this.approximate(curve, resolution);
|
|
128
126
|
return points.map(([x, y]) => `L${x},${y}`).join(' ');
|
|
129
127
|
}
|
|
130
128
|
/**
|
|
131
129
|
* Serialize a {@link SplineCurve} by approximating it into straight lines
|
|
132
130
|
*
|
|
133
131
|
* @param {SplineCurve} curve SplineCurve to serialize
|
|
134
|
-
* @param {number} [
|
|
135
|
-
* @returns string
|
|
132
|
+
* @param {number} [resolution] Approximation resolution
|
|
133
|
+
* @returns {string}
|
|
136
134
|
*/
|
|
137
|
-
static serializeSplineCurve(curve,
|
|
138
|
-
const points =
|
|
135
|
+
static serializeSplineCurve(curve, resolution) {
|
|
136
|
+
const points = this.approximate(curve, resolution);
|
|
139
137
|
return points.map(([x, y]) => `L${x},${y}`).join(' ');
|
|
140
138
|
}
|
|
141
139
|
/**
|
|
142
140
|
* Serialize an {@link EllipseCurve}
|
|
143
141
|
*
|
|
144
142
|
* @param {EllipseCurve} curve EllipseCurve to serialize
|
|
145
|
-
* @returns string
|
|
143
|
+
* @returns {string}
|
|
146
144
|
*/
|
|
147
145
|
static serializeEllipseCurve(curve) {
|
|
148
146
|
const { cx, cy, rx, ry, rotation, startAngle, endAngle, counterclockwise } = curve;
|
|
@@ -167,29 +165,28 @@ export default class PathSVG extends PathContext {
|
|
|
167
165
|
* Serialize an {@link ArcCurve}
|
|
168
166
|
*
|
|
169
167
|
* @param {ArcCurve} curve ArcCurve to serialize
|
|
170
|
-
* @returns string
|
|
168
|
+
* @returns {string}
|
|
171
169
|
*/
|
|
172
170
|
static serializeArcCurve(curve) {
|
|
173
|
-
return
|
|
171
|
+
return this.serializeEllipseCurve(curve);
|
|
174
172
|
}
|
|
175
173
|
/**
|
|
176
174
|
* Serialize an {@link Path}
|
|
177
175
|
*
|
|
178
176
|
* @param {Path} path Path to serialize
|
|
179
|
-
* @param {
|
|
180
|
-
* @
|
|
181
|
-
* @returns string
|
|
177
|
+
* @param {PathSVGSerializationParameters} [params] Serialization parameters
|
|
178
|
+
* @returns {string}
|
|
182
179
|
*/
|
|
183
|
-
static serializePath(path, params
|
|
184
|
-
return path.
|
|
180
|
+
static serializePath(path, params) {
|
|
181
|
+
return path.curves
|
|
185
182
|
.map((curve, index) => {
|
|
186
183
|
let commands = ``;
|
|
187
|
-
const previousPoint = path.
|
|
184
|
+
const previousPoint = path.curves[index - 1]?.getPoint(1);
|
|
188
185
|
const newPoint = curve.getPoint(0);
|
|
189
186
|
if (index === 0 || !previousPoint?.equals(newPoint)) {
|
|
190
187
|
commands += `M${newPoint.x},${newPoint.y}`;
|
|
191
188
|
}
|
|
192
|
-
commands +=
|
|
189
|
+
commands += this.serialize(curve, params);
|
|
193
190
|
return commands;
|
|
194
191
|
})
|
|
195
192
|
.join(' ');
|
package/lib/geometry.js
CHANGED
|
@@ -69,7 +69,7 @@ export function diagonal(width, height) {
|
|
|
69
69
|
*
|
|
70
70
|
* @param {object} target Dimensions of the target
|
|
71
71
|
* @param {object} container Dimensions of the container
|
|
72
|
-
* @param {
|
|
72
|
+
* @param {string} mode Can be 'contain' | 'cover'
|
|
73
73
|
* @returns {object}
|
|
74
74
|
*/
|
|
75
75
|
function fit(target, container, mode) {
|
package/lib/maths.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ export declare function isPowerOf2(value: number): boolean;
|
|
|
24
24
|
*
|
|
25
25
|
* @param {number} value Incoming value
|
|
26
26
|
* @param {string} [mode='ceil'] Can be 'floor' | 'ceil' | 'round'
|
|
27
|
-
* @returns {number}
|
|
27
|
+
* @returns {number} Computed power of 2
|
|
28
28
|
*/
|
|
29
29
|
export declare function toPowerOf2(value: number, mode?: 'floor' | 'ceil' | 'round'): number;
|
|
30
30
|
/**
|
|
@@ -83,24 +83,29 @@ export declare function map(value: number, currentMin: number, currentMax: numbe
|
|
|
83
83
|
/**
|
|
84
84
|
* Interpolate a value between two values using Triangular interpolation
|
|
85
85
|
*
|
|
86
|
-
* @param {number} t
|
|
87
|
-
* @param {number} min
|
|
88
|
-
* @param {number} max
|
|
89
|
-
* @param {number}
|
|
86
|
+
* @param {number} t Normalized time value to interpolate
|
|
87
|
+
* @param {number} min Minimum value
|
|
88
|
+
* @param {number} max Maximum value
|
|
89
|
+
* @param {number} peak Peak value controling the interpolation triangle shape
|
|
90
|
+
* - peak <= min : linear (same as lerp)
|
|
91
|
+
* - peak <= max : linear (same as lerp)
|
|
92
|
+
* - peak > min && peak > max : triangular
|
|
90
93
|
* @returns {number} Interpolated value
|
|
91
94
|
*/
|
|
92
|
-
export declare function triLerp(t: number, min: number, max: number,
|
|
95
|
+
export declare function triLerp(t: number, min: number, max: number, peak: number): number;
|
|
93
96
|
/**
|
|
94
97
|
* Interpolate a value using Exponential interpolation
|
|
95
98
|
*
|
|
96
|
-
* @param {number} t
|
|
97
|
-
* @param {number}
|
|
98
|
-
* @param {number}
|
|
99
|
-
* @param {number}
|
|
100
|
-
*
|
|
99
|
+
* @param {number} t Normalized time value to interpolate
|
|
100
|
+
* @param {number} min Minimum value
|
|
101
|
+
* @param {number} max Maximum value
|
|
102
|
+
* @param {number} power Exponent controling the interpolation curve shape
|
|
103
|
+
* - power > 1 : ease-in
|
|
104
|
+
* - power < 1 : ease-out
|
|
105
|
+
* - power = 1 : linear (same as lerp)
|
|
101
106
|
* @returns {number} Interpolated value
|
|
102
107
|
*/
|
|
103
|
-
export declare function expLerp(t: number,
|
|
108
|
+
export declare function expLerp(t: number, min: number, max: number, power: number): number;
|
|
104
109
|
/**
|
|
105
110
|
* Interpolate a value using Quadratic Bézier interpolation
|
|
106
111
|
*
|
|
@@ -160,7 +165,8 @@ export declare function pingPong(value: number, length: number): number;
|
|
|
160
165
|
export declare function smoothstep(value: number, min?: number, max?: number): number;
|
|
161
166
|
/**
|
|
162
167
|
* Re-map the [0, 1] interval into [0, 1] parabola, such that corners are remaped to 0 and the center to 1
|
|
163
|
-
*
|
|
168
|
+
* - parabola(0) = parabola(1) = 0
|
|
169
|
+
* - parabola(0.5) = 1
|
|
164
170
|
*
|
|
165
171
|
* @param {number} x Normalized coordinate on X axis
|
|
166
172
|
* @param {number} [power=1] Parabola power
|
package/lib/maths.js
CHANGED
|
@@ -30,7 +30,7 @@ export function isPowerOf2(value) {
|
|
|
30
30
|
*
|
|
31
31
|
* @param {number} value Incoming value
|
|
32
32
|
* @param {string} [mode='ceil'] Can be 'floor' | 'ceil' | 'round'
|
|
33
|
-
* @returns {number}
|
|
33
|
+
* @returns {number} Computed power of 2
|
|
34
34
|
*/
|
|
35
35
|
export function toPowerOf2(value, mode = 'ceil') {
|
|
36
36
|
return Math.pow(2, Math[mode](Math.log(value) / Math.log(2)));
|
|
@@ -109,29 +109,34 @@ export function map(value, currentMin, currentMax, targetMin, targetMax) {
|
|
|
109
109
|
/**
|
|
110
110
|
* Interpolate a value between two values using Triangular interpolation
|
|
111
111
|
*
|
|
112
|
-
* @param {number} t
|
|
113
|
-
* @param {number} min
|
|
114
|
-
* @param {number} max
|
|
115
|
-
* @param {number}
|
|
112
|
+
* @param {number} t Normalized time value to interpolate
|
|
113
|
+
* @param {number} min Minimum value
|
|
114
|
+
* @param {number} max Maximum value
|
|
115
|
+
* @param {number} peak Peak value controling the interpolation triangle shape
|
|
116
|
+
* - peak <= min : linear (same as lerp)
|
|
117
|
+
* - peak <= max : linear (same as lerp)
|
|
118
|
+
* - peak > min && peak > max : triangular
|
|
116
119
|
* @returns {number} Interpolated value
|
|
117
120
|
*/
|
|
118
|
-
export function triLerp(t, min, max,
|
|
119
|
-
const x = Math.pow(1 + Math.abs(
|
|
120
|
-
return t <= x ? min - (min -
|
|
121
|
+
export function triLerp(t, min, max, peak) {
|
|
122
|
+
const x = Math.pow(1 + Math.abs(peak - max) / Math.abs(peak - min), -1);
|
|
123
|
+
return t <= x ? min - (min - peak) * (t / x) : peak - (peak - max) * ((t - x) / (1 - x));
|
|
121
124
|
}
|
|
122
125
|
/**
|
|
123
126
|
* Interpolate a value using Exponential interpolation
|
|
124
127
|
*
|
|
125
|
-
* @param {number} t
|
|
126
|
-
* @param {number}
|
|
127
|
-
* @param {number}
|
|
128
|
-
* @param {number}
|
|
129
|
-
*
|
|
128
|
+
* @param {number} t Normalized time value to interpolate
|
|
129
|
+
* @param {number} min Minimum value
|
|
130
|
+
* @param {number} max Maximum value
|
|
131
|
+
* @param {number} power Exponent controling the interpolation curve shape
|
|
132
|
+
* - power > 1 : ease-in
|
|
133
|
+
* - power < 1 : ease-out
|
|
134
|
+
* - power = 1 : linear (same as lerp)
|
|
130
135
|
* @returns {number} Interpolated value
|
|
131
136
|
*/
|
|
132
|
-
export function expLerp(t,
|
|
133
|
-
|
|
134
|
-
|
|
137
|
+
export function expLerp(t, min, max, power) {
|
|
138
|
+
const factor = Math.pow(t, power);
|
|
139
|
+
return min + (max - min) * factor;
|
|
135
140
|
}
|
|
136
141
|
/**
|
|
137
142
|
* Interpolate a value using Quadratic Bézier interpolation
|
|
@@ -221,7 +226,8 @@ export function smoothstep(value, min = 0, max = 1) {
|
|
|
221
226
|
}
|
|
222
227
|
/**
|
|
223
228
|
* Re-map the [0, 1] interval into [0, 1] parabola, such that corners are remaped to 0 and the center to 1
|
|
224
|
-
*
|
|
229
|
+
* - parabola(0) = parabola(1) = 0
|
|
230
|
+
* - parabola(0.5) = 1
|
|
225
231
|
*
|
|
226
232
|
* @param {number} x Normalized coordinate on X axis
|
|
227
233
|
* @param {number} [power=1] Parabola power
|
package/lib/prng.d.ts
CHANGED
|
@@ -60,7 +60,7 @@ export declare function random(prng: PRNGParameters): number;
|
|
|
60
60
|
*
|
|
61
61
|
* @param {PRNGParameters} prng PRNG parameters
|
|
62
62
|
* @param {number} [probability=0.5] Probability to get true
|
|
63
|
-
* @returns {boolean} Either
|
|
63
|
+
* @returns {boolean} Either true or false
|
|
64
64
|
*/
|
|
65
65
|
export declare function randomBoolean(prng: PRNGParameters, probability?: number): boolean;
|
|
66
66
|
/**
|
|
@@ -101,8 +101,8 @@ export declare function randomHexColor(prng: PRNGParameters): string;
|
|
|
101
101
|
* Pick a pseudo-random item from a given array
|
|
102
102
|
*
|
|
103
103
|
* @param {PRNGParameters} prng PRNG parameters
|
|
104
|
-
* @param {
|
|
105
|
-
* @returns {
|
|
104
|
+
* @param {unknown[]} array Array to pick the item from
|
|
105
|
+
* @returns {unknown|undefined} Random item picked
|
|
106
106
|
*/
|
|
107
107
|
export declare function randomItem<T = unknown>(prng: PRNGParameters, array: T[]): T | undefined;
|
|
108
108
|
/**
|
|
@@ -110,7 +110,7 @@ export declare function randomItem<T = unknown>(prng: PRNGParameters, array: T[]
|
|
|
110
110
|
*
|
|
111
111
|
* @param {PRNGParameters} prng PRNG parameters
|
|
112
112
|
* @param {object} object Object to pick the property from
|
|
113
|
-
* @returns {
|
|
113
|
+
* @returns {unknown|undefined} Random item picked
|
|
114
114
|
*/
|
|
115
115
|
export declare function randomObjectProperty<T = unknown>(prng: PRNGParameters, object: Record<string, T>): T | undefined;
|
|
116
116
|
/**
|
package/lib/prng.js
CHANGED
|
@@ -129,7 +129,7 @@ export function random(prng) {
|
|
|
129
129
|
*
|
|
130
130
|
* @param {PRNGParameters} prng PRNG parameters
|
|
131
131
|
* @param {number} [probability=0.5] Probability to get true
|
|
132
|
-
* @returns {boolean} Either
|
|
132
|
+
* @returns {boolean} Either true or false
|
|
133
133
|
*/
|
|
134
134
|
export function randomBoolean(prng, probability = 0.5) {
|
|
135
135
|
return random(prng) < probability;
|
|
@@ -180,8 +180,8 @@ export function randomHexColor(prng) {
|
|
|
180
180
|
* Pick a pseudo-random item from a given array
|
|
181
181
|
*
|
|
182
182
|
* @param {PRNGParameters} prng PRNG parameters
|
|
183
|
-
* @param {
|
|
184
|
-
* @returns {
|
|
183
|
+
* @param {unknown[]} array Array to pick the item from
|
|
184
|
+
* @returns {unknown|undefined} Random item picked
|
|
185
185
|
*/
|
|
186
186
|
export function randomItem(prng, array) {
|
|
187
187
|
if (array.length === 0)
|
|
@@ -193,7 +193,7 @@ export function randomItem(prng, array) {
|
|
|
193
193
|
*
|
|
194
194
|
* @param {PRNGParameters} prng PRNG parameters
|
|
195
195
|
* @param {object} object Object to pick the property from
|
|
196
|
-
* @returns {
|
|
196
|
+
* @returns {unknown|undefined} Random item picked
|
|
197
197
|
*/
|
|
198
198
|
export function randomObjectProperty(prng, object) {
|
|
199
199
|
const keys = Object.keys(object);
|
package/lib/random.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { Point2, Point3 } from './types';
|
|
|
3
3
|
* Generate a random boolean (true or false)
|
|
4
4
|
*
|
|
5
5
|
* @param {number} [probability=0.5] Probability to get true
|
|
6
|
-
* @returns {boolean} Either
|
|
6
|
+
* @returns {boolean} Either true or false
|
|
7
7
|
*/
|
|
8
8
|
export declare function randomBoolean(probability?: number): boolean;
|
|
9
9
|
/**
|
|
@@ -39,15 +39,15 @@ export declare function randomHexColor(): string;
|
|
|
39
39
|
/**
|
|
40
40
|
* Pick a random item from a given array
|
|
41
41
|
*
|
|
42
|
-
* @param {
|
|
43
|
-
* @returns {
|
|
42
|
+
* @param {unknown[]} array Array to pick the item from
|
|
43
|
+
* @returns {unknown|undefined} Random item picked
|
|
44
44
|
*/
|
|
45
45
|
export declare function randomItem<T = unknown>(array: T[]): T | undefined;
|
|
46
46
|
/**
|
|
47
47
|
* Pick a random property value from a given object
|
|
48
48
|
*
|
|
49
49
|
* @param {object} object Object to pick the property from
|
|
50
|
-
* @returns {
|
|
50
|
+
* @returns {unknown|undefined} Random item picked
|
|
51
51
|
*/
|
|
52
52
|
export declare function randomObjectProperty<T = unknown>(object: Record<string, T>): T | undefined;
|
|
53
53
|
/**
|
package/lib/random.js
CHANGED
|
@@ -3,7 +3,7 @@ import { Vector2, Vector3 } from './extras/geometry';
|
|
|
3
3
|
* Generate a random boolean (true or false)
|
|
4
4
|
*
|
|
5
5
|
* @param {number} [probability=0.5] Probability to get true
|
|
6
|
-
* @returns {boolean} Either
|
|
6
|
+
* @returns {boolean} Either true or false
|
|
7
7
|
*/
|
|
8
8
|
export function randomBoolean(probability = 0.5) {
|
|
9
9
|
return Math.random() < probability;
|
|
@@ -49,8 +49,8 @@ export function randomHexColor() {
|
|
|
49
49
|
/**
|
|
50
50
|
* Pick a random item from a given array
|
|
51
51
|
*
|
|
52
|
-
* @param {
|
|
53
|
-
* @returns {
|
|
52
|
+
* @param {unknown[]} array Array to pick the item from
|
|
53
|
+
* @returns {unknown|undefined} Random item picked
|
|
54
54
|
*/
|
|
55
55
|
export function randomItem(array) {
|
|
56
56
|
if (array.length === 0)
|
|
@@ -61,7 +61,7 @@ export function randomItem(array) {
|
|
|
61
61
|
* Pick a random property value from a given object
|
|
62
62
|
*
|
|
63
63
|
* @param {object} object Object to pick the property from
|
|
64
|
-
* @returns {
|
|
64
|
+
* @returns {unknown|undefined} Random item picked
|
|
65
65
|
*/
|
|
66
66
|
export function randomObjectProperty(object) {
|
|
67
67
|
const keys = Object.keys(object);
|
package/lib/strings.d.ts
CHANGED
|
@@ -6,42 +6,48 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export declare function capitalize(string: string): string;
|
|
8
8
|
/**
|
|
9
|
-
* Convert a string to kebab-case
|
|
9
|
+
* Convert a string to kebab-case
|
|
10
|
+
* - 'Hello world' -> 'hello-world'
|
|
10
11
|
*
|
|
11
12
|
* @param {string} string String to convert
|
|
12
13
|
* @returns {string} Converted string
|
|
13
14
|
*/
|
|
14
15
|
export declare function toKebabCase(string: string): string;
|
|
15
16
|
/**
|
|
16
|
-
* Convert a string to snake_case
|
|
17
|
+
* Convert a string to snake_case
|
|
18
|
+
* - 'Hello world' -> 'hello_world'
|
|
17
19
|
*
|
|
18
20
|
* @param {string} string String to convert
|
|
19
21
|
* @returns {string} Converted string
|
|
20
22
|
*/
|
|
21
23
|
export declare function toSnakeCase(string: string): string;
|
|
22
24
|
/**
|
|
23
|
-
* Convert a string to camelCase
|
|
25
|
+
* Convert a string to camelCase
|
|
26
|
+
* - 'Hello world' -> 'helloWorld'
|
|
24
27
|
*
|
|
25
28
|
* @param {string} string String to convert
|
|
26
29
|
* @returns {string} Converted string
|
|
27
30
|
*/
|
|
28
31
|
export declare function toCamelCase(string: string): string;
|
|
29
32
|
/**
|
|
30
|
-
* Convert a string to PascalCase
|
|
33
|
+
* Convert a string to PascalCase
|
|
34
|
+
* - 'Hello world' -> 'HelloWorld'
|
|
31
35
|
*
|
|
32
36
|
* @param {string} string String to convert
|
|
33
37
|
* @returns {string} Converted string
|
|
34
38
|
*/
|
|
35
39
|
export declare function toPascalCase(string: string): string;
|
|
36
40
|
/**
|
|
37
|
-
* Convert a string to Train-Case
|
|
41
|
+
* Convert a string to Train-Case
|
|
42
|
+
* - 'Hello world' -> 'Hello-World'
|
|
38
43
|
*
|
|
39
44
|
* @param {string} string String to convert
|
|
40
45
|
* @returns {string} Converted string
|
|
41
46
|
*/
|
|
42
47
|
export declare function toTrainCase(string: string): string;
|
|
43
48
|
/**
|
|
44
|
-
* Convert a string to CONSTANT_CASE
|
|
49
|
+
* Convert a string to CONSTANT_CASE
|
|
50
|
+
* - 'Hello world' -> 'HELLO_WORLD'
|
|
45
51
|
*
|
|
46
52
|
* @param {string} string String to convert
|
|
47
53
|
* @returns {string} Converted string
|
|
@@ -58,13 +64,13 @@ export declare function cleanPath(path: string): string;
|
|
|
58
64
|
* Convert a path by ensuring it has a trailing slash
|
|
59
65
|
*
|
|
60
66
|
* @param {string} path Path to convert
|
|
61
|
-
* @returns {string}
|
|
67
|
+
* @returns {string} Converted path
|
|
62
68
|
*/
|
|
63
69
|
export declare function addTrailingSlash(path: string): string;
|
|
64
70
|
/**
|
|
65
71
|
* Convert a path by ensuring it has not a trailing slash
|
|
66
72
|
*
|
|
67
73
|
* @param {string} path Path to convert
|
|
68
|
-
* @returns {string}
|
|
74
|
+
* @returns {string} Converted path
|
|
69
75
|
*/
|
|
70
76
|
export declare function removeTrailingSlash(path: string): string;
|
package/lib/strings.js
CHANGED
|
@@ -11,7 +11,8 @@ export function capitalize(string) {
|
|
|
11
11
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
|
-
* Convert a string to kebab-case
|
|
14
|
+
* Convert a string to kebab-case
|
|
15
|
+
* - 'Hello world' -> 'hello-world'
|
|
15
16
|
*
|
|
16
17
|
* @param {string} string String to convert
|
|
17
18
|
* @returns {string} Converted string
|
|
@@ -25,7 +26,8 @@ export function toKebabCase(string) {
|
|
|
25
26
|
.toLowerCase());
|
|
26
27
|
}
|
|
27
28
|
/**
|
|
28
|
-
* Convert a string to snake_case
|
|
29
|
+
* Convert a string to snake_case
|
|
30
|
+
* - 'Hello world' -> 'hello_world'
|
|
29
31
|
*
|
|
30
32
|
* @param {string} string String to convert
|
|
31
33
|
* @returns {string} Converted string
|
|
@@ -39,7 +41,8 @@ export function toSnakeCase(string) {
|
|
|
39
41
|
.toLowerCase());
|
|
40
42
|
}
|
|
41
43
|
/**
|
|
42
|
-
* Convert a string to camelCase
|
|
44
|
+
* Convert a string to camelCase
|
|
45
|
+
* - 'Hello world' -> 'helloWorld'
|
|
43
46
|
*
|
|
44
47
|
* @param {string} string String to convert
|
|
45
48
|
* @returns {string} Converted string
|
|
@@ -52,7 +55,8 @@ export function toCamelCase(string) {
|
|
|
52
55
|
.replace(/^([A-Z])/, (c) => c.toLowerCase()));
|
|
53
56
|
}
|
|
54
57
|
/**
|
|
55
|
-
* Convert a string to PascalCase
|
|
58
|
+
* Convert a string to PascalCase
|
|
59
|
+
* - 'Hello world' -> 'HelloWorld'
|
|
56
60
|
*
|
|
57
61
|
* @param {string} string String to convert
|
|
58
62
|
* @returns {string} Converted string
|
|
@@ -65,7 +69,8 @@ export function toPascalCase(string) {
|
|
|
65
69
|
.replace(/^([a-z])/, (c) => c.toUpperCase()));
|
|
66
70
|
}
|
|
67
71
|
/**
|
|
68
|
-
* Convert a string to Train-Case
|
|
72
|
+
* Convert a string to Train-Case
|
|
73
|
+
* - 'Hello world' -> 'Hello-World'
|
|
69
74
|
*
|
|
70
75
|
* @param {string} string String to convert
|
|
71
76
|
* @returns {string} Converted string
|
|
@@ -81,7 +86,8 @@ export function toTrainCase(string) {
|
|
|
81
86
|
.replace(/\b\w/g, (c) => c.toUpperCase()));
|
|
82
87
|
}
|
|
83
88
|
/**
|
|
84
|
-
* Convert a string to CONSTANT_CASE
|
|
89
|
+
* Convert a string to CONSTANT_CASE
|
|
90
|
+
* - 'Hello world' -> 'HELLO_WORLD'
|
|
85
91
|
*
|
|
86
92
|
* @param {string} string String to convert
|
|
87
93
|
* @returns {string} Converted string
|
|
@@ -110,7 +116,7 @@ export function cleanPath(path) {
|
|
|
110
116
|
* Convert a path by ensuring it has a trailing slash
|
|
111
117
|
*
|
|
112
118
|
* @param {string} path Path to convert
|
|
113
|
-
* @returns {string}
|
|
119
|
+
* @returns {string} Converted path
|
|
114
120
|
*/
|
|
115
121
|
export function addTrailingSlash(path) {
|
|
116
122
|
if (path.match(/\.[a-z]{2,4}$/i) || path.match(/^mailto:/) || path.endsWith('/'))
|
|
@@ -121,7 +127,7 @@ export function addTrailingSlash(path) {
|
|
|
121
127
|
* Convert a path by ensuring it has not a trailing slash
|
|
122
128
|
*
|
|
123
129
|
* @param {string} path Path to convert
|
|
124
|
-
* @returns {string}
|
|
130
|
+
* @returns {string} Converted path
|
|
125
131
|
*/
|
|
126
132
|
export function removeTrailingSlash(path) {
|
|
127
133
|
if (path.endsWith('/'))
|