atomic-fns 0.3.1 → 0.3.3-beta

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.
Files changed (84) hide show
  1. package/.esdoc.json +2 -2
  2. package/README.md +62 -13
  3. package/core/collections/BTree.d.ts +229 -0
  4. package/core/collections/BTree.js +1165 -0
  5. package/core/collections/BTree.ts +1340 -0
  6. package/core/collections/Heap.d.ts +69 -0
  7. package/core/collections/Heap.js +181 -0
  8. package/core/collections/Heap.ts +189 -0
  9. package/core/collections/LRUCache.d.ts +152 -0
  10. package/core/collections/LRUCache.js +409 -0
  11. package/core/collections/LRUCache.ts +486 -0
  12. package/core/collections/SortedMap.d.ts +92 -0
  13. package/core/collections/SortedMap.js +130 -0
  14. package/core/collections/SortedMap.ts +146 -0
  15. package/core/collections/SortedSet.d.ts +69 -0
  16. package/core/collections/SortedSet.js +99 -0
  17. package/core/collections/SortedSet.ts +110 -0
  18. package/core/collections/SortedTree.d.ts +103 -0
  19. package/core/collections/SortedTree.js +141 -0
  20. package/core/collections/SortedTree.ts +160 -0
  21. package/core/collections/SplayTree.d.ts +105 -0
  22. package/core/collections/SplayTree.js +338 -0
  23. package/core/collections/SplayTree.ts +354 -0
  24. package/core/collections/abc.d.ts +152 -0
  25. package/core/collections/abc.js +39 -0
  26. package/core/collections/abc.ts +176 -0
  27. package/core/collections/deque.d.ts +110 -0
  28. package/core/collections/deque.js +285 -0
  29. package/core/collections/deque.ts +329 -0
  30. package/core/collections/frozenset.d.ts +19 -0
  31. package/core/collections/frozenset.js +33 -0
  32. package/core/collections/frozenset.ts +37 -0
  33. package/{src → core}/collections/index.d.ts +43 -162
  34. package/{src → core}/collections/index.js +59 -165
  35. package/{src → core}/collections/index.ts +64 -226
  36. package/core/collections/test.html +10 -0
  37. package/core/collections/typedArrays.d.ts +31 -0
  38. package/core/collections/typedArrays.js +143 -0
  39. package/core/collections/typedArrays.ts +163 -0
  40. package/{src → core}/functools/index.d.ts +2 -2
  41. package/{src → core}/functools/index.js +2 -2
  42. package/{src → core}/functools/index.ts +2 -2
  43. package/{src → core}/globals/index.d.ts +2 -7
  44. package/{src → core}/globals/index.js +5 -2
  45. package/{src → core}/globals/index.ts +6 -9
  46. package/{src → core}/index.d.ts +0 -1
  47. package/{src → core}/index.js +0 -1
  48. package/{src → core}/index.ts +0 -1
  49. package/{src → core}/itertools/index.d.ts +9 -7
  50. package/{src → core}/itertools/index.js +25 -9
  51. package/{src → core}/itertools/index.ts +39 -16
  52. package/{src → core}/math/index.d.ts +0 -0
  53. package/{src → core}/math/index.js +0 -0
  54. package/{src → core}/math/index.ts +0 -0
  55. package/core/operators/index.d.ts +148 -0
  56. package/core/operators/index.js +271 -0
  57. package/core/operators/index.ts +282 -0
  58. package/{src → core}/random/index.d.ts +0 -0
  59. package/{src → core}/random/index.js +0 -0
  60. package/{src → core}/random/index.ts +0 -0
  61. package/package.json +6 -6
  62. package/tests/BTree.spec.ts +143 -0
  63. package/tests/Cache.spec.ts +161 -0
  64. package/tests/Heap.spec.ts +66 -0
  65. package/tests/SortedMap.spec.ts +144 -0
  66. package/tests/SortedSet.spec.ts +84 -0
  67. package/tests/SortedTree.spec.ts +164 -0
  68. package/tests/SplayTree.spec.ts +121 -0
  69. package/tests/collections.spec.ts +1 -1
  70. package/tests/deque.spec.ts +107 -0
  71. package/tests/globals.spec.ts +2 -2
  72. package/tests/itertools.spec.ts +2 -2
  73. package/tests/math.spec.ts +1 -1
  74. package/tests/operators.spec.ts +5 -5
  75. package/tests/random.spec.ts +10 -10
  76. package/tsconfig.json +10 -10
  77. package/tsconfig.node.json +2 -2
  78. package/tsconfig.xo.json +2 -2
  79. package/src/datetime/index.d.ts +0 -6
  80. package/src/datetime/index.js +0 -6
  81. package/src/datetime/index.ts +0 -7
  82. package/src/operators/index.d.ts +0 -31
  83. package/src/operators/index.js +0 -171
  84. package/src/operators/index.ts +0 -175
@@ -171,7 +171,7 @@ export function once(func) {
171
171
  };
172
172
  }
173
173
  /**
174
- * Wraps a function to catch any exceptions inside and return a `go` style error-return type.
174
+ * Wraps a function to catch any exceptions inside and return a `go` style error-return type {@link Result}.
175
175
  *
176
176
  * @param {Function} fun The function to wrap and catch if it throws.
177
177
  * @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
@@ -198,7 +198,7 @@ export function result(fun, onFinally) {
198
198
  *
199
199
  * @param {Function|Promise} awaitable The function or promise to await.
200
200
  * @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
201
- * @returns {Function} A new function that awaits the `awaitable` param and returns a return type.
201
+ * @returns {Function} A new function that awaits the `awaitable` param and returns a {@link Result} type.
202
202
  * @template T,E
203
203
  */
204
204
  export function resultAsync(awaitable, onFinally) {
@@ -204,7 +204,7 @@ export type Maybe<T> = T | undefined
204
204
  export type Result<T, E = any> = [Maybe<T>, Maybe<E>]
205
205
 
206
206
  /**
207
- * Wraps a function to catch any exceptions inside and return a `go` style error-return type.
207
+ * Wraps a function to catch any exceptions inside and return a `go` style error-return type {@link Result}.
208
208
  *
209
209
  * @param {Function} fun The function to wrap and catch if it throws.
210
210
  * @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
@@ -233,7 +233,7 @@ export function result<T = any, E = unknown>(
233
233
  *
234
234
  * @param {Function|Promise} awaitable The function or promise to await.
235
235
  * @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
236
- * @returns {Function} A new function that awaits the `awaitable` param and returns a return type.
236
+ * @returns {Function} A new function that awaits the `awaitable` param and returns a {@link Result} type.
237
237
  * @template T,E
238
238
  */
239
239
  export function resultAsync<T = any, E = unknown>(
@@ -3,11 +3,6 @@
3
3
  *
4
4
  * @module Globals
5
5
  */
6
- /** Describes a function used to compare two values.
7
- *
8
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description Array.sort()}
9
- */
10
- export declare type Comp = (x: any, y: any) => -1 | 0 | 1;
11
6
  /** This is just a noop function. */
12
7
  export declare const _: () => void;
13
8
  /**
@@ -27,7 +22,7 @@ export declare type Function<T = any> = (...args: any) => T;
27
22
  * @see {@link Collections.sortedUniq}
28
23
  * @see {@link Collections.groupBy}
29
24
  */
30
- export declare type Iteratee<T = any> = (value: T, key?: any, arr?: any) => any;
25
+ export declare type Iteratee<T = any, K = any> = (value: T, key?: K, arr?: any) => any;
31
26
  export declare type Predicate<T = any> = (value: T) => any;
32
27
  /** A function that always returns `true`. */
33
28
  export declare const True: () => boolean;
@@ -130,7 +125,7 @@ export declare const isGenerator: (x: any) => x is Generator<unknown, any, unkno
130
125
  export declare const isNull: (x: any) => x is null | undefined;
131
126
  /** Check if value is not `null` or `undefined`. */
132
127
  export declare const notNull: (x: any) => boolean;
133
- /** Returns `true` for objects without length or falsey values. */
128
+ /** Returns `true` for objects without length or falsy values. */
134
129
  export declare const isEmpty: (x: any) => boolean;
135
130
  /** Check if value is a `Symbol` type */
136
131
  export declare const isSymbol: (x: any) => x is symbol;
@@ -164,7 +164,7 @@ export const isGenerator = (x) => {
164
164
  export const isNull = (x) => x == null;
165
165
  /** Check if value is not `null` or `undefined`. */
166
166
  export const notNull = (x) => x != null;
167
- /** Returns `true` for objects without length or falsey values. */
167
+ /** Returns `true` for objects without length or falsy values. */
168
168
  export const isEmpty = (x) => (len(x) === 0 ? true : !x);
169
169
  /** Check if value is a `Symbol` type */
170
170
  export const isSymbol = (x) => type(x) === 'symbol';
@@ -179,12 +179,15 @@ export const isSymbol = (x) => type(x) === 'symbol';
179
179
  export function len(value) {
180
180
  if (value == null)
181
181
  return;
182
- if (value.length !== undefined) {
182
+ if (isNumber(value.length)) {
183
183
  return value.length;
184
184
  }
185
185
  if (isFunc(value.size)) {
186
186
  return value.size();
187
187
  }
188
+ if (isNumber(value.size)) {
189
+ return value.size;
190
+ }
188
191
  if (isObject(value)) {
189
192
  return Object.keys(value).length;
190
193
  }
@@ -4,12 +4,6 @@
4
4
  * @module Globals
5
5
  */
6
6
 
7
- /** Describes a function used to compare two values.
8
- *
9
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description Array.sort()}
10
- */
11
- export type Comp = (x, y) => -1 | 0 | 1
12
-
13
7
  /** This is just a noop function. */
14
8
  export const _ = () => {}
15
9
 
@@ -31,7 +25,7 @@ export type Function<T = any> = (...args: any) => T
31
25
  * @see {@link Collections.sortedUniq}
32
26
  * @see {@link Collections.groupBy}
33
27
  */
34
- export type Iteratee<T = any> = (value: T, key?, arr?) => any
28
+ export type Iteratee<T = any, K = any> = (value: T, key?: K, arr?) => any
35
29
 
36
30
  export type Predicate<T = any> = (value: T) => any
37
31
 
@@ -204,7 +198,7 @@ export const isNull = (x): x is null | undefined => x == null
204
198
  /** Check if value is not `null` or `undefined`. */
205
199
  export const notNull = (x) => x != null
206
200
 
207
- /** Returns `true` for objects without length or falsey values. */
201
+ /** Returns `true` for objects without length or falsy values. */
208
202
  export const isEmpty = (x) => (len(x) === 0 ? true : !x)
209
203
 
210
204
  /** Check if value is a `Symbol` type */
@@ -220,12 +214,15 @@ export const isSymbol = (x): x is symbol => type(x) === 'symbol'
220
214
  */
221
215
  export function len(value) {
222
216
  if (value == null) return
223
- if (value.length !== undefined) {
217
+ if (isNumber(value.length)) {
224
218
  return value.length
225
219
  }
226
220
  if (isFunc(value.size)) {
227
221
  return value.size()
228
222
  }
223
+ if (isNumber(value.size)) {
224
+ return value.size
225
+ }
229
226
  if (isObject(value)) {
230
227
  return Object.keys(value).length
231
228
  }
@@ -1,5 +1,4 @@
1
1
  export * from './collections/index.js';
2
- export * from './datetime/index.js';
3
2
  export * from './functools/index.js';
4
3
  export * from './globals/index.js';
5
4
  export * from './itertools/index.js';
@@ -1,5 +1,4 @@
1
1
  export * from './collections/index.js';
2
- export * from './datetime/index.js';
3
2
  export * from './functools/index.js';
4
3
  export * from './globals/index.js';
5
4
  export * from './itertools/index.js';
@@ -1,5 +1,4 @@
1
1
  export * from './collections/index.js'
2
- export * from './datetime/index.js'
3
2
  export * from './functools/index.js'
4
3
  export * from './globals/index.js'
5
4
  export * from './itertools/index.js'
@@ -3,8 +3,8 @@
3
3
  *
4
4
  * @module Iterators
5
5
  */
6
- import { Comp, Function, Predicate } from '../globals/index.js';
7
- import { add } from '../operators/index.js';
6
+ import { Function, Iteratee, Predicate } from '../globals/index.js';
7
+ import { add, Comparer } from '../operators/index.js';
8
8
  /**
9
9
  * Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional `func` argument).
10
10
  * @param iterable The iterable to accumulate
@@ -82,6 +82,8 @@ export declare function contains(collection: Iterable<any>, value: any): any;
82
82
  * @returns Elements
83
83
  */
84
84
  export declare function dropWhile<T>(iterable: Iterable<T>, predicate: Predicate): Generator<unknown, any, unknown>;
85
+ export declare function iforEach<T>(iterable: Iterable<T>, fun: Iteratee<T>): void;
86
+ export declare function IterableIterator<T = any>(next: any): Iterator<T, any, undefined> & Iterable<T>;
85
87
  /**
86
88
  * Yields elements like `[index, item]` from an iterable. Index starts at zero by default.
87
89
  * @template T
@@ -106,7 +108,7 @@ export declare function enumerate<T = any>(iterable: Iterable<T>, start?: number
106
108
  */
107
109
  export declare function first<T>(iterable: Iterable<T>): any;
108
110
  /**
109
- * Creates a new generator that filters all `falsey` and empty values from iterable.
111
+ * Creates a new generator that filters all falsy and empty values from iterable.
110
112
  * @param arr The array to compact
111
113
  * @returns A new array with the filtered values.
112
114
  * @example
@@ -120,7 +122,7 @@ export declare function icompact<T>(iterable: Iterable<T>): Generator<T, void, u
120
122
  * Iterates over elements of collection, producing only those elements where predicate returns a `truthy` value.
121
123
  */
122
124
  export declare function ifilter<T>(iterable: Iterable<T>, predicate: Predicate): Generator<T, void, unknown>;
123
- export declare function imap<T>(iterable: Iterable<T>, mapFn: (x: T) => any): Generator<any, void, unknown>;
125
+ export declare function imap<T, TReturn = any>(iterable: Iterable<T>, mapFn: (x: T) => TReturn): Generator<TReturn, void, unknown>;
124
126
  /**
125
127
  * Returns a generator flattening one level of nesting in a list of lists.
126
128
  * @param iterables The iterable to flatten
@@ -187,13 +189,13 @@ export declare function reduce<T>(iterable: Iterable<T>, reducer: (prev: T, valu
187
189
  export declare function reversed<T>(iterable: Iterable<T>): T[];
188
190
  export declare function sorted(args: any[]): any[];
189
191
  export declare function sorted(args: any[], reverse: boolean): any[];
190
- export declare function sorted(args: any[], reverse: boolean, comp: Comp): any[];
192
+ export declare function sorted(args: any[], reverse: boolean, comp: Comparer): any[];
191
193
  export declare function sorted(args: any[], key: Function): any[];
192
194
  export declare function sorted(args: any[], key: Function, reverse: boolean): any[];
193
195
  export declare function sort(args: any[]): any[];
194
196
  export declare function sort(args: any[], reverse: boolean): any[];
195
- export declare function sort(args: any[], compareFn: Comp): any[];
196
- export declare function sort(args: any[], reverse: boolean, compareFn: Comp): any[];
197
+ export declare function sort(args: any[], compareFn: Comparer): any[];
198
+ export declare function sort(args: any[], reverse: boolean, compareFn: Comparer): any[];
197
199
  export declare function take<T>(n: number, iterable: Iterable<T>): unknown[];
198
200
  /**
199
201
  * Returns a generator that takes elements from the iterable as long as the predicate is `true`.
@@ -4,7 +4,7 @@
4
4
  * @module Iterators
5
5
  */
6
6
  import { call, isEmpty, isObject, notNull, ValueError } from '../globals/index.js';
7
- import { add, bool, comp, eq, id } from '../operators/index.js';
7
+ import { add, bool, compare, eq, id } from '../operators/index.js';
8
8
  /**
9
9
  * Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional `func` argument).
10
10
  * @param iterable The iterable to accumulate
@@ -109,7 +109,7 @@ export function contains(collection, value) {
109
109
  return includes;
110
110
  return any(collection, (x) => eq(value, x));
111
111
  }
112
- const compKey = (cmp, key) => (x, y) => cmp(key(x), key(y));
112
+ const compKey = (compare, key) => (x, y) => compare(key(x), key(y));
113
113
  /**
114
114
  * 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`.
115
115
  * @param iterable The iterable to inspect.
@@ -134,6 +134,22 @@ export function* dropWhile(iterable, predicate) {
134
134
  yield head.value;
135
135
  }
136
136
  }
137
+ export function iforEach(iterable, fun) {
138
+ for (const [index, value] of enumerate(iterable)) {
139
+ if (fun(value, index, iterable) === false) {
140
+ return;
141
+ }
142
+ }
143
+ }
144
+ export function IterableIterator(next) {
145
+ const it = {
146
+ next,
147
+ [Symbol.iterator]() {
148
+ return this;
149
+ }
150
+ };
151
+ return it;
152
+ }
137
153
  /**
138
154
  * Yields elements like `[index, item]` from an iterable. Index starts at zero by default.
139
155
  * @template T
@@ -165,7 +181,7 @@ export function first(iterable) {
165
181
  return iter(iterable).next().value;
166
182
  }
167
183
  /**
168
- * Creates a new generator that filters all `falsey` and empty values from iterable.
184
+ * Creates a new generator that filters all falsy and empty values from iterable.
169
185
  * @param arr The array to compact
170
186
  * @returns A new array with the filtered values.
171
187
  * @example
@@ -350,8 +366,8 @@ export function reversed(iterable) {
350
366
  *
351
367
  * @param {(Array | Object)} args
352
368
  * @param {(boolean | Function)} [key]
353
- * @param {(boolean | Comp)} [reverse]
354
- * @param {Comp} [compareFn]
369
+ * @param {(boolean | Comparer)} [reverse]
370
+ * @param {Comparer} [compareFn]
355
371
  * @return
356
372
  */
357
373
  export function sorted(args, key, reverse, compareFn) {
@@ -368,7 +384,7 @@ export function sorted(args, key, reverse, compareFn) {
368
384
  key = id;
369
385
  }
370
386
  const copy = Array.from(args);
371
- const _compare = compKey(compareFn || comp, key || id);
387
+ const _compare = compKey(compareFn || compare, key || id);
372
388
  copy.sort(_compare);
373
389
  if (reverse)
374
390
  copy.reverse();
@@ -381,8 +397,8 @@ export function sorted(args, key, reverse, compareFn) {
381
397
  * - sort([...], true, (x, y) => number) => reverse and using custom compare
382
398
  *
383
399
  * @param {any[]} args
384
- * @param {(boolean | Comp)} [reverse]
385
- * @param {Comp} [compareFn]
400
+ * @param {(boolean | Comparer)} [reverse]
401
+ * @param {Comparer} [compareFn]
386
402
  * @return
387
403
  */
388
404
  export function sort(args, reverse, compareFn) {
@@ -390,7 +406,7 @@ export function sort(args, reverse, compareFn) {
390
406
  compareFn = reverse;
391
407
  reverse = false;
392
408
  }
393
- args.sort(compareFn || comp);
409
+ args.sort(compareFn || compare);
394
410
  if (reverse === true)
395
411
  args.reverse();
396
412
  return args;
@@ -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, comp, eq, id } from '../operators/index.js'
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 = (cmp: Comp, key) => (x, y) => cmp(key(x), key(y))
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 `falsey` and empty values from iterable.
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) => any) {
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: Comp): any[]
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 | Comp)} [reverse]
373
- * @param {Comp} [compareFn]
390
+ * @param {(boolean | Comparer)} [reverse]
391
+ * @param {Comparer} [compareFn]
374
392
  * @return
375
393
  */
376
- export function sorted(args, key?: boolean | Function, reverse?: boolean | Comp, compareFn?: Comp) {
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 || comp, key || id)
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: Comp): any[]
397
- export function sort(args: any[], reverse: boolean, compareFn: Comp): any[]
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 | Comp)} [reverse]
407
- * @param {Comp} [compareFn]
429
+ * @param {(boolean | Comparer)} [reverse]
430
+ * @param {Comparer} [compareFn]
408
431
  * @return
409
432
  */
410
- export function sort(args: any[], reverse?: boolean | Comp, compareFn?: Comp) {
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 || comp)
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;