@naturalcycles/js-lib 15.107.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 +10 -4
- package/dist/array/sort.js +1 -1
- package/dist/browser/adminService.js +1 -1
- package/dist/datetime/localDate.js +4 -2
- package/dist/datetime/localTime.js +1 -1
- package/dist/decorators/asyncMemo.decorator.d.ts +1 -1
- package/dist/decorators/asyncMemo.decorator.js +88 -86
- package/dist/decorators/memo.decorator.d.ts +1 -1
- package/dist/decorators/memo.decorator.js +62 -60
- package/dist/decorators/memoSimple.decorator.d.ts +1 -1
- package/dist/decorators/memoSimple.decorator.js +39 -37
- package/dist/decorators/memoized.js +2 -2
- package/dist/decorators/memoizedAsync.js +2 -2
- package/dist/decorators/swarmSafe.decorator.d.ts +1 -1
- package/dist/decorators/swarmSafe.decorator.js +28 -26
- package/dist/define.js +2 -2
- package/dist/error/tryCatch.d.ts +1 -1
- package/dist/error/tryCatch.js +7 -5
- package/dist/http/fetch.js +2 -1
- package/dist/is.util.d.ts +6 -6
- package/dist/is.util.js +18 -6
- package/dist/log/commonLogger.js +6 -2
- package/dist/math/math.util.js +3 -3
- package/dist/object/keySortedMap.js +1 -0
- package/dist/object/lazyKeySortedMap.js +1 -0
- package/dist/promise/pMap.js +2 -2
- package/dist/qr/qr.js +3 -1
- package/dist/semver.js +4 -2
- package/dist/string/slugify.js +4 -6
- package/dist/types.d.ts +1 -1
- package/dist/types.js +3 -1
- package/package.json +2 -2
- package/src/abort.ts +20 -1
- package/src/array/array.util.ts +10 -4
- package/src/array/sort.ts +1 -1
- package/src/browser/adminService.ts +2 -1
- package/src/datetime/localDate.ts +6 -2
- package/src/datetime/localTime.ts +1 -1
- package/src/decorators/asyncMemo.decorator.ts +3 -3
- package/src/decorators/memo.decorator.ts +3 -3
- package/src/decorators/memoSimple.decorator.ts +14 -14
- package/src/decorators/memoized.ts +1 -1
- package/src/decorators/memoizedAsync.ts +1 -1
- package/src/decorators/swarmSafe.decorator.ts +29 -27
- package/src/define.ts +1 -1
- package/src/error/tryCatch.ts +3 -3
- package/src/http/fetch.ts +2 -1
- package/src/is.util.ts +22 -6
- package/src/log/commonLogger.ts +5 -2
- package/src/math/math.util.ts +3 -3
- package/src/object/keySortedMap.ts +1 -0
- package/src/object/lazyKeySortedMap.ts +1 -0
- package/src/promise/pMap.ts +1 -1
- package/src/qr/qr.ts +5 -1
- package/src/semver.ts +6 -2
- package/src/string/slugify.ts +7 -9
- package/src/types.ts +3 -1
package/src/define.ts
CHANGED
|
@@ -44,7 +44,7 @@ export function _defineLazyProperty<OBJ extends AnyObject>(
|
|
|
44
44
|
propertyName: keyof OBJ,
|
|
45
45
|
fn: AnyFunction,
|
|
46
46
|
): OBJ {
|
|
47
|
-
|
|
47
|
+
function define(value: any): void {
|
|
48
48
|
Object.defineProperty(obj, propertyName, { value, enumerable: true, writable: true })
|
|
49
49
|
}
|
|
50
50
|
|
package/src/error/tryCatch.ts
CHANGED
|
@@ -66,10 +66,10 @@ export function _tryCatch<T extends AnyFunction>(fn: T, opt: TryCatchOptions = {
|
|
|
66
66
|
} as any
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
export
|
|
70
|
-
(
|
|
71
|
-
(_target, _key, descriptor) => {
|
|
69
|
+
export function _TryCatch(opt: TryCatchOptions = {}): MethodDecorator {
|
|
70
|
+
return (_target, _key, descriptor) => {
|
|
72
71
|
const originalFn = descriptor.value
|
|
73
72
|
descriptor.value = _tryCatch(originalFn as any, opt)
|
|
74
73
|
return descriptor
|
|
75
74
|
}
|
|
75
|
+
}
|
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/is.util.ts
CHANGED
|
@@ -4,10 +4,21 @@ type Nullish<T> = T extends NullishValue ? T : never
|
|
|
4
4
|
type Truthy<T> = T extends FalsyValue ? never : T
|
|
5
5
|
type Falsy<T> = T extends FalsyValue ? T : never
|
|
6
6
|
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
export function _isNull<T>(v: T): v is T extends null ? T : never {
|
|
8
|
+
return v === null
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function _isUndefined<T>(v: T): v is T extends undefined ? T : never {
|
|
12
|
+
return v === undefined
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function _isNullish<T>(v: T): v is Nullish<T> {
|
|
16
|
+
return v === undefined || v === null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function _isNotNullish<T>(v: T): v is NonNullable<T> {
|
|
20
|
+
return v !== undefined && v !== null
|
|
21
|
+
}
|
|
11
22
|
|
|
12
23
|
/**
|
|
13
24
|
* Same as Boolean, but with correct type output.
|
|
@@ -20,8 +31,13 @@ export const _isNotNullish = <T>(v: T): v is NonNullable<T> => v !== undefined &
|
|
|
20
31
|
* [1, 2, undefined].filter(_isTruthy)
|
|
21
32
|
* // => [1, 2]
|
|
22
33
|
*/
|
|
23
|
-
export
|
|
24
|
-
|
|
34
|
+
export function _isTruthy<T>(v: T): v is Truthy<T> {
|
|
35
|
+
return !!v
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function _isFalsy<T>(v: T): v is Falsy<T> {
|
|
39
|
+
return !v
|
|
40
|
+
}
|
|
25
41
|
|
|
26
42
|
/**
|
|
27
43
|
* Returns true if item is Object, not null and not Array.
|
package/src/log/commonLogger.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// copy-pasted to avoid weird circular dependency
|
|
2
|
-
|
|
2
|
+
function _noop(..._args: any[]): undefined {
|
|
3
|
+
return undefined
|
|
4
|
+
}
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* These levels follow console.* naming,
|
|
@@ -76,8 +78,9 @@ export interface CommonLogger {
|
|
|
76
78
|
* after the logger is created (e.g a request context whose user is resolved later).
|
|
77
79
|
*/
|
|
78
80
|
export function commonLoggerCreate(sink: LogSink, context?: () => LogData): CommonLogger {
|
|
79
|
-
|
|
81
|
+
function emit(level: CommonLogLevel, [input, data]: CommonLogArgs): void {
|
|
80
82
|
sink({ ...context?.(), ...toLogData(input, data), level })
|
|
83
|
+
}
|
|
81
84
|
|
|
82
85
|
return {
|
|
83
86
|
debug: (...args) => emit('debug', args),
|
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
|
|
@@ -25,6 +25,7 @@ export interface KeySortedMapOptions<K> {
|
|
|
25
25
|
// oxlint-disable-next-line no-unsafe-declaration-merging -- Map<K,V> workaround for oxlint TS2420 false positive
|
|
26
26
|
export interface KeySortedMap<K, V> extends Map<K, V> {}
|
|
27
27
|
|
|
28
|
+
// oxlint-disable-next-line no-unsafe-declaration-merging -- see the interface above
|
|
28
29
|
export class KeySortedMap<K, V> {
|
|
29
30
|
private readonly map: Map<K, V>
|
|
30
31
|
readonly #sortedKeys: K[]
|
|
@@ -20,6 +20,7 @@ export interface LazyKeySortedMapOptions<K> {
|
|
|
20
20
|
// oxlint-disable-next-line no-unsafe-declaration-merging -- Map<K,V> workaround for oxlint TS2420 false positive
|
|
21
21
|
export interface LazyKeySortedMap<K, V> extends Map<K, V> {}
|
|
22
22
|
|
|
23
|
+
// oxlint-disable-next-line no-unsafe-declaration-merging -- see the interface above
|
|
23
24
|
export class LazyKeySortedMap<K, V> {
|
|
24
25
|
private readonly map: Map<K, V>
|
|
25
26
|
private readonly maybeSortedKeys: K[]
|
package/src/promise/pMap.ts
CHANGED
package/src/qr/qr.ts
CHANGED
|
@@ -543,7 +543,11 @@ function lostPointBlocks(modules: boolean[][]): number {
|
|
|
543
543
|
function lostPointFinderLike(modules: boolean[][]): number {
|
|
544
544
|
const size = modules.length
|
|
545
545
|
let lostPoint = 0
|
|
546
|
-
|
|
546
|
+
|
|
547
|
+
function isDark(r: number, c: number): boolean {
|
|
548
|
+
return modules[r]![c] === true
|
|
549
|
+
}
|
|
550
|
+
|
|
547
551
|
for (let row = 0; row < size; row++) {
|
|
548
552
|
for (let col = 0; col < size - 6; col++) {
|
|
549
553
|
if (
|
package/src/semver.ts
CHANGED
|
@@ -138,8 +138,12 @@ class SemverFactory {
|
|
|
138
138
|
*/
|
|
139
139
|
sort(items: Semver[], opt: SortOptions = {}): Semver[] {
|
|
140
140
|
const mod = opt.dir === 'desc' ? -1 : 1
|
|
141
|
-
|
|
142
|
-
|
|
141
|
+
|
|
142
|
+
function cmp(a: Semver, b: Semver): number {
|
|
143
|
+
return a.compare(b) * mod
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return opt.mutate ? items.sort(cmp) : items.slice().sort(cmp)
|
|
143
147
|
}
|
|
144
148
|
}
|
|
145
149
|
|
package/src/string/slugify.ts
CHANGED
|
@@ -61,16 +61,14 @@ function buildPatternSlug(options: any): RegExp {
|
|
|
61
61
|
let negationSetPattern = String.raw`a-z\d`
|
|
62
62
|
negationSetPattern += options.lowercase ? '' : 'A-Z'
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
negationSetPattern += escapeStringRegexp(character)
|
|
64
|
+
for (const character of options.preserveCharacters) {
|
|
65
|
+
if (character === options.separator) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`The separator character \`${options.separator}\` cannot be included in preserved characters: ${options.preserveCharacters}`,
|
|
68
|
+
)
|
|
73
69
|
}
|
|
70
|
+
|
|
71
|
+
negationSetPattern += escapeStringRegexp(character)
|
|
74
72
|
}
|
|
75
73
|
|
|
76
74
|
return new RegExp(`[^${negationSetPattern}]+`, 'g')
|
package/src/types.ts
CHANGED
|
@@ -182,7 +182,9 @@ export const _passUndefinedMapper: IndexedMapper<any, void> = () => undefined
|
|
|
182
182
|
/**
|
|
183
183
|
* Function that does nothings and returns `undefined`.
|
|
184
184
|
*/
|
|
185
|
-
export
|
|
185
|
+
export function _noop(..._args: any[]): undefined {
|
|
186
|
+
return undefined
|
|
187
|
+
}
|
|
186
188
|
|
|
187
189
|
export type Predicate<T> = (item: T, index: number) => boolean
|
|
188
190
|
export type AsyncPredicate<T> = (item: T, index: number) => PromiseLike<boolean>
|