@naturalcycles/js-lib 15.42.0 → 15.43.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/array/array.util.d.ts +1 -21
- package/dist/array/array.util.js +0 -23
- package/dist/array/array2.d.ts +17 -0
- package/dist/array/array2.js +44 -0
- package/dist/array/index.d.ts +4 -0
- package/dist/array/index.js +4 -0
- package/dist/array/sort.d.ts +51 -0
- package/dist/array/sort.js +61 -0
- package/dist/array/sortedArray.d.ts +32 -0
- package/dist/array/sortedArray.js +55 -0
- package/dist/array/sortedSet.d.ts +22 -0
- package/dist/array/sortedSet.js +37 -0
- package/dist/datetime/localDate.d.ts +1 -2
- package/dist/datetime/localTime.d.ts +1 -2
- package/dist/json-schema/jsonSchemaBuilder.d.ts +5 -3
- package/dist/json-schema/jsonSchemaBuilder.js +1 -1
- package/dist/log/commonLogger.d.ts +1 -1
- package/dist/math/math.util.js +3 -3
- package/dist/number/number.util.d.ts +0 -11
- package/dist/number/number.util.js +0 -13
- package/dist/object/keySortedMap.d.ts +9 -7
- package/dist/object/keySortedMap.js +1 -11
- package/dist/object/object.util.d.ts +1 -2
- package/dist/object/set2.d.ts +4 -0
- package/dist/object/set2.js +16 -0
- package/dist/semver.d.ts +1 -1
- package/dist/types.d.ts +23 -10
- package/dist/types.js +0 -7
- package/package.json +2 -2
- package/src/array/array.util.ts +1 -42
- package/src/array/array2.ts +52 -0
- package/src/array/index.ts +4 -0
- package/src/array/sort.ts +96 -0
- package/src/array/sortedArray.ts +78 -0
- package/src/array/sortedSet.ts +54 -0
- package/src/datetime/localDate.ts +2 -1
- package/src/datetime/localTime.ts +1 -1
- package/src/json-schema/jsonSchemaBuilder.ts +10 -6
- package/src/log/commonLogger.ts +1 -1
- package/src/math/math.util.ts +3 -3
- package/src/number/number.util.ts +0 -15
- package/src/object/keySortedMap.ts +10 -16
- package/src/object/object.util.ts +1 -1
- package/src/object/set2.ts +21 -1
- package/src/semver.ts +1 -1
- package/src/types.ts +25 -18
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AbortablePredicate, FalsyValue, Mapper,
|
|
1
|
+
import type { AbortablePredicate, FalsyValue, Mapper, MutateOptions, Predicate, StringMap } from '../types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
|
|
4
4
|
* final chunk will be the remaining elements.
|
|
@@ -90,26 +90,6 @@ export declare function _mapBy<ITEM, KEY>(items: readonly ITEM[], mapper: Mapper
|
|
|
90
90
|
* Returning `undefined` from the Mapper will EXCLUDE the item.
|
|
91
91
|
*/
|
|
92
92
|
export declare function _groupBy<T>(items: readonly T[], mapper: Mapper<T, any>): StringMap<T[]>;
|
|
93
|
-
export interface MutateOptions {
|
|
94
|
-
/**
|
|
95
|
-
* Defaults to false.
|
|
96
|
-
*/
|
|
97
|
-
mutate?: boolean;
|
|
98
|
-
}
|
|
99
|
-
export interface SortOptions extends MutateOptions {
|
|
100
|
-
/**
|
|
101
|
-
* Defaults to 'asc'.
|
|
102
|
-
*/
|
|
103
|
-
dir?: SortDirection;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* _sortBy([{age: 20}, {age: 10}], 'age')
|
|
107
|
-
* // => [{age: 10}, {age: 20}]
|
|
108
|
-
*
|
|
109
|
-
* Same:
|
|
110
|
-
* _sortBy([{age: 20}, {age: 10}], o => o.age)
|
|
111
|
-
*/
|
|
112
|
-
export declare function _sortBy<T, COMPARE_TYPE extends string | number>(items: T[], mapper: Mapper<T, COMPARE_TYPE>, opt?: SortOptions): T[];
|
|
113
93
|
/**
|
|
114
94
|
* Similar to `Array.find`, but the `predicate` may return `END` to stop the iteration early.
|
|
115
95
|
*
|
package/dist/array/array.util.js
CHANGED
|
@@ -151,29 +151,6 @@ export function _groupBy(items, mapper) {
|
|
|
151
151
|
}
|
|
152
152
|
return map;
|
|
153
153
|
}
|
|
154
|
-
/**
|
|
155
|
-
* _sortBy([{age: 20}, {age: 10}], 'age')
|
|
156
|
-
* // => [{age: 10}, {age: 20}]
|
|
157
|
-
*
|
|
158
|
-
* Same:
|
|
159
|
-
* _sortBy([{age: 20}, {age: 10}], o => o.age)
|
|
160
|
-
*/
|
|
161
|
-
export function _sortBy(items, mapper, opt = {}) {
|
|
162
|
-
const mod = opt.dir === 'desc' ? -1 : 1;
|
|
163
|
-
return (opt.mutate ? items : [...items]).sort((_a, _b) => {
|
|
164
|
-
// This implementation may call mapper more than once per item,
|
|
165
|
-
// but the benchmarks show no significant difference in performance.
|
|
166
|
-
const a = mapper(_a);
|
|
167
|
-
const b = mapper(_b);
|
|
168
|
-
// if (typeof a === 'number' && typeof b === 'number') return (a - b) * mod
|
|
169
|
-
// return String(a).localeCompare(String(b)) * mod
|
|
170
|
-
if (a > b)
|
|
171
|
-
return mod;
|
|
172
|
-
if (a < b)
|
|
173
|
-
return -mod;
|
|
174
|
-
return 0;
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
154
|
/**
|
|
178
155
|
* Similar to `Array.find`, but the `predicate` may return `END` to stop the iteration early.
|
|
179
156
|
*
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Better Array.
|
|
3
|
+
*
|
|
4
|
+
* @experimental
|
|
5
|
+
*/
|
|
6
|
+
export declare class Array2<T> extends Array<T> {
|
|
7
|
+
static of<T>(...items: T[]): Array2<T>;
|
|
8
|
+
get [Symbol.toStringTag](): string;
|
|
9
|
+
firstOrUndefined(): T | undefined;
|
|
10
|
+
first(): T;
|
|
11
|
+
lastOrUndefined(): T | undefined;
|
|
12
|
+
last(): T;
|
|
13
|
+
uniq(): Array2<T>;
|
|
14
|
+
shuffle(): Array2<T>;
|
|
15
|
+
isEmpty(): boolean;
|
|
16
|
+
isNotEmpty(): boolean;
|
|
17
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { _shuffle } from './array.util.js';
|
|
2
|
+
/**
|
|
3
|
+
* Better Array.
|
|
4
|
+
*
|
|
5
|
+
* @experimental
|
|
6
|
+
*/
|
|
7
|
+
export class Array2 extends Array {
|
|
8
|
+
static of(...items) {
|
|
9
|
+
return new Array2(...items);
|
|
10
|
+
}
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/class-literal-property-style
|
|
12
|
+
get [Symbol.toStringTag]() {
|
|
13
|
+
return 'Array2';
|
|
14
|
+
}
|
|
15
|
+
firstOrUndefined() {
|
|
16
|
+
return this[0];
|
|
17
|
+
}
|
|
18
|
+
first() {
|
|
19
|
+
if (!this.length)
|
|
20
|
+
throw new Error('Array.first called on empty array');
|
|
21
|
+
return this[0];
|
|
22
|
+
}
|
|
23
|
+
lastOrUndefined() {
|
|
24
|
+
return this[this.length - 1];
|
|
25
|
+
}
|
|
26
|
+
last() {
|
|
27
|
+
const { length } = this;
|
|
28
|
+
if (!length)
|
|
29
|
+
throw new Error('Array.last called on empty array');
|
|
30
|
+
return this[length - 1];
|
|
31
|
+
}
|
|
32
|
+
uniq() {
|
|
33
|
+
return new Array2(...new Set(this));
|
|
34
|
+
}
|
|
35
|
+
shuffle() {
|
|
36
|
+
return new Array2(..._shuffle(this));
|
|
37
|
+
}
|
|
38
|
+
isEmpty() {
|
|
39
|
+
return this.length === 0;
|
|
40
|
+
}
|
|
41
|
+
isNotEmpty() {
|
|
42
|
+
return this.length !== 0;
|
|
43
|
+
}
|
|
44
|
+
}
|
package/dist/array/index.d.ts
CHANGED
package/dist/array/index.js
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type Comparator, type Mapper, type SortDirection, type SortOptions, type StringMap } from '../types.js';
|
|
2
|
+
declare class Comparators {
|
|
3
|
+
/**
|
|
4
|
+
* Good for numbers.
|
|
5
|
+
*/
|
|
6
|
+
numericAsc(a: number, b: number): number;
|
|
7
|
+
numericDesc(a: number, b: number): number;
|
|
8
|
+
localeAsc(a: string, b: string): number;
|
|
9
|
+
localeDesc(a: string, b: string): number;
|
|
10
|
+
by<T, COMPARE_TYPE extends string | number>(mapper: Mapper<T, COMPARE_TYPE>, opt?: ComparatorByOptions): Comparator<T>;
|
|
11
|
+
updatedAsc(a: {
|
|
12
|
+
updated: number;
|
|
13
|
+
}, b: {
|
|
14
|
+
updated: number;
|
|
15
|
+
}): number;
|
|
16
|
+
updatedDesc(a: {
|
|
17
|
+
updated: number;
|
|
18
|
+
}, b: {
|
|
19
|
+
updated: number;
|
|
20
|
+
}): number;
|
|
21
|
+
createdAsc(a: {
|
|
22
|
+
created: number;
|
|
23
|
+
}, b: {
|
|
24
|
+
created: number;
|
|
25
|
+
}): number;
|
|
26
|
+
createdDesc(a: {
|
|
27
|
+
created: number;
|
|
28
|
+
}, b: {
|
|
29
|
+
created: number;
|
|
30
|
+
}): number;
|
|
31
|
+
}
|
|
32
|
+
export declare const comparators: Comparators;
|
|
33
|
+
interface ComparatorByOptions {
|
|
34
|
+
/**
|
|
35
|
+
* Defaults to 'asc'.
|
|
36
|
+
*/
|
|
37
|
+
dir?: SortDirection;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* _sortBy([{age: 20}, {age: 10}], 'age')
|
|
41
|
+
* // => [{age: 10}, {age: 20}]
|
|
42
|
+
*
|
|
43
|
+
* Same:
|
|
44
|
+
* _sortBy([{age: 20}, {age: 10}], o => o.age)
|
|
45
|
+
*/
|
|
46
|
+
export declare function _sortBy<T, COMPARE_TYPE extends string | number>(items: T[], mapper: Mapper<T, COMPARE_TYPE>, opt?: SortOptions): T[];
|
|
47
|
+
/**
|
|
48
|
+
* Like _stringMapValues, but values are sorted.
|
|
49
|
+
*/
|
|
50
|
+
export declare function _stringMapValuesSorted<T>(map: StringMap<T>, mapper: Mapper<T, any>, dir?: SortDirection): T[];
|
|
51
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { _stringMapValues, } from '../types.js';
|
|
2
|
+
class Comparators {
|
|
3
|
+
/**
|
|
4
|
+
* Good for numbers.
|
|
5
|
+
*/
|
|
6
|
+
numericAsc(a, b) {
|
|
7
|
+
return a - b;
|
|
8
|
+
}
|
|
9
|
+
numericDesc(a, b) {
|
|
10
|
+
return b - a;
|
|
11
|
+
}
|
|
12
|
+
localeAsc(a, b) {
|
|
13
|
+
return a.localeCompare(b);
|
|
14
|
+
}
|
|
15
|
+
localeDesc(a, b) {
|
|
16
|
+
return -a.localeCompare(b);
|
|
17
|
+
}
|
|
18
|
+
by(mapper, opt = {}) {
|
|
19
|
+
const mod = opt.dir === 'desc' ? -1 : 1;
|
|
20
|
+
return (objA, objB) => {
|
|
21
|
+
// This implementation may call mapper more than once per item,
|
|
22
|
+
// but the benchmarks show no significant difference in performance.
|
|
23
|
+
const a = mapper(objA);
|
|
24
|
+
const b = mapper(objB);
|
|
25
|
+
if (a > b)
|
|
26
|
+
return mod;
|
|
27
|
+
if (a < b)
|
|
28
|
+
return -mod;
|
|
29
|
+
return 0;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
updatedAsc(a, b) {
|
|
33
|
+
return a.updated - b.updated;
|
|
34
|
+
}
|
|
35
|
+
updatedDesc(a, b) {
|
|
36
|
+
return b.updated - a.updated;
|
|
37
|
+
}
|
|
38
|
+
createdAsc(a, b) {
|
|
39
|
+
return a.created - b.created;
|
|
40
|
+
}
|
|
41
|
+
createdDesc(a, b) {
|
|
42
|
+
return b.created - a.created;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export const comparators = new Comparators();
|
|
46
|
+
/**
|
|
47
|
+
* _sortBy([{age: 20}, {age: 10}], 'age')
|
|
48
|
+
* // => [{age: 10}, {age: 20}]
|
|
49
|
+
*
|
|
50
|
+
* Same:
|
|
51
|
+
* _sortBy([{age: 20}, {age: 10}], o => o.age)
|
|
52
|
+
*/
|
|
53
|
+
export function _sortBy(items, mapper, opt = {}) {
|
|
54
|
+
return (opt.mutate ? items : [...items]).sort(comparators.by(mapper, opt));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Like _stringMapValues, but values are sorted.
|
|
58
|
+
*/
|
|
59
|
+
export function _stringMapValuesSorted(map, mapper, dir = 'asc') {
|
|
60
|
+
return _sortBy(_stringMapValues(map), mapper, { dir });
|
|
61
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Comparator } from '../types.js';
|
|
2
|
+
import { Array2 } from './array2.js';
|
|
3
|
+
export interface SortedArrayOptions<T> {
|
|
4
|
+
/**
|
|
5
|
+
* Defaults to undefined.
|
|
6
|
+
* Undefined (default comparator) works well for String keys.
|
|
7
|
+
* For Number keys - use comparators.numericAsc (or desc),
|
|
8
|
+
* otherwise sorting will be wrong (lexicographic).
|
|
9
|
+
*/
|
|
10
|
+
comparator?: Comparator<T>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Like Array, but keeps values sorted after every insertion.
|
|
14
|
+
*/
|
|
15
|
+
export declare class SortedArray<T> extends Array2<T> {
|
|
16
|
+
#private;
|
|
17
|
+
constructor(values?: Iterable<T>, opt?: SortedArrayOptions<T>);
|
|
18
|
+
push(...values: T[]): number;
|
|
19
|
+
unshift(...values: T[]): number;
|
|
20
|
+
splice(start: number, deleteCount?: number, ...items: T[]): T[];
|
|
21
|
+
static get [Symbol.species](): ArrayConstructor;
|
|
22
|
+
get [Symbol.toStringTag](): string;
|
|
23
|
+
private resort;
|
|
24
|
+
}
|
|
25
|
+
export declare class SortedStringArray extends SortedArray<string> {
|
|
26
|
+
constructor(values?: Iterable<string>);
|
|
27
|
+
get [Symbol.toStringTag](): string;
|
|
28
|
+
}
|
|
29
|
+
export declare class SortedNumberArray extends SortedArray<number> {
|
|
30
|
+
constructor(values?: Iterable<number>);
|
|
31
|
+
get [Symbol.toStringTag](): string;
|
|
32
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Array2 } from './array2.js';
|
|
2
|
+
import { comparators } from './sort.js';
|
|
3
|
+
/**
|
|
4
|
+
* Like Array, but keeps values sorted after every insertion.
|
|
5
|
+
*/
|
|
6
|
+
export class SortedArray extends Array2 {
|
|
7
|
+
constructor(values = [], opt = {}) {
|
|
8
|
+
super(...values);
|
|
9
|
+
this.#comparator = opt.comparator;
|
|
10
|
+
this.resort();
|
|
11
|
+
}
|
|
12
|
+
#comparator;
|
|
13
|
+
push(...values) {
|
|
14
|
+
const length = super.push(...values);
|
|
15
|
+
this.resort();
|
|
16
|
+
return length;
|
|
17
|
+
}
|
|
18
|
+
unshift(...values) {
|
|
19
|
+
const length = super.unshift(...values);
|
|
20
|
+
this.resort();
|
|
21
|
+
return length;
|
|
22
|
+
}
|
|
23
|
+
splice(start, deleteCount, ...items) {
|
|
24
|
+
const removed = super.splice(start, deleteCount ?? this.length - start, ...items);
|
|
25
|
+
if (items.length) {
|
|
26
|
+
this.resort();
|
|
27
|
+
}
|
|
28
|
+
return removed;
|
|
29
|
+
}
|
|
30
|
+
static get [Symbol.species]() {
|
|
31
|
+
return Array;
|
|
32
|
+
}
|
|
33
|
+
get [Symbol.toStringTag]() {
|
|
34
|
+
return 'Array';
|
|
35
|
+
}
|
|
36
|
+
resort() {
|
|
37
|
+
super.sort(this.#comparator);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export class SortedStringArray extends SortedArray {
|
|
41
|
+
constructor(values = []) {
|
|
42
|
+
super(values);
|
|
43
|
+
}
|
|
44
|
+
get [Symbol.toStringTag]() {
|
|
45
|
+
return 'Array';
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export class SortedNumberArray extends SortedArray {
|
|
49
|
+
constructor(values = []) {
|
|
50
|
+
super(values, { comparator: comparators.numericAsc });
|
|
51
|
+
}
|
|
52
|
+
get [Symbol.toStringTag]() {
|
|
53
|
+
return 'Array';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Set2 } from '../object/index.js';
|
|
2
|
+
import type { Comparator } from '../types.js';
|
|
3
|
+
export interface SortedSetOptions<T> {
|
|
4
|
+
/**
|
|
5
|
+
* Defaults to undefined.
|
|
6
|
+
* Undefined (default comparator) works well for String keys.
|
|
7
|
+
* For Number keys - use comparators.numericAsc (or desc),
|
|
8
|
+
* otherwise sorting will be wrong (lexicographic).
|
|
9
|
+
*/
|
|
10
|
+
comparator?: Comparator<T>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Like Set, but keeps members sorted after every insertion.
|
|
14
|
+
*/
|
|
15
|
+
export declare class SortedSet<T> extends Set2<T> {
|
|
16
|
+
#private;
|
|
17
|
+
constructor(values?: Iterable<T>, opt?: SortedSetOptions<T>);
|
|
18
|
+
add(value: T): this;
|
|
19
|
+
addMany(items: Iterable<T>): this;
|
|
20
|
+
private recreate;
|
|
21
|
+
get [Symbol.toStringTag](): string;
|
|
22
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Set2 } from '../object/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Like Set, but keeps members sorted after every insertion.
|
|
4
|
+
*/
|
|
5
|
+
export class SortedSet extends Set2 {
|
|
6
|
+
constructor(values = [], opt = {}) {
|
|
7
|
+
super();
|
|
8
|
+
this.#comparator = opt.comparator;
|
|
9
|
+
this.addMany(values);
|
|
10
|
+
}
|
|
11
|
+
#comparator;
|
|
12
|
+
add(value) {
|
|
13
|
+
if (super.has(value)) {
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
super.add(value);
|
|
17
|
+
return this.recreate();
|
|
18
|
+
}
|
|
19
|
+
addMany(items) {
|
|
20
|
+
for (const item of items) {
|
|
21
|
+
super.add(item);
|
|
22
|
+
}
|
|
23
|
+
return this.recreate();
|
|
24
|
+
}
|
|
25
|
+
recreate() {
|
|
26
|
+
const items = Array.from(super.values());
|
|
27
|
+
items.sort(this.#comparator);
|
|
28
|
+
super.clear();
|
|
29
|
+
for (const item of items) {
|
|
30
|
+
super.add(item);
|
|
31
|
+
}
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
get [Symbol.toStringTag]() {
|
|
35
|
+
return 'Set';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { MutateOptions, SortOptions } from '../array/array.util.js';
|
|
2
1
|
import { Iterable2 } from '../iter/iterable2.js';
|
|
3
|
-
import type { Inclusiveness, IsoDate, IsoDateTime, MonthId, UnixTimestamp, UnixTimestampMillis } from '../types.js';
|
|
2
|
+
import type { Inclusiveness, IsoDate, IsoDateTime, MonthId, MutateOptions, SortOptions, UnixTimestamp, UnixTimestampMillis } from '../types.js';
|
|
4
3
|
import type { DateObject, ISODayOfWeek, LocalTime } from './localTime.js';
|
|
5
4
|
export type LocalDateUnit = LocalDateUnitStrict | 'week';
|
|
6
5
|
export type LocalDateUnitStrict = 'year' | 'month' | 'day';
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { MutateOptions } from '../
|
|
2
|
-
import type { IANATimezone, Inclusiveness, IsoDate, IsoDateTime, MonthId, NumberOfHours, NumberOfMinutes, SortDirection, UnixTimestamp, UnixTimestampMillis } from '../types.js';
|
|
1
|
+
import type { IANATimezone, Inclusiveness, IsoDate, IsoDateTime, MonthId, MutateOptions, NumberOfHours, NumberOfMinutes, SortDirection, UnixTimestamp, UnixTimestampMillis } from '../types.js';
|
|
3
2
|
import type { LocalDate } from './localDate.js';
|
|
4
3
|
import { WallTime } from './wallTime.js';
|
|
5
4
|
export type LocalTimeUnit = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second';
|
|
@@ -19,7 +19,7 @@ export declare const j: {
|
|
|
19
19
|
integer<T extends number = number>(): JsonSchemaNumberBuilder<T, false>;
|
|
20
20
|
string<T extends string = string>(): JsonSchemaStringBuilder<T, false>;
|
|
21
21
|
object: typeof object;
|
|
22
|
-
dbEntity<
|
|
22
|
+
dbEntity<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props: P): JsonSchemaObjectBuilder<BaseDBEntity & { [K in keyof P]: P[K] extends JsonSchemaAnyBuilder<infer U, any, any> ? U : never; }>;
|
|
23
23
|
rootObject<T extends AnyObject>(props: { [K in keyof T]: JsonSchemaAnyBuilder<T[K]>; }): JsonSchemaObjectBuilder<T, false>;
|
|
24
24
|
array<T extends JsonSchemaAnyBuilder<any>>(itemSchema: T): JsonSchemaArrayBuilder<T["infer"], false>;
|
|
25
25
|
tuple<T extends any[] = unknown[]>(items: JsonSchemaAnyBuilder[]): JsonSchemaTupleBuilder<T>;
|
|
@@ -139,12 +139,14 @@ export declare class JsonSchemaArrayBuilder<ITEM, Opt extends boolean = false> e
|
|
|
139
139
|
constructor(itemsSchema: JsonSchemaBuilder<ITEM>);
|
|
140
140
|
min(minItems: number): this;
|
|
141
141
|
max(maxItems: number): this;
|
|
142
|
-
unique(uniqueItems
|
|
142
|
+
unique(uniqueItems?: boolean): this;
|
|
143
143
|
}
|
|
144
144
|
export declare class JsonSchemaTupleBuilder<T extends any[]> extends JsonSchemaAnyBuilder<T, JsonSchemaTuple<T>> {
|
|
145
145
|
constructor(items: JsonSchemaBuilder[]);
|
|
146
146
|
}
|
|
147
|
-
declare function object<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props?:
|
|
147
|
+
declare function object<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props?: {
|
|
148
|
+
[K in keyof P]: P[K] & JsonSchemaAnyBuilder<any, any, any>;
|
|
149
|
+
}): JsonSchemaObjectBuilder<{
|
|
148
150
|
[K in keyof P as P[K] extends JsonSchemaAnyBuilder<any, any, infer Opt> ? Opt extends true ? never : K : never]: P[K] extends JsonSchemaAnyBuilder<infer U, any, any> ? U : never;
|
|
149
151
|
} & {
|
|
150
152
|
[K in keyof P as P[K] extends JsonSchemaAnyBuilder<any, any, infer Opt> ? Opt extends true ? K : never : never]?: P[K] extends JsonSchemaAnyBuilder<infer U, any, any> ? U : never;
|
|
@@ -392,7 +392,7 @@ export class JsonSchemaArrayBuilder extends JsonSchemaAnyBuilder {
|
|
|
392
392
|
Object.assign(this.schema, { maxItems });
|
|
393
393
|
return this;
|
|
394
394
|
}
|
|
395
|
-
unique(uniqueItems) {
|
|
395
|
+
unique(uniqueItems = true) {
|
|
396
396
|
Object.assign(this.schema, { uniqueItems });
|
|
397
397
|
return this;
|
|
398
398
|
}
|
package/dist/math/math.util.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { comparators } from '../array/sort.js';
|
|
1
2
|
import { _assert } from '../error/assert.js';
|
|
2
|
-
import { _sortNumbers } from '../number/number.util.js';
|
|
3
3
|
/**
|
|
4
4
|
* @returns Average of the array of numbers
|
|
5
5
|
*
|
|
@@ -46,7 +46,7 @@ export function _averageWeighted(values, weights) {
|
|
|
46
46
|
* // 3
|
|
47
47
|
*/
|
|
48
48
|
export function _percentile(values, pc) {
|
|
49
|
-
const sorted =
|
|
49
|
+
const sorted = values.sort(comparators.numericAsc);
|
|
50
50
|
// Floating pos in the range of [0; length - 1]
|
|
51
51
|
const pos = ((values.length - 1) * pc) / 100;
|
|
52
52
|
const dec = pos % 1;
|
|
@@ -59,7 +59,7 @@ export function _percentile(values, pc) {
|
|
|
59
59
|
*/
|
|
60
60
|
export function _percentiles(values, pcs) {
|
|
61
61
|
const r = {};
|
|
62
|
-
const sorted =
|
|
62
|
+
const sorted = values.sort(comparators.numericAsc);
|
|
63
63
|
for (const pc of pcs) {
|
|
64
64
|
// Floating pos in the range of [0; length - 1]
|
|
65
65
|
const pos = ((values.length - 1) * pc) / 100;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { SortOptions } from '../array/array.util.js';
|
|
2
1
|
import type { Inclusiveness } from '../types.js';
|
|
3
2
|
export declare function _randomInt(minIncl: number, maxIncl: number): number;
|
|
4
3
|
/**
|
|
@@ -29,16 +28,6 @@ export declare function _runLessOften(percent: number): boolean;
|
|
|
29
28
|
*/
|
|
30
29
|
export declare function _isBetween<T extends number | string>(x: T, min: T, max: T, incl: Inclusiveness): boolean;
|
|
31
30
|
export declare function _clamp(x: number, minIncl: number, maxIncl: number): number;
|
|
32
|
-
/**
|
|
33
|
-
* This function exists, because in JS you cannot just .sort() numbers,
|
|
34
|
-
* as .sort() function first maps everything to String.
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
*
|
|
38
|
-
* _sortNumbers([1, 3, 2])
|
|
39
|
-
* // [1, 2, 3]
|
|
40
|
-
*/
|
|
41
|
-
export declare function _sortNumbers(numbers: number[], opt?: SortOptions): number[];
|
|
42
31
|
/**
|
|
43
32
|
* Same as .toFixed(), but conveniently casts the output to Number.
|
|
44
33
|
*
|
|
@@ -43,19 +43,6 @@ export function _clamp(x, minIncl, maxIncl) {
|
|
|
43
43
|
// oxlint-disable-next-line unicorn/prefer-math-min-max
|
|
44
44
|
return x <= minIncl ? minIncl : x >= maxIncl ? maxIncl : x;
|
|
45
45
|
}
|
|
46
|
-
/**
|
|
47
|
-
* This function exists, because in JS you cannot just .sort() numbers,
|
|
48
|
-
* as .sort() function first maps everything to String.
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
*
|
|
52
|
-
* _sortNumbers([1, 3, 2])
|
|
53
|
-
* // [1, 2, 3]
|
|
54
|
-
*/
|
|
55
|
-
export function _sortNumbers(numbers, opt = {}) {
|
|
56
|
-
const mod = opt.dir === 'desc' ? -1 : 1;
|
|
57
|
-
return (opt.mutate ? numbers : [...numbers]).sort((a, b) => (a - b) * mod);
|
|
58
|
-
}
|
|
59
46
|
/**
|
|
60
47
|
* Same as .toFixed(), but conveniently casts the output to Number.
|
|
61
48
|
*
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
import type { Comparator } from '../types.js';
|
|
2
|
+
export interface KeySortedMapOptions<K> {
|
|
2
3
|
/**
|
|
3
|
-
* Defaults to
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Defaults to undefined.
|
|
5
|
+
* Undefined (default comparator) works well for String keys.
|
|
6
|
+
* For Number keys - use comparators.numericAsc (or desc),
|
|
7
|
+
* otherwise sorting will be wrong (lexicographic).
|
|
6
8
|
*/
|
|
7
|
-
|
|
9
|
+
comparator?: Comparator<K>;
|
|
8
10
|
}
|
|
9
11
|
/**
|
|
10
12
|
* Maintains sorted array of keys.
|
|
@@ -18,10 +20,10 @@ export interface KeySortedMapOptions {
|
|
|
18
20
|
* @experimental
|
|
19
21
|
*/
|
|
20
22
|
export declare class KeySortedMap<K, V> implements Map<K, V> {
|
|
21
|
-
opt: KeySortedMapOptions
|
|
23
|
+
opt: KeySortedMapOptions<K>;
|
|
22
24
|
private readonly map;
|
|
23
25
|
private readonly sortedKeys;
|
|
24
|
-
constructor(entries?: [K, V][], opt?: KeySortedMapOptions);
|
|
26
|
+
constructor(entries?: [K, V][], opt?: KeySortedMapOptions<K>);
|
|
25
27
|
/**
|
|
26
28
|
* Convenience way to create KeySortedMap from object.
|
|
27
29
|
*/
|
|
@@ -190,16 +190,6 @@ export class KeySortedMap {
|
|
|
190
190
|
return lo;
|
|
191
191
|
}
|
|
192
192
|
sortKeys() {
|
|
193
|
-
|
|
194
|
-
;
|
|
195
|
-
this.sortedKeys.sort(numericAscCompare);
|
|
196
|
-
}
|
|
197
|
-
else {
|
|
198
|
-
// Default sort - fastest for Strings
|
|
199
|
-
this.sortedKeys.sort();
|
|
200
|
-
}
|
|
193
|
+
this.sortedKeys.sort(this.opt.comparator);
|
|
201
194
|
}
|
|
202
195
|
}
|
|
203
|
-
function numericAscCompare(a, b) {
|
|
204
|
-
return a - b;
|
|
205
|
-
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { MutateOptions } from '../
|
|
2
|
-
import type { AnyObject, KeyValueTuple, ObjectMapper, ObjectPredicate, Reviver, StringMap, ValueOf } from '../types.js';
|
|
1
|
+
import type { AnyObject, KeyValueTuple, MutateOptions, ObjectMapper, ObjectPredicate, Reviver, StringMap, ValueOf } from '../types.js';
|
|
3
2
|
import { SKIP } from '../types.js';
|
|
4
3
|
/**
|
|
5
4
|
* Returns clone of `obj` with only `props` preserved.
|
package/dist/object/set2.d.ts
CHANGED
|
@@ -6,11 +6,15 @@
|
|
|
6
6
|
* @experimental
|
|
7
7
|
*/
|
|
8
8
|
export declare class Set2<T = any> extends Set<T> {
|
|
9
|
+
static of<T>(items?: Iterable<T> | null): Set2<T>;
|
|
9
10
|
/**
|
|
10
11
|
* Like .add(), but allows to add multiple items at once.
|
|
11
12
|
* Mutates the Set, but also returns it conveniently.
|
|
12
13
|
*/
|
|
13
14
|
addMany(items: Iterable<T>): this;
|
|
15
|
+
first(): T;
|
|
16
|
+
firstOrUndefined(): T | undefined;
|
|
14
17
|
toArray(): T[];
|
|
15
18
|
toJSON(): T[];
|
|
19
|
+
get [Symbol.toStringTag](): string;
|
|
16
20
|
}
|
package/dist/object/set2.js
CHANGED
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
* @experimental
|
|
7
7
|
*/
|
|
8
8
|
export class Set2 extends Set {
|
|
9
|
+
static of(items) {
|
|
10
|
+
return new Set2(items);
|
|
11
|
+
}
|
|
9
12
|
/**
|
|
10
13
|
* Like .add(), but allows to add multiple items at once.
|
|
11
14
|
* Mutates the Set, but also returns it conveniently.
|
|
@@ -16,10 +19,23 @@ export class Set2 extends Set {
|
|
|
16
19
|
}
|
|
17
20
|
return this;
|
|
18
21
|
}
|
|
22
|
+
first() {
|
|
23
|
+
if (!this.size)
|
|
24
|
+
throw new Error('Set.first called on empty set');
|
|
25
|
+
return this.firstOrUndefined();
|
|
26
|
+
}
|
|
27
|
+
firstOrUndefined() {
|
|
28
|
+
return this.values().next().value;
|
|
29
|
+
}
|
|
30
|
+
// Last is not implemented, because it requires to traverse the whole Set - not optimal
|
|
31
|
+
// last(): T {
|
|
19
32
|
toArray() {
|
|
20
33
|
return [...this];
|
|
21
34
|
}
|
|
22
35
|
toJSON() {
|
|
23
36
|
return [...this];
|
|
24
37
|
}
|
|
38
|
+
get [Symbol.toStringTag]() {
|
|
39
|
+
return 'Set';
|
|
40
|
+
}
|
|
25
41
|
}
|
package/dist/semver.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { SortOptions } from './
|
|
1
|
+
import type { SortOptions } from './types.js';
|
|
2
2
|
export type SemverInput = string | Semver;
|
|
3
3
|
export type SemverInputNullable = SemverInput | null | undefined;
|
|
4
4
|
export type SemverTokens = [major: number, minor: number, patch: number];
|