@naturalcycles/js-lib 15.8.1 → 15.9.1
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/array/array.util.d.ts +14 -6
- package/dist/array/array.util.js +57 -36
- package/dist/datetime/dateInterval.d.ts +4 -5
- package/dist/datetime/dateInterval.js +8 -11
- package/dist/datetime/localDate.d.ts +9 -8
- package/dist/datetime/localDate.js +15 -17
- package/dist/datetime/localTime.d.ts +9 -8
- package/dist/datetime/localTime.js +35 -36
- package/dist/datetime/timeInterval.d.ts +2 -2
- package/dist/datetime/timeInterval.js +3 -4
- package/dist/http/fetcher.js +1 -1
- package/dist/json-schema/jsonSchema.util.js +1 -1
- package/dist/log/commonLogger.d.ts +2 -1
- package/dist/log/commonLogger.js +2 -3
- package/dist/math/math.util.js +0 -1
- package/dist/number/number.util.d.ts +4 -3
- package/dist/number/number.util.js +9 -14
- package/dist/object/object.util.d.ts +14 -13
- package/dist/object/object.util.js +26 -26
- package/dist/semver.d.ts +2 -2
- package/dist/semver.js +3 -3
- package/dist/types.d.ts +7 -5
- package/dist/types.js +1 -1
- package/package.json +1 -1
- package/src/array/array.util.ts +73 -45
- package/src/datetime/dateInterval.ts +9 -12
- package/src/datetime/localDate.ts +17 -18
- package/src/datetime/localTime.ts +36 -36
- package/src/datetime/timeInterval.ts +4 -5
- package/src/http/fetcher.ts +1 -1
- package/src/json-schema/jsonSchema.util.ts +1 -1
- package/src/log/commonLogger.ts +4 -2
- package/src/math/math.util.ts +0 -1
- package/src/number/number.util.ts +9 -19
- package/src/object/object.util.ts +30 -26
- package/src/semver.ts +4 -4
- package/src/types.ts +11 -6
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SortOptions } from '../array/array.util.js'
|
|
2
|
+
import type { Inclusiveness } from '../types.js'
|
|
2
3
|
|
|
3
4
|
export function _randomInt(minIncl: number, maxIncl: number): number {
|
|
4
5
|
return Math.floor(Math.random() * (maxIncl - minIncl + 1) + minIncl)
|
|
@@ -42,18 +43,11 @@ export function _isBetween<T extends number | string>(
|
|
|
42
43
|
x: T,
|
|
43
44
|
min: T,
|
|
44
45
|
max: T,
|
|
45
|
-
incl: Inclusiveness
|
|
46
|
+
incl: Inclusiveness,
|
|
46
47
|
): boolean {
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (incl === '[]') {
|
|
51
|
-
return x >= min && x <= max
|
|
52
|
-
}
|
|
53
|
-
if (incl === '(]') {
|
|
54
|
-
return x > min && x <= max
|
|
55
|
-
}
|
|
56
|
-
return x > min && x < max
|
|
48
|
+
if (x < min || x > max) return false
|
|
49
|
+
if (x === max && incl === '[)') return false
|
|
50
|
+
return true
|
|
57
51
|
}
|
|
58
52
|
|
|
59
53
|
export function _clamp(x: number, minIncl: number, maxIncl: number): number {
|
|
@@ -70,13 +64,9 @@ export function _clamp(x: number, minIncl: number, maxIncl: number): number {
|
|
|
70
64
|
* _sortNumbers([1, 3, 2])
|
|
71
65
|
* // [1, 2, 3]
|
|
72
66
|
*/
|
|
73
|
-
export function _sortNumbers(
|
|
74
|
-
|
|
75
|
-
mutate
|
|
76
|
-
dir: SortDirection = 'asc',
|
|
77
|
-
): number[] {
|
|
78
|
-
const mod = dir === 'desc' ? -1 : 1
|
|
79
|
-
return (mutate ? numbers : [...numbers]).sort((a, b) => (a - b) * mod)
|
|
67
|
+
export function _sortNumbers(numbers: number[], opt: SortOptions = {}): number[] {
|
|
68
|
+
const mod = opt.dir === 'desc' ? -1 : 1
|
|
69
|
+
return (opt.mutate ? numbers : [...numbers]).sort((a, b) => (a - b) * mod)
|
|
80
70
|
}
|
|
81
71
|
|
|
82
72
|
/**
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { MutateOptions } from '../array/array.util.js'
|
|
1
2
|
import { _isEmpty, _isObject } from '../is.util.js'
|
|
2
3
|
import type {
|
|
3
4
|
AnyObject,
|
|
@@ -16,9 +17,9 @@ import { _objectEntries, SKIP } from '../types.js'
|
|
|
16
17
|
export function _pick<T extends AnyObject, K extends keyof T>(
|
|
17
18
|
obj: T,
|
|
18
19
|
props: readonly K[],
|
|
19
|
-
|
|
20
|
+
opt: MutateOptions = {},
|
|
20
21
|
): T {
|
|
21
|
-
if (mutate) {
|
|
22
|
+
if (opt.mutate) {
|
|
22
23
|
// Start as original object (mutable), DELETE properties that are not whitelisted
|
|
23
24
|
for (const k of Object.keys(obj)) {
|
|
24
25
|
if (!props.includes(k as K)) delete obj[k]
|
|
@@ -40,9 +41,9 @@ export function _pick<T extends AnyObject, K extends keyof T>(
|
|
|
40
41
|
export function _pickWithUndefined<T extends AnyObject, K extends keyof T>(
|
|
41
42
|
obj: T,
|
|
42
43
|
props: readonly K[],
|
|
43
|
-
|
|
44
|
+
opt: MutateOptions = {},
|
|
44
45
|
): T {
|
|
45
|
-
const r: T = mutate ? obj : { ...obj }
|
|
46
|
+
const r: T = opt.mutate ? obj : { ...obj }
|
|
46
47
|
for (const k of Object.keys(r)) {
|
|
47
48
|
if (!props.includes(k as K)) {
|
|
48
49
|
r[k as K] = undefined as any
|
|
@@ -58,9 +59,9 @@ export function _pickWithUndefined<T extends AnyObject, K extends keyof T>(
|
|
|
58
59
|
export function _omit<T extends AnyObject, K extends keyof T>(
|
|
59
60
|
obj: T,
|
|
60
61
|
props: readonly K[],
|
|
61
|
-
|
|
62
|
+
opt: MutateOptions = {},
|
|
62
63
|
): T {
|
|
63
|
-
if (mutate) {
|
|
64
|
+
if (opt.mutate) {
|
|
64
65
|
for (const k of props) {
|
|
65
66
|
delete obj[k]
|
|
66
67
|
}
|
|
@@ -81,9 +82,9 @@ export function _omit<T extends AnyObject, K extends keyof T>(
|
|
|
81
82
|
export function _omitWithUndefined<T extends AnyObject, K extends keyof T>(
|
|
82
83
|
obj: T,
|
|
83
84
|
props: readonly K[],
|
|
84
|
-
|
|
85
|
+
opt: MutateOptions = {},
|
|
85
86
|
): T {
|
|
86
|
-
const r: T = mutate ? obj : { ...obj }
|
|
87
|
+
const r: T = opt.mutate ? obj : { ...obj }
|
|
87
88
|
for (const k of props) {
|
|
88
89
|
r[k] = undefined as any
|
|
89
90
|
}
|
|
@@ -98,8 +99,8 @@ export function _omitWithUndefined<T extends AnyObject, K extends keyof T>(
|
|
|
98
99
|
* 'account.updated',
|
|
99
100
|
* ])
|
|
100
101
|
*/
|
|
101
|
-
export function _mask<T extends AnyObject>(obj: T, props: string[],
|
|
102
|
-
const r = mutate ? obj : _deepCopy(obj)
|
|
102
|
+
export function _mask<T extends AnyObject>(obj: T, props: string[], opt: MutateOptions = {}): T {
|
|
103
|
+
const r = opt.mutate ? obj : _deepCopy(obj)
|
|
103
104
|
for (const k of props) {
|
|
104
105
|
_unset(r, k)
|
|
105
106
|
}
|
|
@@ -109,27 +110,27 @@ export function _mask<T extends AnyObject>(obj: T, props: string[], mutate = fal
|
|
|
109
110
|
/**
|
|
110
111
|
* Removes "falsy" values from the object.
|
|
111
112
|
*/
|
|
112
|
-
export function _filterFalsyValues<T extends AnyObject>(obj: T,
|
|
113
|
-
return _filterObject(obj, (_k, v) => !!v,
|
|
113
|
+
export function _filterFalsyValues<T extends AnyObject>(obj: T, opt: MutateOptions = {}): T {
|
|
114
|
+
return _filterObject(obj, (_k, v) => !!v, opt)
|
|
114
115
|
}
|
|
115
116
|
|
|
116
117
|
/**
|
|
117
118
|
* Removes values from the object that are `null` or `undefined`.
|
|
118
119
|
*/
|
|
119
|
-
export function _filterNullishValues<T extends AnyObject>(obj: T,
|
|
120
|
-
return _filterObject(obj, (_k, v) => v !== undefined && v !== null,
|
|
120
|
+
export function _filterNullishValues<T extends AnyObject>(obj: T, opt: MutateOptions = {}): T {
|
|
121
|
+
return _filterObject(obj, (_k, v) => v !== undefined && v !== null, opt)
|
|
121
122
|
}
|
|
122
123
|
|
|
123
124
|
/**
|
|
124
125
|
* Removes values from the object that are `undefined`.
|
|
125
126
|
* Only `undefined` values are removed. `null` values are kept!
|
|
126
127
|
*/
|
|
127
|
-
export function _filterUndefinedValues<T extends AnyObject>(obj: T,
|
|
128
|
-
return _filterObject(obj, (_k, v) => v !== undefined,
|
|
128
|
+
export function _filterUndefinedValues<T extends AnyObject>(obj: T, opt: MutateOptions = {}): T {
|
|
129
|
+
return _filterObject(obj, (_k, v) => v !== undefined, opt)
|
|
129
130
|
}
|
|
130
131
|
|
|
131
|
-
export function _filterEmptyArrays<T extends AnyObject>(obj: T,
|
|
132
|
-
return _filterObject(obj, (_k, v) => !Array.isArray(v) || v.length > 0,
|
|
132
|
+
export function _filterEmptyArrays<T extends AnyObject>(obj: T, opt: MutateOptions = {}): T {
|
|
133
|
+
return _filterObject(obj, (_k, v) => !Array.isArray(v) || v.length > 0, opt)
|
|
133
134
|
}
|
|
134
135
|
|
|
135
136
|
/**
|
|
@@ -139,9 +140,9 @@ export function _filterEmptyArrays<T extends AnyObject>(obj: T, mutate = false):
|
|
|
139
140
|
export function _filterObject<T extends AnyObject>(
|
|
140
141
|
obj: T,
|
|
141
142
|
predicate: ObjectPredicate<T>,
|
|
142
|
-
|
|
143
|
+
opt: MutateOptions = {},
|
|
143
144
|
): T {
|
|
144
|
-
if (mutate) {
|
|
145
|
+
if (opt.mutate) {
|
|
145
146
|
for (const [k, v] of _objectEntries(obj)) {
|
|
146
147
|
if (!predicate(k, v, obj)) {
|
|
147
148
|
delete obj[k]
|
|
@@ -173,9 +174,9 @@ export function _filterObject<T extends AnyObject>(
|
|
|
173
174
|
export function _mapValues<OUT = unknown, IN extends AnyObject = AnyObject>(
|
|
174
175
|
obj: IN,
|
|
175
176
|
mapper: ObjectMapper<IN, any>,
|
|
176
|
-
|
|
177
|
+
opt: MutateOptions = {},
|
|
177
178
|
): OUT {
|
|
178
|
-
const map: any = mutate ? obj : {}
|
|
179
|
+
const map: any = opt.mutate ? obj : {}
|
|
179
180
|
for (const [k, v] of Object.entries(obj)) {
|
|
180
181
|
map[k] = mapper(k, v, obj)
|
|
181
182
|
}
|
|
@@ -231,8 +232,11 @@ export function _findKeyByValue<T extends AnyObject>(obj: T, v: ValueOf<T>): key
|
|
|
231
232
|
return Object.entries(obj).find(([_, value]) => value === v)?.[0] as keyof T
|
|
232
233
|
}
|
|
233
234
|
|
|
234
|
-
export function _objectNullValuesToUndefined<T extends AnyObject>(
|
|
235
|
-
|
|
235
|
+
export function _objectNullValuesToUndefined<T extends AnyObject>(
|
|
236
|
+
obj: T,
|
|
237
|
+
opt: MutateOptions = {},
|
|
238
|
+
): T {
|
|
239
|
+
return _mapValues(obj, (_k, v) => (v === null ? undefined : v), opt)
|
|
236
240
|
}
|
|
237
241
|
|
|
238
242
|
/**
|
|
@@ -253,8 +257,8 @@ export function _undefinedIfEmpty<T>(obj: T | undefined): T | undefined {
|
|
|
253
257
|
/**
|
|
254
258
|
* Filters the object by removing all key-value pairs where Value is Empty (according to _isEmpty() specification).
|
|
255
259
|
*/
|
|
256
|
-
export function _filterEmptyValues<T extends AnyObject>(obj: T,
|
|
257
|
-
return _filterObject(obj, (_k, v) => !_isEmpty(v),
|
|
260
|
+
export function _filterEmptyValues<T extends AnyObject>(obj: T, opt: MutateOptions = {}): T {
|
|
261
|
+
return _filterObject(obj, (_k, v) => !_isEmpty(v), opt)
|
|
258
262
|
}
|
|
259
263
|
|
|
260
264
|
/**
|
package/src/semver.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import type { SortOptions } from './array/array.util.js'
|
|
1
2
|
import { _range } from './array/range.js'
|
|
2
3
|
import { _assert } from './error/assert.js'
|
|
3
|
-
import type { SortDirection } from './types.js'
|
|
4
4
|
|
|
5
5
|
export type SemverInput = string | Semver
|
|
6
6
|
export type SemverInputNullable = SemverInput | null | undefined
|
|
@@ -134,9 +134,9 @@ class SemverFactory {
|
|
|
134
134
|
/**
|
|
135
135
|
* Sorts an array of Semvers in `dir` order (ascending by default).
|
|
136
136
|
*/
|
|
137
|
-
sort(items: Semver[],
|
|
138
|
-
const mod = dir === 'desc' ? -1 : 1
|
|
139
|
-
return (mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod)
|
|
137
|
+
sort(items: Semver[], opt: SortOptions = {}): Semver[] {
|
|
138
|
+
const mod = opt.dir === 'desc' ? -1 : 1
|
|
139
|
+
return (opt.mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod)
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
|
package/src/types.ts
CHANGED
|
@@ -144,11 +144,16 @@ export const MISS = Symbol('MISS')
|
|
|
144
144
|
/**
|
|
145
145
|
* Function which is called for every item in `input`. Expected to return a `Promise` or value.
|
|
146
146
|
*/
|
|
147
|
-
export type AsyncMapper<IN = any, OUT = any> = (input: IN
|
|
148
|
-
export type
|
|
147
|
+
export type AsyncMapper<IN = any, OUT = any> = (input: IN) => OUT | PromiseLike<OUT>
|
|
148
|
+
export type AsyncIndexedMapper<IN = any, OUT = any> = (
|
|
149
|
+
input: IN,
|
|
150
|
+
index: number,
|
|
151
|
+
) => OUT | PromiseLike<OUT>
|
|
152
|
+
export type Mapper<IN = any, OUT = any> = (input: IN) => OUT
|
|
153
|
+
export type IndexedMapper<IN = any, OUT = any> = (input: IN, index: number) => OUT
|
|
149
154
|
|
|
150
|
-
export const _passthroughMapper:
|
|
151
|
-
export const _passUndefinedMapper:
|
|
155
|
+
export const _passthroughMapper: IndexedMapper = item => item
|
|
156
|
+
export const _passUndefinedMapper: IndexedMapper<any, void> = () => undefined
|
|
152
157
|
|
|
153
158
|
/**
|
|
154
159
|
* Function that does nothings and returns `undefined`.
|
|
@@ -350,7 +355,7 @@ export function _stringMapValuesSorted<T>(
|
|
|
350
355
|
mapper: Mapper<T, any>,
|
|
351
356
|
dir: SortDirection = 'asc',
|
|
352
357
|
): T[] {
|
|
353
|
-
return _sortBy(_stringMapValues(map), mapper,
|
|
358
|
+
return _sortBy(_stringMapValues(map), mapper, { dir })
|
|
354
359
|
}
|
|
355
360
|
|
|
356
361
|
/**
|
|
@@ -419,7 +424,7 @@ export type ErrorDataTuple<T = unknown, ERR = Error> = [err: null, data: T] | [e
|
|
|
419
424
|
|
|
420
425
|
export type SortDirection = 'asc' | 'desc'
|
|
421
426
|
|
|
422
|
-
export type Inclusiveness = '
|
|
427
|
+
export type Inclusiveness = '[]' | '[)'
|
|
423
428
|
|
|
424
429
|
/**
|
|
425
430
|
* @experimental
|