@naturalcycles/js-lib 15.108.0 → 15.109.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/dist/abort.d.ts +6 -0
- package/dist/abort.js +18 -1
- package/dist/array/array.util.js +7 -3
- package/dist/array/sort.js +1 -1
- package/dist/datetime/localDate.js +1 -1
- package/dist/datetime/localTime.js +1 -1
- package/dist/http/fetch.js +2 -1
- package/dist/math/math.util.js +3 -3
- package/dist/semver.js +1 -1
- package/package.json +2 -2
- package/src/abort.ts +20 -1
- package/src/array/array.util.ts +7 -3
- package/src/array/sort.ts +1 -1
- package/src/datetime/localDate.ts +1 -1
- package/src/datetime/localTime.ts +1 -1
- package/src/http/fetch.ts +2 -1
- package/src/math/math.util.ts +3 -3
- package/src/semver.ts +1 -1
package/dist/abort.d.ts
CHANGED
|
@@ -28,6 +28,12 @@ export declare function createAbortableSignal(): AbortableSignal;
|
|
|
28
28
|
* Otherwise returns undefined.
|
|
29
29
|
*/
|
|
30
30
|
export declare function abortSignalTimeoutOrUndefined(ms: NumberOfMilliseconds | undefined): AbortSignal | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Returns an AbortSignal that aborts after `ms` with a "TimeoutError" DOMException.
|
|
33
|
+
* Uses native `AbortSignal.timeout()` when available, falls back to a polyfill.
|
|
34
|
+
*/
|
|
35
|
+
export declare function abortSignalTimeout(ms: NumberOfMilliseconds): AbortSignal;
|
|
36
|
+
export declare function polyfilledAbortSignalTimeout(ms: NumberOfMilliseconds): AbortSignal;
|
|
31
37
|
/**
|
|
32
38
|
* Returns AbortSignal.any(signals) is the array (after filtering undefined inputs) is not empty,
|
|
33
39
|
* otherwise undefined.
|
package/dist/abort.js
CHANGED
|
@@ -16,7 +16,24 @@ export function createAbortableSignal() {
|
|
|
16
16
|
* Otherwise returns undefined.
|
|
17
17
|
*/
|
|
18
18
|
export function abortSignalTimeoutOrUndefined(ms) {
|
|
19
|
-
return ms ?
|
|
19
|
+
return ms ? abortSignalTimeout(ms) : undefined;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Returns an AbortSignal that aborts after `ms` with a "TimeoutError" DOMException.
|
|
23
|
+
* Uses native `AbortSignal.timeout()` when available, falls back to a polyfill.
|
|
24
|
+
*/
|
|
25
|
+
export function abortSignalTimeout(ms) {
|
|
26
|
+
// AbortSignal.timeout needs Chrome 103+ / Safari 16+, below our browser floor (Chrome 110, iOS 16.7),
|
|
27
|
+
// but Fetcher calls it on every request and browsers like iOS 15 still visit our sites
|
|
28
|
+
return typeof AbortSignal.timeout === 'function'
|
|
29
|
+
? AbortSignal.timeout(ms)
|
|
30
|
+
: polyfilledAbortSignalTimeout(ms);
|
|
31
|
+
}
|
|
32
|
+
export function polyfilledAbortSignalTimeout(ms) {
|
|
33
|
+
const ac = new AbortController();
|
|
34
|
+
// Same reason as the native one, Fetcher tells a timeout from an abort by its name
|
|
35
|
+
setTimeout(() => ac.abort(new DOMException('signal timed out', 'TimeoutError')), ms);
|
|
36
|
+
return ac.signal;
|
|
20
37
|
}
|
|
21
38
|
/**
|
|
22
39
|
* Returns AbortSignal.any(signals) is the array (after filtering undefined inputs) is not empty,
|
package/dist/array/array.util.js
CHANGED
|
@@ -177,7 +177,7 @@ export function _find(items, predicate) {
|
|
|
177
177
|
* Use `Array.findLast` if you don't need to stop the iteration early.
|
|
178
178
|
*/
|
|
179
179
|
export function _findLast(items, predicate) {
|
|
180
|
-
return _find(items.
|
|
180
|
+
return _find(items.slice().reverse(), predicate);
|
|
181
181
|
}
|
|
182
182
|
export function _takeWhile(items, predicate) {
|
|
183
183
|
let proceed = true;
|
|
@@ -185,7 +185,10 @@ export function _takeWhile(items, predicate) {
|
|
|
185
185
|
}
|
|
186
186
|
export function _takeRightWhile(items, predicate) {
|
|
187
187
|
let proceed = true;
|
|
188
|
-
return items
|
|
188
|
+
return items
|
|
189
|
+
.slice()
|
|
190
|
+
.reverse()
|
|
191
|
+
.filter((v, index) => (proceed &&= predicate(v, index)));
|
|
189
192
|
}
|
|
190
193
|
export function _dropWhile(items, predicate) {
|
|
191
194
|
let proceed = false;
|
|
@@ -194,7 +197,8 @@ export function _dropWhile(items, predicate) {
|
|
|
194
197
|
export function _dropRightWhile(items, predicate) {
|
|
195
198
|
let proceed = false;
|
|
196
199
|
return items
|
|
197
|
-
.
|
|
200
|
+
.slice()
|
|
201
|
+
.reverse()
|
|
198
202
|
.filter((v, index) => (proceed ||= !predicate(v, index)))
|
|
199
203
|
.reverse();
|
|
200
204
|
}
|
package/dist/array/sort.js
CHANGED
|
@@ -52,7 +52,7 @@ export const comparators = /* @__PURE__ */ new Comparators();
|
|
|
52
52
|
*/
|
|
53
53
|
export function _sortBy(items, mapper, opt = {}) {
|
|
54
54
|
const cmp = comparators.by(mapper, opt);
|
|
55
|
-
return opt.mutate ? items.sort(cmp) : items.
|
|
55
|
+
return opt.mutate ? items.sort(cmp) : items.slice().sort(cmp);
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
58
58
|
* Like _stringMapValues, but values are sorted.
|
|
@@ -622,7 +622,7 @@ class LocalDateFactory {
|
|
|
622
622
|
function cmp(a, b) {
|
|
623
623
|
return a.compare(b) * mod;
|
|
624
624
|
}
|
|
625
|
-
return opt.mutate ? items.sort(cmp) : items.
|
|
625
|
+
return opt.mutate ? items.sort(cmp) : items.slice().sort(cmp);
|
|
626
626
|
}
|
|
627
627
|
/**
|
|
628
628
|
* Returns the earliest (min) LocalDate from the array, or undefined if the array is empty.
|
|
@@ -876,7 +876,7 @@ class LocalTimeFactory {
|
|
|
876
876
|
}
|
|
877
877
|
sort(items, dir = 'asc', opt = {}) {
|
|
878
878
|
const cmp = comparators.by((lt) => lt.$date.valueOf(), { dir });
|
|
879
|
-
return opt.mutate ? items.sort(cmp) : items.
|
|
879
|
+
return opt.mutate ? items.sort(cmp) : items.slice().sort(cmp);
|
|
880
880
|
}
|
|
881
881
|
minOrUndefined(items) {
|
|
882
882
|
let min;
|
package/dist/http/fetch.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference lib="dom" preserve="true" />
|
|
2
|
+
import { abortSignalTimeoutOrUndefined } from '../abort.js';
|
|
2
3
|
/**
|
|
3
4
|
* Minimal wrapper around native `fetch`:
|
|
4
5
|
*
|
|
@@ -24,7 +25,7 @@ export async function _fetch(url, opt = {}) {
|
|
|
24
25
|
...init,
|
|
25
26
|
method,
|
|
26
27
|
// A fresh signal per attempt, as a timed-out signal stays aborted
|
|
27
|
-
signal:
|
|
28
|
+
signal: abortSignalTimeoutOrUndefined(timeout),
|
|
28
29
|
});
|
|
29
30
|
if (res.ok) {
|
|
30
31
|
const text = await res.text();
|
package/dist/math/math.util.js
CHANGED
|
@@ -70,7 +70,7 @@ export function _averageWeighted(values, weights) {
|
|
|
70
70
|
* // 3
|
|
71
71
|
*/
|
|
72
72
|
export function _percentile(values, pc) {
|
|
73
|
-
const sorted = values.
|
|
73
|
+
const sorted = values.slice().sort(comparators.numericAsc);
|
|
74
74
|
// Floating pos in the range of [0; length - 1]
|
|
75
75
|
const pos = ((values.length - 1) * pc) / 100;
|
|
76
76
|
const dec = pos % 1;
|
|
@@ -83,7 +83,7 @@ export function _percentile(values, pc) {
|
|
|
83
83
|
*/
|
|
84
84
|
export function _percentiles(values, pcs) {
|
|
85
85
|
const r = {};
|
|
86
|
-
const sorted = values.
|
|
86
|
+
const sorted = values.slice().sort(comparators.numericAsc);
|
|
87
87
|
for (const pc of pcs) {
|
|
88
88
|
// Floating pos in the range of [0; length - 1]
|
|
89
89
|
const pos = ((values.length - 1) * pc) / 100;
|
|
@@ -104,7 +104,7 @@ export function _percentiles(values, pcs) {
|
|
|
104
104
|
* // 2.5
|
|
105
105
|
*/
|
|
106
106
|
export function _median(values) {
|
|
107
|
-
const sorted = values.
|
|
107
|
+
const sorted = values.slice().sort(comparators.numericAsc);
|
|
108
108
|
const len = sorted.length;
|
|
109
109
|
const mid = Math.floor(len / 2);
|
|
110
110
|
return len % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
|
package/dist/semver.js
CHANGED
|
@@ -125,7 +125,7 @@ class SemverFactory {
|
|
|
125
125
|
function cmp(a, b) {
|
|
126
126
|
return a.compare(b) * mod;
|
|
127
127
|
}
|
|
128
|
-
return opt.mutate ? items.sort(cmp) : items.
|
|
128
|
+
return opt.mutate ? items.sort(cmp) : items.slice().sort(cmp);
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
131
|
// Built inside a function, so the whole construct can be marked pure and tree-shaken (see treeshake.test.ts)
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "15.
|
|
4
|
+
"version": "15.109.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"tslib": "^2"
|
|
7
7
|
},
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@naturalcycles/dev-lib": "20.
|
|
17
|
+
"@naturalcycles/dev-lib": "20.81.0",
|
|
18
18
|
"@types/node": "^26",
|
|
19
19
|
"@types/semver": "^7",
|
|
20
20
|
"rolldown": "^1",
|
package/src/abort.ts
CHANGED
|
@@ -39,7 +39,26 @@ export function createAbortableSignal(): AbortableSignal {
|
|
|
39
39
|
export function abortSignalTimeoutOrUndefined(
|
|
40
40
|
ms: NumberOfMilliseconds | undefined,
|
|
41
41
|
): AbortSignal | undefined {
|
|
42
|
-
return ms ?
|
|
42
|
+
return ms ? abortSignalTimeout(ms) : undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Returns an AbortSignal that aborts after `ms` with a "TimeoutError" DOMException.
|
|
47
|
+
* Uses native `AbortSignal.timeout()` when available, falls back to a polyfill.
|
|
48
|
+
*/
|
|
49
|
+
export function abortSignalTimeout(ms: NumberOfMilliseconds): AbortSignal {
|
|
50
|
+
// AbortSignal.timeout needs Chrome 103+ / Safari 16+, below our browser floor (Chrome 110, iOS 16.7),
|
|
51
|
+
// but Fetcher calls it on every request and browsers like iOS 15 still visit our sites
|
|
52
|
+
return typeof AbortSignal.timeout === 'function'
|
|
53
|
+
? AbortSignal.timeout(ms)
|
|
54
|
+
: polyfilledAbortSignalTimeout(ms)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function polyfilledAbortSignalTimeout(ms: NumberOfMilliseconds): AbortSignal {
|
|
58
|
+
const ac = new AbortController()
|
|
59
|
+
// Same reason as the native one, Fetcher tells a timeout from an abort by its name
|
|
60
|
+
setTimeout(() => ac.abort(new DOMException('signal timed out', 'TimeoutError')), ms)
|
|
61
|
+
return ac.signal
|
|
43
62
|
}
|
|
44
63
|
|
|
45
64
|
/**
|
package/src/array/array.util.ts
CHANGED
|
@@ -198,7 +198,7 @@ export function _find<T>(items: readonly T[], predicate: AbortablePredicate<T>):
|
|
|
198
198
|
* Use `Array.findLast` if you don't need to stop the iteration early.
|
|
199
199
|
*/
|
|
200
200
|
export function _findLast<T>(items: readonly T[], predicate: AbortablePredicate<T>): T | undefined {
|
|
201
|
-
return _find(items.
|
|
201
|
+
return _find(items.slice().reverse(), predicate)
|
|
202
202
|
}
|
|
203
203
|
|
|
204
204
|
export function _takeWhile<T>(items: readonly T[], predicate: Predicate<T>): T[] {
|
|
@@ -208,7 +208,10 @@ export function _takeWhile<T>(items: readonly T[], predicate: Predicate<T>): T[]
|
|
|
208
208
|
|
|
209
209
|
export function _takeRightWhile<T>(items: readonly T[], predicate: Predicate<T>): T[] {
|
|
210
210
|
let proceed = true
|
|
211
|
-
return items
|
|
211
|
+
return items
|
|
212
|
+
.slice()
|
|
213
|
+
.reverse()
|
|
214
|
+
.filter((v, index) => (proceed &&= predicate(v, index)))
|
|
212
215
|
}
|
|
213
216
|
|
|
214
217
|
export function _dropWhile<T>(items: readonly T[], predicate: Predicate<T>): T[] {
|
|
@@ -219,7 +222,8 @@ export function _dropWhile<T>(items: readonly T[], predicate: Predicate<T>): T[]
|
|
|
219
222
|
export function _dropRightWhile<T>(items: readonly T[], predicate: Predicate<T>): T[] {
|
|
220
223
|
let proceed = false
|
|
221
224
|
return items
|
|
222
|
-
.
|
|
225
|
+
.slice()
|
|
226
|
+
.reverse()
|
|
223
227
|
.filter((v, index) => (proceed ||= !predicate(v, index)))
|
|
224
228
|
.reverse()
|
|
225
229
|
}
|
package/src/array/sort.ts
CHANGED
|
@@ -77,7 +77,7 @@ export function _sortBy<T, COMPARE_TYPE extends string | number>(
|
|
|
77
77
|
opt: SortOptions = {},
|
|
78
78
|
): T[] {
|
|
79
79
|
const cmp = comparators.by(mapper, opt)
|
|
80
|
-
return opt.mutate ? items.sort(cmp) : items.
|
|
80
|
+
return opt.mutate ? items.sort(cmp) : items.slice().sort(cmp)
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
/**
|
|
@@ -1027,7 +1027,7 @@ class LocalTimeFactory {
|
|
|
1027
1027
|
|
|
1028
1028
|
sort(items: LocalTime[], dir: SortDirection = 'asc', opt: MutateOptions = {}): LocalTime[] {
|
|
1029
1029
|
const cmp = comparators.by((lt: LocalTime) => lt.$date.valueOf(), { dir })
|
|
1030
|
-
return opt.mutate ? items.sort(cmp) : items.
|
|
1030
|
+
return opt.mutate ? items.sort(cmp) : items.slice().sort(cmp)
|
|
1031
1031
|
}
|
|
1032
1032
|
|
|
1033
1033
|
minOrUndefined(items: readonly LocalTimeInputNullable[]): LocalTime | undefined {
|
package/src/http/fetch.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference lib="dom" preserve="true" />
|
|
2
2
|
|
|
3
|
+
import { abortSignalTimeoutOrUndefined } from '../abort.js'
|
|
3
4
|
import type { HttpRequestErrorData } from '../error/error.model.js'
|
|
4
5
|
import type { NumberOfMilliseconds } from '../types.js'
|
|
5
6
|
import type { HttpMethod } from './http.model.js'
|
|
@@ -30,7 +31,7 @@ export async function _fetch<T = unknown>(url: string, opt: FetchOptions = {}):
|
|
|
30
31
|
...init,
|
|
31
32
|
method,
|
|
32
33
|
// A fresh signal per attempt, as a timed-out signal stays aborted
|
|
33
|
-
signal:
|
|
34
|
+
signal: abortSignalTimeoutOrUndefined(timeout),
|
|
34
35
|
})
|
|
35
36
|
if (res.ok) {
|
|
36
37
|
const text = await res.text()
|
package/src/math/math.util.ts
CHANGED
|
@@ -75,7 +75,7 @@ export function _averageWeighted(values: readonly number[], weights: readonly nu
|
|
|
75
75
|
* // 3
|
|
76
76
|
*/
|
|
77
77
|
export function _percentile(values: readonly number[], pc: number): number {
|
|
78
|
-
const sorted = values.
|
|
78
|
+
const sorted = values.slice().sort(comparators.numericAsc)
|
|
79
79
|
|
|
80
80
|
// Floating pos in the range of [0; length - 1]
|
|
81
81
|
const pos = ((values.length - 1) * pc) / 100
|
|
@@ -95,7 +95,7 @@ export function _percentiles(
|
|
|
95
95
|
): Record<number, number> {
|
|
96
96
|
const r = {} as Record<number, number>
|
|
97
97
|
|
|
98
|
-
const sorted = values.
|
|
98
|
+
const sorted = values.slice().sort(comparators.numericAsc)
|
|
99
99
|
|
|
100
100
|
for (const pc of pcs) {
|
|
101
101
|
// Floating pos in the range of [0; length - 1]
|
|
@@ -120,7 +120,7 @@ export function _percentiles(
|
|
|
120
120
|
* // 2.5
|
|
121
121
|
*/
|
|
122
122
|
export function _median(values: readonly number[]): number {
|
|
123
|
-
const sorted = values.
|
|
123
|
+
const sorted = values.slice().sort(comparators.numericAsc)
|
|
124
124
|
const len = sorted.length
|
|
125
125
|
const mid = Math.floor(len / 2)
|
|
126
126
|
return len % 2 ? sorted[mid]! : (sorted[mid - 1]! + sorted[mid]!) / 2
|