toosoon-utils 3.1.0 → 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/lib/files.js CHANGED
@@ -1,13 +1,10 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.upload = exports.download = void 0;
4
1
  /**
5
2
  * Download a Blob object into user files
6
3
  *
7
4
  * @param {Blob} blob Blob object to download
8
5
  * @param {string} filename Downloaded file name
9
6
  */
10
- function download(blob, filename) {
7
+ export function download(blob, filename) {
11
8
  var link = document.createElement('a');
12
9
  link.setAttribute('href', URL.createObjectURL(blob));
13
10
  link.setAttribute('download', filename);
@@ -15,14 +12,13 @@ function download(blob, filename) {
15
12
  link.click();
16
13
  document.body.removeChild(link);
17
14
  }
18
- exports.download = download;
19
15
  /**
20
16
  * Upload a file from user files
21
17
  *
22
18
  * @param {Function} onLoad Callback called once the file is loaded
23
19
  * @param {string} [accept=''] MIME type the file input should accept
24
20
  */
25
- function upload(onLoad, accept) {
21
+ export function upload(onLoad, accept) {
26
22
  if (accept === void 0) { accept = ''; }
27
23
  var input = document.createElement('input');
28
24
  input.setAttribute('type', 'file');
@@ -40,4 +36,3 @@ function upload(onLoad, accept) {
40
36
  input.click();
41
37
  document.body.removeChild(input);
42
38
  }
43
- exports.upload = upload;
package/lib/functions.js CHANGED
@@ -1,22 +1,17 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.now = exports.defer = exports.throttle = exports.debounce = exports.wait = exports.noop = void 0;
4
1
  /**
5
2
  * No-op function
6
3
  */
7
- var noop = function () { };
8
- exports.noop = noop;
4
+ export var noop = function () { };
9
5
  /**
10
6
  * Promise wrapped setTimeout
11
7
  *
12
8
  * @param {number} [delay=0] Time to wait (in milliseconds)
13
9
  * @returns {Promise}
14
10
  */
15
- function wait(delay) {
11
+ export function wait(delay) {
16
12
  if (delay === void 0) { delay = 0; }
17
13
  return new Promise(function (resolve) { return setTimeout(resolve, delay); });
18
14
  }
19
- exports.wait = wait;
20
15
  /**
21
16
  * Create a debounced function that delays the execution of `callback` until a specified `delay` time has passed since the last call
22
17
  *
@@ -24,7 +19,7 @@ exports.wait = wait;
24
19
  * @param {number} delay Delay (in milliseconds)
25
20
  * @returns {Function} Debounced function
26
21
  */
27
- function debounce(callback, delay) {
22
+ export function debounce(callback, delay) {
28
23
  var timeout = null;
29
24
  return function () {
30
25
  var args = [];
@@ -36,7 +31,6 @@ function debounce(callback, delay) {
36
31
  timeout = setTimeout(function () { return callback.apply(void 0, args); }, delay);
37
32
  };
38
33
  }
39
- exports.debounce = debounce;
40
34
  /**
41
35
  * Create a throttled function that limits the execution of `callback` to once every `limit` time
42
36
  *
@@ -44,27 +38,26 @@ exports.debounce = debounce;
44
38
  * @param {number} limit Minimum interval between two calls (in milliseconds)
45
39
  * @returns {Function} Throttled function
46
40
  */
47
- function throttle(callback, limit) {
41
+ export function throttle(callback, limit) {
48
42
  var lastTime = 0;
49
43
  return function () {
50
44
  var args = [];
51
45
  for (var _i = 0; _i < arguments.length; _i++) {
52
46
  args[_i] = arguments[_i];
53
47
  }
54
- var time = (0, exports.now)();
48
+ var time = now();
55
49
  if (time - lastTime >= limit) {
56
50
  lastTime = time;
57
51
  callback.apply(void 0, args);
58
52
  }
59
53
  };
60
54
  }
61
- exports.throttle = throttle;
62
55
  /**
63
56
  * Deferred promise implementation
64
57
  *
65
58
  * @returns {Deferred}
66
59
  */
67
- function defer() {
60
+ export function defer() {
68
61
  var resolve;
69
62
  var reject;
70
63
  var promise = new Promise(function (_resolve, _reject) {
@@ -73,10 +66,13 @@ function defer() {
73
66
  });
74
67
  return { promise: promise, resolve: resolve, reject: reject };
75
68
  }
76
- exports.defer = defer;
69
+ /**
70
+ * Polyfill for `now()` functions
71
+ */
72
+ export var now;
77
73
  // In node.js, use `process.hrtime`
78
74
  if (typeof process !== 'undefined' && process.hrtime) {
79
- exports.now = function () {
75
+ now = function () {
80
76
  // Convert [seconds, nanoseconds] to milliseconds
81
77
  var time = process.hrtime();
82
78
  return time[0] * 1000 + time[1] / 1000000;
@@ -85,13 +81,13 @@ if (typeof process !== 'undefined' && process.hrtime) {
85
81
  // In a browser use `performance` or `Date`
86
82
  else if (typeof performance !== 'undefined') {
87
83
  // This must be bound, because directly assigning this function leads to an invocation exception in Chrome
88
- exports.now = performance.now.bind(performance);
84
+ now = performance.now.bind(performance);
89
85
  }
90
86
  else if (typeof Date.now !== 'undefined') {
91
- exports.now = Date.now;
87
+ now = Date.now;
92
88
  }
93
89
  else {
94
- exports.now = function () {
90
+ now = function () {
95
91
  return new Date().getTime();
96
92
  };
97
93
  }
package/lib/geometry.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Vector3 } from './types';
1
+ import type { Vector3 } from './types';
2
2
  /**
3
3
  * Convert a radians value into degrees
4
4
  *
package/lib/geometry.js CHANGED
@@ -1,27 +1,22 @@
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");
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) / constants_1.PI;
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 * constants_1.PI) / 180;
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 > constants_1.PI ? target - 2 * constants_1.PI : target < -constants_1.PI ? delta + 2 * constants_1.PI : target;
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;