atomic-fns 0.3.1 → 0.3.3
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/.esdoc.json +2 -2
- package/README.md +48 -13
- package/core/collections/BTree.d.ts +229 -0
- package/core/collections/BTree.js +1165 -0
- package/core/collections/BTree.ts +1340 -0
- package/core/collections/Heap.d.ts +69 -0
- package/core/collections/Heap.js +181 -0
- package/core/collections/Heap.ts +189 -0
- package/core/collections/LRUCache.d.ts +131 -0
- package/core/collections/LRUCache.js +388 -0
- package/core/collections/LRUCache.ts +465 -0
- package/core/collections/SortedMap.d.ts +92 -0
- package/core/collections/SortedMap.js +130 -0
- package/core/collections/SortedMap.ts +146 -0
- package/core/collections/SortedSet.d.ts +63 -0
- package/core/collections/SortedSet.js +93 -0
- package/core/collections/SortedSet.ts +104 -0
- package/core/collections/SortedTree.d.ts +103 -0
- package/core/collections/SortedTree.js +141 -0
- package/core/collections/SortedTree.ts +160 -0
- package/core/collections/SplayTree.d.ts +105 -0
- package/core/collections/SplayTree.js +338 -0
- package/core/collections/SplayTree.ts +354 -0
- package/core/collections/abc.d.ts +152 -0
- package/core/collections/abc.js +39 -0
- package/core/collections/abc.ts +176 -0
- package/core/collections/deque.d.ts +110 -0
- package/core/collections/deque.js +285 -0
- package/core/collections/deque.ts +329 -0
- package/core/collections/frozenset.d.ts +19 -0
- package/core/collections/frozenset.js +33 -0
- package/core/collections/frozenset.ts +37 -0
- package/{src → core}/collections/index.d.ts +41 -162
- package/{src → core}/collections/index.js +57 -165
- package/{src → core}/collections/index.ts +61 -225
- package/core/collections/test.html +10 -0
- package/core/collections/typedArrays.d.ts +31 -0
- package/core/collections/typedArrays.js +143 -0
- package/core/collections/typedArrays.ts +163 -0
- package/core/datetime/index.d.ts +45 -0
- package/core/datetime/index.js +71 -0
- package/core/datetime/index.ts +128 -0
- package/{src → core}/functools/index.d.ts +2 -2
- package/{src → core}/functools/index.js +2 -2
- package/{src → core}/functools/index.ts +2 -2
- package/{src → core}/globals/index.d.ts +2 -7
- package/{src → core}/globals/index.js +5 -2
- package/{src → core}/globals/index.ts +6 -9
- package/{src → core}/index.d.ts +0 -1
- package/{src → core}/index.js +0 -1
- package/{src → core}/index.ts +0 -1
- package/{src → core}/itertools/index.d.ts +9 -7
- package/{src → core}/itertools/index.js +25 -9
- package/{src → core}/itertools/index.ts +39 -16
- package/{src → core}/math/index.d.ts +0 -0
- package/{src → core}/math/index.js +0 -0
- package/{src → core}/math/index.ts +0 -0
- package/core/operators/index.d.ts +148 -0
- package/core/operators/index.js +271 -0
- package/core/operators/index.ts +282 -0
- package/{src → core}/random/index.d.ts +0 -0
- package/{src → core}/random/index.js +0 -0
- package/{src → core}/random/index.ts +0 -0
- package/package.json +6 -6
- package/tests/BTree.spec.ts +143 -0
- package/tests/Cache.spec.ts +161 -0
- package/tests/Heap.spec.ts +66 -0
- package/tests/SortedMap.spec.ts +144 -0
- package/tests/SortedSet.spec.ts +84 -0
- package/tests/SortedTree.spec.ts +164 -0
- package/tests/SplayTree.spec.ts +121 -0
- package/tests/collections.spec.ts +1 -1
- package/tests/deque.spec.ts +107 -0
- package/tests/globals.spec.ts +2 -2
- package/tests/itertools.spec.ts +2 -2
- package/tests/math.spec.ts +1 -1
- package/tests/operators.spec.ts +5 -5
- package/tests/random.spec.ts +10 -10
- package/tsconfig.json +10 -10
- package/tsconfig.node.json +2 -2
- package/tsconfig.xo.json +2 -2
- package/src/datetime/index.d.ts +0 -6
- package/src/datetime/index.js +0 -6
- package/src/datetime/index.ts +0 -7
- package/src/operators/index.d.ts +0 -31
- package/src/operators/index.js +0 -171
- package/src/operators/index.ts +0 -175
|
@@ -6,15 +6,15 @@
|
|
|
6
6
|
|
|
7
7
|
import {
|
|
8
8
|
call,
|
|
9
|
-
Comp,
|
|
10
9
|
Function,
|
|
11
10
|
isEmpty,
|
|
12
11
|
isObject,
|
|
12
|
+
Iteratee,
|
|
13
13
|
notNull,
|
|
14
14
|
Predicate,
|
|
15
15
|
ValueError
|
|
16
16
|
} from '../globals/index.js'
|
|
17
|
-
import { add, bool,
|
|
17
|
+
import { add, bool, compare, Comparer, eq, id } from '../operators/index.js'
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional `func` argument).
|
|
@@ -119,7 +119,7 @@ export function contains(collection: Iterable<any>, value) {
|
|
|
119
119
|
return any(collection, (x) => eq(value, x))
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
const compKey = (
|
|
122
|
+
const compKey = (compare: Comparer, key) => (x, y) => compare(key(x), key(y))
|
|
123
123
|
|
|
124
124
|
/**
|
|
125
125
|
* Returns a generator that drops elements from the iterable as long as the predicate is `true`; afterwards, returns every remaining element. Note, the generator does not produce any output until the predicate first becomes `false`.
|
|
@@ -144,6 +144,24 @@ export function* dropWhile<T>(iterable: Iterable<T>, predicate: Predicate) {
|
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
+
export function iforEach<T>(iterable: Iterable<T>, fun: Iteratee<T>) {
|
|
148
|
+
for (const [index, value] of enumerate(iterable)) {
|
|
149
|
+
if (fun(value as T, index, iterable) === false) {
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function IterableIterator<T = any>(next) {
|
|
156
|
+
const it: Iterator<T> & Iterable<T> = {
|
|
157
|
+
next,
|
|
158
|
+
[Symbol.iterator]() {
|
|
159
|
+
return this
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return it
|
|
163
|
+
}
|
|
164
|
+
|
|
147
165
|
/**
|
|
148
166
|
* Yields elements like `[index, item]` from an iterable. Index starts at zero by default.
|
|
149
167
|
* @template T
|
|
@@ -177,7 +195,7 @@ export function first<T>(iterable: Iterable<T>) {
|
|
|
177
195
|
}
|
|
178
196
|
|
|
179
197
|
/**
|
|
180
|
-
* Creates a new generator that filters all
|
|
198
|
+
* Creates a new generator that filters all falsy and empty values from iterable.
|
|
181
199
|
* @param arr The array to compact
|
|
182
200
|
* @returns A new array with the filtered values.
|
|
183
201
|
* @example
|
|
@@ -201,7 +219,7 @@ export function* ifilter<T>(iterable: Iterable<T>, predicate: Predicate) {
|
|
|
201
219
|
}
|
|
202
220
|
}
|
|
203
221
|
|
|
204
|
-
export function* imap<T>(iterable: Iterable<T>, mapFn: (x: T) =>
|
|
222
|
+
export function* imap<T, TReturn = any>(iterable: Iterable<T>, mapFn: (x: T) => TReturn) {
|
|
205
223
|
for (const item of iterable) {
|
|
206
224
|
yield mapFn(item)
|
|
207
225
|
}
|
|
@@ -352,7 +370,7 @@ export function reversed<T>(iterable: Iterable<T>) {
|
|
|
352
370
|
|
|
353
371
|
export function sorted(args: any[]): any[]
|
|
354
372
|
export function sorted(args: any[], reverse: boolean): any[]
|
|
355
|
-
export function sorted(args: any[], reverse: boolean, comp:
|
|
373
|
+
export function sorted(args: any[], reverse: boolean, comp: Comparer): any[]
|
|
356
374
|
export function sorted(args: any[], key: Function): any[]
|
|
357
375
|
export function sorted(args: any[], key: Function, reverse: boolean): any[]
|
|
358
376
|
|
|
@@ -369,11 +387,16 @@ export function sorted(args: any[], key: Function, reverse: boolean): any[]
|
|
|
369
387
|
*
|
|
370
388
|
* @param {(Array | Object)} args
|
|
371
389
|
* @param {(boolean | Function)} [key]
|
|
372
|
-
* @param {(boolean |
|
|
373
|
-
* @param {
|
|
390
|
+
* @param {(boolean | Comparer)} [reverse]
|
|
391
|
+
* @param {Comparer} [compareFn]
|
|
374
392
|
* @return
|
|
375
393
|
*/
|
|
376
|
-
export function sorted(
|
|
394
|
+
export function sorted(
|
|
395
|
+
args,
|
|
396
|
+
key?: boolean | Function,
|
|
397
|
+
reverse?: boolean | Comparer,
|
|
398
|
+
compareFn?: Comparer
|
|
399
|
+
) {
|
|
377
400
|
if (isObject(args)) args = Object.keys(args)
|
|
378
401
|
if (typeof key === 'boolean') {
|
|
379
402
|
if (typeof reverse === 'function') {
|
|
@@ -385,7 +408,7 @@ export function sorted(args, key?: boolean | Function, reverse?: boolean | Comp,
|
|
|
385
408
|
key = id
|
|
386
409
|
}
|
|
387
410
|
const copy = Array.from(args)
|
|
388
|
-
const _compare = compKey(compareFn ||
|
|
411
|
+
const _compare = compKey(compareFn || compare, key || id)
|
|
389
412
|
copy.sort(_compare)
|
|
390
413
|
if (reverse) copy.reverse()
|
|
391
414
|
return copy
|
|
@@ -393,8 +416,8 @@ export function sorted(args, key?: boolean | Function, reverse?: boolean | Comp,
|
|
|
393
416
|
|
|
394
417
|
export function sort(args: any[]): any[]
|
|
395
418
|
export function sort(args: any[], reverse: boolean): any[]
|
|
396
|
-
export function sort(args: any[], compareFn:
|
|
397
|
-
export function sort(args: any[], reverse: boolean, compareFn:
|
|
419
|
+
export function sort(args: any[], compareFn: Comparer): any[]
|
|
420
|
+
export function sort(args: any[], reverse: boolean, compareFn: Comparer): any[]
|
|
398
421
|
|
|
399
422
|
/**
|
|
400
423
|
* Sort `args` in place. Can be called like this:
|
|
@@ -403,16 +426,16 @@ export function sort(args: any[], reverse: boolean, compareFn: Comp): any[]
|
|
|
403
426
|
* - sort([...], true, (x, y) => number) => reverse and using custom compare
|
|
404
427
|
*
|
|
405
428
|
* @param {any[]} args
|
|
406
|
-
* @param {(boolean |
|
|
407
|
-
* @param {
|
|
429
|
+
* @param {(boolean | Comparer)} [reverse]
|
|
430
|
+
* @param {Comparer} [compareFn]
|
|
408
431
|
* @return
|
|
409
432
|
*/
|
|
410
|
-
export function sort(args: any[], reverse?: boolean |
|
|
433
|
+
export function sort(args: any[], reverse?: boolean | Comparer, compareFn?: Comparer) {
|
|
411
434
|
if (typeof reverse === 'function') {
|
|
412
435
|
compareFn = reverse
|
|
413
436
|
reverse = false
|
|
414
437
|
}
|
|
415
|
-
args.sort(compareFn ||
|
|
438
|
+
args.sort(compareFn || compare)
|
|
416
439
|
if (reverse === true) args.reverse()
|
|
417
440
|
return args
|
|
418
441
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic operations for both values and objects.
|
|
3
|
+
*
|
|
4
|
+
* @module Operators
|
|
5
|
+
*/
|
|
6
|
+
/** This is the identity function. It always returns the same value that was passed in */
|
|
7
|
+
export declare const id: (x: any) => any;
|
|
8
|
+
/**
|
|
9
|
+
* Called to implement truth value testing.
|
|
10
|
+
* @param x
|
|
11
|
+
* @returns {boolean} `x.bool()` if exists or `!!x`.
|
|
12
|
+
*/
|
|
13
|
+
export declare const bool: (x: any) => any;
|
|
14
|
+
/**
|
|
15
|
+
* Returns `true` if `x` is a falsy value.
|
|
16
|
+
* @see {@link bool}
|
|
17
|
+
*/
|
|
18
|
+
export declare const not: (x: any) => boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Returns `true` if `x` is an instance of class `y`
|
|
21
|
+
* @param x The instance object
|
|
22
|
+
* @param y The parent class
|
|
23
|
+
* @returns {boolean}
|
|
24
|
+
*/
|
|
25
|
+
export declare const isinstance: (x: any, y: any) => boolean;
|
|
26
|
+
/** Describes a function used to compare two values.
|
|
27
|
+
*
|
|
28
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters Array.sort()}
|
|
29
|
+
*/
|
|
30
|
+
export declare type Comparer<T = any> = (x: T, y: T) => number;
|
|
31
|
+
/**
|
|
32
|
+
* Checks whether `x` is a comparable type and returns the result of `x.compare(y)`.
|
|
33
|
+
* Otherwise the return value of the compare function is equivalent to `obj === other ? 0 : obj < other ? -1 : 1`
|
|
34
|
+
* @param {*} x An initial value
|
|
35
|
+
* @param {*} y Other value to compare
|
|
36
|
+
* @returns {number} The comparison result
|
|
37
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters Array.sort()}
|
|
38
|
+
* @see {@link eq}
|
|
39
|
+
* @see {@link lt}
|
|
40
|
+
*/
|
|
41
|
+
export declare function compare(x: any, y: any): number;
|
|
42
|
+
/**
|
|
43
|
+
* Compares two values to check if they are the same. If `x` is an object with an `eq` method, it returns `x.eq(y)`.
|
|
44
|
+
* Otherwise it returns the result of `x === y`.
|
|
45
|
+
* @param {*} x
|
|
46
|
+
* @param {*} y
|
|
47
|
+
* @returns {boolean} `true` if `x` and `y` are equal or `false` otherwise.
|
|
48
|
+
*/
|
|
49
|
+
export declare function eq(x: any, y: any): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Compares two values to check if `x` is strictly less than `y`. If `x` is an object with a `lt` method, it returns `x.lt(y)`.
|
|
52
|
+
* Otherwise it returns the result of `x < y`.
|
|
53
|
+
* @param {*} x
|
|
54
|
+
* @param {*} y
|
|
55
|
+
* @returns {boolean} `true` if `x` is strictly less than `y` or `false` otherwise.
|
|
56
|
+
*/
|
|
57
|
+
export declare function lt(x: any, y: any): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Compares two values to check if `x <= y`. If `x` is an object with a `lte` method, it returns `x.lte(y)`, also checks `x.eq(y)` and `x.lt(y)`.
|
|
60
|
+
* Otherwise it returns the result of `x <= y`.
|
|
61
|
+
* @param {*} x
|
|
62
|
+
* @param {*} y
|
|
63
|
+
* @returns {boolean} `true` if `x` is less than or equal to `y` or `false` otherwise.
|
|
64
|
+
* @see {@link eq}
|
|
65
|
+
* @see {@link lt}
|
|
66
|
+
*/
|
|
67
|
+
export declare function lte(x: any, y: any): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Compares two values to check if `x` is strictly greater than `y`. If `x` is an object with a `gt` method, it returns `x.gt(y)`.
|
|
70
|
+
* Otherwise it returns the result of `x > y`.
|
|
71
|
+
* @param {*} x
|
|
72
|
+
* @param {*} y
|
|
73
|
+
* @returns {boolean} `true` if `x` is strictly greater than `y` or `false` otherwise.
|
|
74
|
+
*/
|
|
75
|
+
export declare function gt(x: any, y: any): any;
|
|
76
|
+
/**
|
|
77
|
+
* Compares two values to check if `x >= y`. If `x` is an object with a `gte` method, it returns `x.gte(y)`, also checks `x.eq(y)` and `x.gt(y)`.
|
|
78
|
+
* Otherwise it returns the result of `x >= y`.
|
|
79
|
+
* @param {*} x
|
|
80
|
+
* @param {*} y
|
|
81
|
+
* @returns {boolean} `true` if `x` is less greater than or equal to `y` or `false` otherwise.
|
|
82
|
+
* @see {@link eq}
|
|
83
|
+
* @see {@link gt}
|
|
84
|
+
*/
|
|
85
|
+
export declare function gte(x: any, y: any): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* The addition (+) operator. If `x` is an object with an `add` method, it returns `x.add(y)`.
|
|
88
|
+
* Otherwise it returns the result of `x + y`.
|
|
89
|
+
* @param {*} x
|
|
90
|
+
* @param {*} y
|
|
91
|
+
* @returns {*} x + y
|
|
92
|
+
*/
|
|
93
|
+
export declare function add(x: any, y: any): any;
|
|
94
|
+
/**
|
|
95
|
+
* The subtraction (-) operator. If `x` is an object with a `sub` method, it returns `x.sub(y)`.
|
|
96
|
+
* Otherwise it returns the result of `x - y`.
|
|
97
|
+
* @param {*} x
|
|
98
|
+
* @param {*} y
|
|
99
|
+
* @returns {*} x - y
|
|
100
|
+
*/
|
|
101
|
+
export declare function sub(x: any, y: any): any;
|
|
102
|
+
/**
|
|
103
|
+
* The multiplication (*) operator. If `x` is an object with an `mul` method, it returns `x.mul(y)`.
|
|
104
|
+
* Otherwise it returns the result of `x * y`.
|
|
105
|
+
* @param {*} x
|
|
106
|
+
* @param {*} y
|
|
107
|
+
* @returns {*} x * y
|
|
108
|
+
*/
|
|
109
|
+
export declare function mul(x: any, y: any): any;
|
|
110
|
+
/**
|
|
111
|
+
* The division (/) operator. If `x` is an object with a `div` method, it returns `x.div(y)`.
|
|
112
|
+
* Otherwise it returns the result of `x / y`.
|
|
113
|
+
* @param {*} x
|
|
114
|
+
* @param {*} y
|
|
115
|
+
* @returns {*} x / y
|
|
116
|
+
*/
|
|
117
|
+
export declare function div(x: any, y: any): any;
|
|
118
|
+
/**
|
|
119
|
+
* The modulo (%) operator. If `x` is an object with a `mod` method, it returns `x.mod(y)`.
|
|
120
|
+
* Otherwise it returns the result of `x % y`.
|
|
121
|
+
* @param {*} x
|
|
122
|
+
* @param {*} y
|
|
123
|
+
* @returns {*} x % y
|
|
124
|
+
*/
|
|
125
|
+
export declare function mod(x: any, y: any): any;
|
|
126
|
+
/**
|
|
127
|
+
* The power (**) operator. If `x` is an object with a `pow` method, it returns `x.pow(y)`.
|
|
128
|
+
* Otherwise it returns the result of `x ** y`.
|
|
129
|
+
* @param {*} x
|
|
130
|
+
* @param {*} y
|
|
131
|
+
* @returns {*} x ** y
|
|
132
|
+
*/
|
|
133
|
+
export declare function pow(x: any, y: any): any;
|
|
134
|
+
/**
|
|
135
|
+
* Performs a shallow comparison of two objects or arrays to check if they have the same keys, length and values.
|
|
136
|
+
* @param obj
|
|
137
|
+
* @param other
|
|
138
|
+
* @returns `true` if the objects are considered equal.
|
|
139
|
+
* @see {@link deepEqual}
|
|
140
|
+
*/
|
|
141
|
+
export declare function shallowEqual(obj: any, other: any): boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Performs a recursive comparison of two objects or arrays to check if they have the same keys, length and values. Unlike {@link shallowEqual} this method compares nested values also.
|
|
144
|
+
* @param obj
|
|
145
|
+
* @param other
|
|
146
|
+
* @returns `true` if the objects are considered equal.
|
|
147
|
+
*/
|
|
148
|
+
export declare function deepEqual(obj: any, other: any, checker?: typeof eq): boolean;
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic operations for both values and objects.
|
|
3
|
+
*
|
|
4
|
+
* @module Operators
|
|
5
|
+
*/
|
|
6
|
+
import { call, isObject } from '../globals/index.js';
|
|
7
|
+
/** This is the identity function. It always returns the same value that was passed in */
|
|
8
|
+
export const id = (x) => x;
|
|
9
|
+
/**
|
|
10
|
+
* Called to implement truth value testing.
|
|
11
|
+
* @param x
|
|
12
|
+
* @returns {boolean} `x.bool()` if exists or `!!x`.
|
|
13
|
+
*/
|
|
14
|
+
export const bool = (x) => {
|
|
15
|
+
const r = call(x, 'bool');
|
|
16
|
+
if (r != null)
|
|
17
|
+
return r;
|
|
18
|
+
return !!x;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Returns `true` if `x` is a falsy value.
|
|
22
|
+
* @see {@link bool}
|
|
23
|
+
*/
|
|
24
|
+
export const not = (x) => !bool(x);
|
|
25
|
+
/**
|
|
26
|
+
* Returns `true` if `x` is an instance of class `y`
|
|
27
|
+
* @param x The instance object
|
|
28
|
+
* @param y The parent class
|
|
29
|
+
* @returns {boolean}
|
|
30
|
+
*/
|
|
31
|
+
export const isinstance = (x, y) => x instanceof y;
|
|
32
|
+
/**
|
|
33
|
+
* Checks whether `x` is a comparable type and returns the result of `x.compare(y)`.
|
|
34
|
+
* Otherwise the return value of the compare function is equivalent to `obj === other ? 0 : obj < other ? -1 : 1`
|
|
35
|
+
* @param {*} x An initial value
|
|
36
|
+
* @param {*} y Other value to compare
|
|
37
|
+
* @returns {number} The comparison result
|
|
38
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters Array.sort()}
|
|
39
|
+
* @see {@link eq}
|
|
40
|
+
* @see {@link lt}
|
|
41
|
+
*/
|
|
42
|
+
export function compare(x, y) {
|
|
43
|
+
if (x === y || eq(x, y))
|
|
44
|
+
return 0;
|
|
45
|
+
const op = call(x, 'compare', y);
|
|
46
|
+
if (typeof op === 'number')
|
|
47
|
+
return op;
|
|
48
|
+
if (x < y || lt(x, y))
|
|
49
|
+
return -1;
|
|
50
|
+
return 1;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Compares two values to check if they are the same. If `x` is an object with an `eq` method, it returns `x.eq(y)`.
|
|
54
|
+
* Otherwise it returns the result of `x === y`.
|
|
55
|
+
* @param {*} x
|
|
56
|
+
* @param {*} y
|
|
57
|
+
* @returns {boolean} `true` if `x` and `y` are equal or `false` otherwise.
|
|
58
|
+
*/
|
|
59
|
+
export function eq(x, y) {
|
|
60
|
+
return x === y || call(x, 'eq', y) != null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Compares two values to check if `x` is strictly less than `y`. If `x` is an object with a `lt` method, it returns `x.lt(y)`.
|
|
64
|
+
* Otherwise it returns the result of `x < y`.
|
|
65
|
+
* @param {*} x
|
|
66
|
+
* @param {*} y
|
|
67
|
+
* @returns {boolean} `true` if `x` is strictly less than `y` or `false` otherwise.
|
|
68
|
+
*/
|
|
69
|
+
export function lt(x, y) {
|
|
70
|
+
return x < y || call(x, 'lt', y) != null;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Compares two values to check if `x <= y`. If `x` is an object with a `lte` method, it returns `x.lte(y)`, also checks `x.eq(y)` and `x.lt(y)`.
|
|
74
|
+
* Otherwise it returns the result of `x <= y`.
|
|
75
|
+
* @param {*} x
|
|
76
|
+
* @param {*} y
|
|
77
|
+
* @returns {boolean} `true` if `x` is less than or equal to `y` or `false` otherwise.
|
|
78
|
+
* @see {@link eq}
|
|
79
|
+
* @see {@link lt}
|
|
80
|
+
*/
|
|
81
|
+
export function lte(x, y) {
|
|
82
|
+
if (x <= y || call(x, 'lte', y))
|
|
83
|
+
return true;
|
|
84
|
+
if (x === y || eq(x, y))
|
|
85
|
+
return true;
|
|
86
|
+
if (x < y || lt(x, y))
|
|
87
|
+
return true;
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Compares two values to check if `x` is strictly greater than `y`. If `x` is an object with a `gt` method, it returns `x.gt(y)`.
|
|
92
|
+
* Otherwise it returns the result of `x > y`.
|
|
93
|
+
* @param {*} x
|
|
94
|
+
* @param {*} y
|
|
95
|
+
* @returns {boolean} `true` if `x` is strictly greater than `y` or `false` otherwise.
|
|
96
|
+
*/
|
|
97
|
+
export function gt(x, y) {
|
|
98
|
+
return x > y || call(x, 'gt', y);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Compares two values to check if `x >= y`. If `x` is an object with a `gte` method, it returns `x.gte(y)`, also checks `x.eq(y)` and `x.gt(y)`.
|
|
102
|
+
* Otherwise it returns the result of `x >= y`.
|
|
103
|
+
* @param {*} x
|
|
104
|
+
* @param {*} y
|
|
105
|
+
* @returns {boolean} `true` if `x` is less greater than or equal to `y` or `false` otherwise.
|
|
106
|
+
* @see {@link eq}
|
|
107
|
+
* @see {@link gt}
|
|
108
|
+
*/
|
|
109
|
+
export function gte(x, y) {
|
|
110
|
+
if (x <= y || call(x, 'gte', y))
|
|
111
|
+
return true;
|
|
112
|
+
if (x === y || eq(x, y))
|
|
113
|
+
return true;
|
|
114
|
+
if (x > y || gt(x, y))
|
|
115
|
+
return true;
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The addition (+) operator. If `x` is an object with an `add` method, it returns `x.add(y)`.
|
|
120
|
+
* Otherwise it returns the result of `x + y`.
|
|
121
|
+
* @param {*} x
|
|
122
|
+
* @param {*} y
|
|
123
|
+
* @returns {*} x + y
|
|
124
|
+
*/
|
|
125
|
+
export function add(x, y) {
|
|
126
|
+
const op = call(x, 'add', y);
|
|
127
|
+
if (op != null)
|
|
128
|
+
return op;
|
|
129
|
+
return x + y;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The subtraction (-) operator. If `x` is an object with a `sub` method, it returns `x.sub(y)`.
|
|
133
|
+
* Otherwise it returns the result of `x - y`.
|
|
134
|
+
* @param {*} x
|
|
135
|
+
* @param {*} y
|
|
136
|
+
* @returns {*} x - y
|
|
137
|
+
*/
|
|
138
|
+
export function sub(x, y) {
|
|
139
|
+
const op = call(x, 'sub', y);
|
|
140
|
+
if (op != null)
|
|
141
|
+
return op;
|
|
142
|
+
return x - y;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* The multiplication (*) operator. If `x` is an object with an `mul` method, it returns `x.mul(y)`.
|
|
146
|
+
* Otherwise it returns the result of `x * y`.
|
|
147
|
+
* @param {*} x
|
|
148
|
+
* @param {*} y
|
|
149
|
+
* @returns {*} x * y
|
|
150
|
+
*/
|
|
151
|
+
export function mul(x, y) {
|
|
152
|
+
const op = call(x, 'mul', y);
|
|
153
|
+
if (op != null)
|
|
154
|
+
return op;
|
|
155
|
+
return x * y;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The division (/) operator. If `x` is an object with a `div` method, it returns `x.div(y)`.
|
|
159
|
+
* Otherwise it returns the result of `x / y`.
|
|
160
|
+
* @param {*} x
|
|
161
|
+
* @param {*} y
|
|
162
|
+
* @returns {*} x / y
|
|
163
|
+
*/
|
|
164
|
+
export function div(x, y) {
|
|
165
|
+
const op = call(x, 'div', y);
|
|
166
|
+
if (op != null)
|
|
167
|
+
return op;
|
|
168
|
+
return x / y;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The modulo (%) operator. If `x` is an object with a `mod` method, it returns `x.mod(y)`.
|
|
172
|
+
* Otherwise it returns the result of `x % y`.
|
|
173
|
+
* @param {*} x
|
|
174
|
+
* @param {*} y
|
|
175
|
+
* @returns {*} x % y
|
|
176
|
+
*/
|
|
177
|
+
export function mod(x, y) {
|
|
178
|
+
const op = call(x, 'mod', y);
|
|
179
|
+
if (op != null)
|
|
180
|
+
return op;
|
|
181
|
+
return x % y;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* The power (**) operator. If `x` is an object with a `pow` method, it returns `x.pow(y)`.
|
|
185
|
+
* Otherwise it returns the result of `x ** y`.
|
|
186
|
+
* @param {*} x
|
|
187
|
+
* @param {*} y
|
|
188
|
+
* @returns {*} x ** y
|
|
189
|
+
*/
|
|
190
|
+
export function pow(x, y) {
|
|
191
|
+
const op = call(x, 'pow', y);
|
|
192
|
+
if (op != null)
|
|
193
|
+
return op;
|
|
194
|
+
return x ** y;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Performs a shallow comparison of two objects or arrays to check if they have the same keys, length and values.
|
|
198
|
+
* @param obj
|
|
199
|
+
* @param other
|
|
200
|
+
* @returns `true` if the objects are considered equal.
|
|
201
|
+
* @see {@link deepEqual}
|
|
202
|
+
*/
|
|
203
|
+
export function shallowEqual(obj, other) {
|
|
204
|
+
if (obj === other)
|
|
205
|
+
return true;
|
|
206
|
+
if (!obj || !other)
|
|
207
|
+
return false;
|
|
208
|
+
if (Array.isArray(obj)) {
|
|
209
|
+
// compare the arrays
|
|
210
|
+
if (obj.length !== other.length)
|
|
211
|
+
return false;
|
|
212
|
+
for (let i = 0; i < obj.length; i++) {
|
|
213
|
+
if (obj[i] !== other[i]) {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
else if (isObject(obj)) {
|
|
220
|
+
// compare the object keys
|
|
221
|
+
const objKeys = Object.keys(obj);
|
|
222
|
+
const otherKeys = Object.keys(other);
|
|
223
|
+
if (objKeys.length !== otherKeys.length)
|
|
224
|
+
return false;
|
|
225
|
+
for (const key of objKeys) {
|
|
226
|
+
if (!Object.prototype.hasOwnProperty.call(other, key) || obj[key] !== other[key]) {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Performs a recursive comparison of two objects or arrays to check if they have the same keys, length and values. Unlike {@link shallowEqual} this method compares nested values also.
|
|
236
|
+
* @param obj
|
|
237
|
+
* @param other
|
|
238
|
+
* @returns `true` if the objects are considered equal.
|
|
239
|
+
*/
|
|
240
|
+
export function deepEqual(obj, other, checker = eq) {
|
|
241
|
+
if (checker(obj, other))
|
|
242
|
+
return true;
|
|
243
|
+
if (!obj || !other)
|
|
244
|
+
return false;
|
|
245
|
+
if (Array.isArray(obj)) {
|
|
246
|
+
// compare the arrays
|
|
247
|
+
if (obj.length !== other.length)
|
|
248
|
+
return false;
|
|
249
|
+
for (let i = 0; i < obj.length; i++) {
|
|
250
|
+
if (!deepEqual(obj[i], other[i], checker)) {
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
else if (isObject(obj)) {
|
|
257
|
+
// compare the object keys
|
|
258
|
+
const objKeys = Object.keys(obj);
|
|
259
|
+
const otherKeys = Object.keys(other);
|
|
260
|
+
if (objKeys.length !== otherKeys.length)
|
|
261
|
+
return false;
|
|
262
|
+
for (const key of objKeys) {
|
|
263
|
+
if (!Object.prototype.hasOwnProperty.call(other, key) ||
|
|
264
|
+
!deepEqual(obj[key], other[key], checker)) {
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
return false;
|
|
271
|
+
}
|