atomic-fns 0.2.2 → 0.2.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.
Files changed (64) hide show
  1. package/README.md +5 -3
  2. package/package.json +6 -7
  3. package/src/collections/index.d.ts +658 -0
  4. package/src/collections/index.js +1068 -0
  5. package/src/collections/index.ts +1162 -0
  6. package/src/datetime/index.d.ts +6 -0
  7. package/src/datetime/index.js +6 -0
  8. package/src/datetime/index.ts +7 -0
  9. package/src/{functools.d.ts → functools/index.d.ts} +8 -7
  10. package/src/functools/index.js +84 -0
  11. package/src/{functools.ts → functools/index.ts} +9 -7
  12. package/src/globals/index.d.ts +291 -0
  13. package/src/globals/index.js +391 -0
  14. package/src/globals/index.ts +456 -0
  15. package/src/index.d.ts +8 -7
  16. package/src/index.js +8 -7
  17. package/src/index.ts +8 -7
  18. package/src/itertools/index.d.ts +254 -0
  19. package/src/itertools/index.js +485 -0
  20. package/src/itertools/index.ts +511 -0
  21. package/src/math/index.d.ts +21 -0
  22. package/src/math/index.js +39 -0
  23. package/src/math/index.ts +44 -0
  24. package/src/{operators.d.ts → operators/index.d.ts} +8 -6
  25. package/src/operators/index.js +171 -0
  26. package/src/{operators.ts → operators/index.ts} +75 -30
  27. package/src/random/index.d.ts +42 -0
  28. package/src/random/index.js +65 -0
  29. package/src/random/index.ts +66 -0
  30. package/tests/collections.spec.ts +42 -25
  31. package/tests/globals.spec.ts +51 -47
  32. package/tests/itertools.spec.ts +190 -4
  33. package/tests/operators.spec.ts +1 -26
  34. package/tests/random.spec.ts +46 -0
  35. package/tsconfig.json +12 -4
  36. package/src/collections.d.ts +0 -228
  37. package/src/collections.js +0 -553
  38. package/src/collections.ts +0 -641
  39. package/src/datetime.d.ts +0 -5
  40. package/src/datetime.js +0 -5
  41. package/src/datetime.ts +0 -6
  42. package/src/functools.js +0 -78
  43. package/src/globals.d.ts +0 -179
  44. package/src/globals.js +0 -309
  45. package/src/globals.ts +0 -377
  46. package/src/itertools.d.ts +0 -20
  47. package/src/itertools.js +0 -98
  48. package/src/itertools.ts +0 -116
  49. package/src/math.d.ts +0 -16
  50. package/src/math.js +0 -70
  51. package/src/math.ts +0 -77
  52. package/src/objects.d.ts +0 -0
  53. package/src/objects.js +0 -1
  54. package/src/objects.ts +0 -0
  55. package/src/operators.js +0 -109
  56. package/src/random.d.ts +0 -22
  57. package/src/random.js +0 -48
  58. package/src/random.ts +0 -53
  59. package/src/strings.d.ts +0 -0
  60. package/src/strings.js +0 -1
  61. package/src/strings.ts +0 -0
  62. package/src/util.d.ts +0 -0
  63. package/src/util.js +0 -1
  64. package/src/util.ts +0 -0
@@ -0,0 +1,511 @@
1
+ /**
2
+ * This module includes functions that deal with or produce iterators.
3
+ *
4
+ * @module Iterators
5
+ */
6
+
7
+ import {
8
+ call,
9
+ Comp,
10
+ Function,
11
+ isEmpty,
12
+ isObject,
13
+ notNull,
14
+ Predicate,
15
+ ValueError
16
+ } from '../globals/index.js'
17
+ import { bool, comp, eq, gt, id, le, add } from '../operators/index.js'
18
+
19
+ /**
20
+ * Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional `func` argument).
21
+ * @param iterable The iterable to accumulate
22
+ * @param func The accumulator function (default `add`).
23
+ * @param initial Optional initial value (defaults to the first element in iterable)
24
+ * @returns A sequence of accumulated values
25
+ */
26
+ export function* accumulate<T>(iterable: Iterable<T>, func = add, initial?) {
27
+ const it = iter(iterable)
28
+ let total = initial
29
+ let head = it.next()
30
+ if (initial == null) total = head.value
31
+ while (!head.done) {
32
+ yield total
33
+ head = it.next()
34
+ if (head.done) return
35
+ total = func(total, head.value)
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Returns `true` when all of the items in iterable are truthy. An optional predicate function can be used to define what truthiness means for this specific collection.
41
+ * @param iterable
42
+ * @param fn
43
+ * @returns `True` if all elements pass the test or are truthy else `false`.
44
+ * @example
45
+ ```js
46
+ all([0])
47
+ // => true
48
+
49
+ all([1, 2, 3])
50
+ // => true
51
+
52
+ all([2, 4, 6] => (n) => n > 1)
53
+ // => true
54
+ ```
55
+ @see {@link any}
56
+ */
57
+ export const all = <T>(iterable: Iterable<T>, fn: Predicate<T> = bool) => {
58
+ for (const item of iterable) {
59
+ if (!fn(item)) return false
60
+ }
61
+ return true
62
+ }
63
+
64
+ /**
65
+ * Returns true when any of the items in iterable are truthy. An optional predicate function can be used to define what truthiness means for this specific collection.
66
+ * @param iterable
67
+ * @param fn
68
+ * @returns `True` if any element passes the test or is truthy else `false`.
69
+ * @example
70
+ ```js
71
+ any([0])
72
+ // => false
73
+
74
+ any([0, 1, null, undefined])
75
+ // => true
76
+
77
+ any([1, 4, 5] => (n) => n % 2 === 0)
78
+ // => true
79
+ ```
80
+ @see {@link all}
81
+ */
82
+ export const any = <T>(iterable: Iterable<T>, fn: Predicate<T> = bool) => {
83
+ for (const item of iterable) {
84
+ if (fn(item)) return true
85
+ }
86
+ return false
87
+ }
88
+
89
+ export function chain<T>(...iterables: Array<Iterable<T>>) {
90
+ return iflatten(iterables)
91
+ }
92
+
93
+ /**
94
+ * Checks if the `value` is in `collection`.
95
+ * @param collection The collection to inspect.
96
+ * @param value The value to search for.
97
+ * @returns Returns `true` if value is found, else `false`.
98
+ * @example
99
+ ```js
100
+ contains([1, 2, 3], 1)
101
+ // => true
102
+
103
+ contains([1, 2, 3], 1, 2)
104
+ // => false
105
+
106
+ contains({ 'a': 1, 'b': 2 }, 1)
107
+ // => true
108
+
109
+ contains('abcd', 'bc')
110
+ // => true
111
+ ```
112
+ *
113
+ * @see {@link collections.find}
114
+ */
115
+ export function contains(collection: Iterable<any>, value) {
116
+ if (!collection) return false
117
+ const includes = call(collection, 'includes', value)
118
+ if (includes != null) return includes
119
+ return any(collection, (x) => eq(value, x))
120
+ }
121
+
122
+ const compKey = (cmp: Comp, key) => (x, y) => cmp(key(x), key(y))
123
+
124
+ /**
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`.
126
+ * @param iterable The iterable to inspect.
127
+ * @param predicate A function invoked per element to check if it should be dropped.
128
+ * @returns Elements
129
+ */
130
+ export function* dropWhile<T>(iterable: Iterable<T>, predicate: Predicate) {
131
+ const it = iter(iterable)
132
+ while (true) {
133
+ const head = it.next()
134
+ if (!predicate(head.value)) {
135
+ if (head.done) return head.value
136
+ yield head.value
137
+ break
138
+ }
139
+ }
140
+ while (true) {
141
+ const head = it.next()
142
+ if (head.done) return head.value
143
+ yield head.value
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Yields elements like `[index, item]` from an iterable. Index starts at zero by default.
149
+ * @template T
150
+ * @param iterable The iterable to inspect.
151
+ * @param start The starting index.
152
+ * @returns A generator with tuples like `[index, item]`.
153
+ * @example
154
+ ```js
155
+ console.log([...enumerate(['hello', 'world'])])
156
+ // => [[0, 'hello'], [1, 'world']]
157
+
158
+ // Start with index `1`
159
+ console.log([...enumerate(['hello', 'world'], 1)])
160
+ // => [[1, 'hello'], [2, 'world']]
161
+ ```
162
+ */
163
+ export function* enumerate<T = any>(iterable: Iterable<T>, start = 0) {
164
+ let index = start
165
+ for (const item of iterable) {
166
+ yield [index++, item]
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Returns the first element in an iterable.
172
+ * @param iterable The iterable to inspect.
173
+ * @returns The first element found or undefined if iterable is empty.
174
+ */
175
+ export function first<T>(iterable: Iterable<T>) {
176
+ return iter(iterable).next().value
177
+ }
178
+
179
+ /**
180
+ * Creates a new generator that filters all `falsey` and empty values from iterable.
181
+ * @param arr The array to compact
182
+ * @returns A new array with the filtered values.
183
+ * @example
184
+ ```js
185
+ compact([0, 1, false, 2, '', 3])
186
+ // => [1, 2, 3]
187
+ ```
188
+ */
189
+ export function* icompact<T>(iterable: Iterable<T>) {
190
+ for (const value of iterable) {
191
+ if (!isEmpty(value)) yield value
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Iterates over elements of collection, producing only those elements where predicate returns a `truthy` value.
197
+ */
198
+ export function* ifilter<T>(iterable: Iterable<T>, predicate: Predicate) {
199
+ for (const value of iterable) {
200
+ if (predicate(value)) yield value
201
+ }
202
+ }
203
+
204
+ export function* imap<T>(iterable: Iterable<T>, mapFn: (x: T) => any) {
205
+ for (const item of iterable) {
206
+ yield mapFn(item)
207
+ }
208
+ }
209
+
210
+ /**
211
+ * Returns a generator flattening one level of nesting in a list of lists.
212
+ * @param iterables The iterable to flatten
213
+ * @example
214
+ ```js
215
+ [...iflatten([[0, 1], [2, 3]])]
216
+ // => [0, 1, 2, 3]
217
+ ```
218
+ */
219
+ export function* iflatten<T = any>(iterables: Iterable<Iterable<T>>) {
220
+ for (const iterator of iterables) {
221
+ for (const item of iterator) {
222
+ yield item
223
+ }
224
+ }
225
+ }
226
+
227
+ export function flatMap<T>(iterable: Iterable<T>, mapFn: (x: T) => any = id) {
228
+ return iflatten<T>(imap(iterable, mapFn))
229
+ }
230
+
231
+ export function* islice<T>(iterable: Iterable<T>, start: number, stop?: number, step = 1) {
232
+ if (stop != null && stop < 0) throw new ValueError(`stop parameter can't be negative`)
233
+ if (start < 0) throw new ValueError(`start parameter can't be negative`)
234
+ if (stop != null && start > stop) throw new ValueError(`start parameter can't be after stop`)
235
+ if (step < 0) throw new ValueError(`step parameter can't be negative`)
236
+ for (const [i, value] of enumerate(iterable)) {
237
+ if (i === start) {
238
+ if (stop && start + step > stop) return value
239
+ yield value
240
+ start += step
241
+ }
242
+ }
243
+ }
244
+
245
+ export function* itake<T>(n: number, iterable: Iterable<T>) {
246
+ const it = iter(iterable)
247
+ while (n-- > 0) {
248
+ const item = it.next()
249
+ if (!item.done) yield item.value
250
+ else return
251
+ }
252
+ }
253
+
254
+ /**
255
+ * Returns an Iterator from the elements of a collection or the object keys.
256
+ * @param obj The given collection to iterate over.
257
+ * @returns An Iterator type.
258
+ */
259
+ export function iter<T>(obj): Iterator<T> {
260
+ const iterable = call(obj, Symbol.iterator)
261
+ if (notNull(iterable)) return iterable as Iterator<T>
262
+ return Object.keys(obj)[Symbol.iterator]() as Iterator<T>
263
+ }
264
+
265
+ export function partition<T>(iterable: Iterable<T>, predicate: Predicate<T>) {
266
+ const left = [] as T[]
267
+ const right = [] as T[]
268
+ for (const value of iterable) {
269
+ if (predicate(value)) left.push(value)
270
+ else right.push(value)
271
+ }
272
+ return [left, right]
273
+ }
274
+
275
+ /**
276
+ * Returns the smallest element in an iterable. The optional `key` argument specifies a transform on the elements before comparing them. If the elements in the iterable are objects that implement a custom `le` method, this will be called to compare.
277
+ * @param iterable The iterable to inspect.
278
+ * @param key Optional key function to transform the elements (default `id`).
279
+ * @returns The smallest element in the iterable.
280
+ */
281
+ export function min<T>(iterable: Iterable<T>, key = id) {
282
+ return reduce(iterable, (x, y) => (le(key(x), key(y)) ? x : y))
283
+ }
284
+
285
+ /**
286
+ * Returns the largest element in an iterable. The optional `key` argument specifies a transform on the elements before comparing them. If the elements in the iterable are objects that implement a custom `gt` method, this will be called to compare.
287
+ * @param iterable The iterable to inspect.
288
+ * @param key Optional key function to transform the elements (default `id`).
289
+ * @returns The largest element in the iterable.
290
+ */
291
+ export function max<T>(iterable: Iterable<T>, key = id) {
292
+ return reduce(iterable, (x, y) => (gt(key(x), key(y)) ? x : y))
293
+ }
294
+
295
+ /**
296
+ * Returns a generator for a sequence of numbers in the given range, starting from `start` (default 0) to `stop` (not-inclusive) in increments of `step` (default 1).
297
+ *
298
+ * `range(x)` is a shorthand for `range(0, x, 1)`.
299
+ * @param args When called with a single argument, it is assigned to `stop` param. If two arguments are given, it will assign them to `(start, stop)`. When three arguments, it means `(start, stop, step)`.
300
+ * @example
301
+ ```js
302
+ range(5)
303
+ // => [0, 1, 2, 3, 4]
304
+
305
+ range(2, 5)
306
+ // => [2, 3, 4]
307
+
308
+ range(0, 5, 2)
309
+ // => [0, 2, 4]
310
+
311
+ range(5, 0, -1)
312
+ // => [5, 4, 3, 2, 1]
313
+
314
+ range(-3)
315
+ // => []
316
+ ```
317
+ */
318
+ export function* range(...args: number[]) {
319
+ let start = 0
320
+ let stop = 0
321
+ let step = 1
322
+ if (args.length === 1) {
323
+ stop = args[0]
324
+ } else if (args.length === 2) {
325
+ start = args[0]
326
+ stop = args[1]
327
+ } else {
328
+ start = args[0]
329
+ stop = args[1]
330
+ step = args[2] || 1
331
+ }
332
+ for (; step > 0 ? start < stop : start > stop; start += step) {
333
+ yield start
334
+ }
335
+ }
336
+
337
+ /**
338
+ * Applies a `reducer` function on each element of the iterable, in order, passing the return value from the operation on the previous element. The final result of running the reducer across all elements of the iterable is a single value.
339
+ * @param iterable The collection to reduce.
340
+ * @param reducer A reducer function that will be called per item.
341
+ * @param initial Optional initial value (defaults to the first element in the iterable).
342
+ * @returns The result of calling reducer on each item.
343
+ * @example
344
+ ```js
345
+ reduce([1, 2, 3, 4, 5], (x, y) => x + y, 0)
346
+ // => 15
347
+ ```
348
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce Array.reduce()}
349
+ */
350
+ export function reduce<T>(
351
+ iterable: Iterable<T>,
352
+ reducer: (prev: T, value: T, index: any) => T,
353
+ initial?: any
354
+ ) {
355
+ const it = iter<T>(iterable)
356
+ let output = initial ?? it.next().value
357
+ for (const [index, value] of enumerate(it as any)) {
358
+ output = reducer(output, value as T, index as number)
359
+ }
360
+ return output
361
+ }
362
+
363
+ /**
364
+ * Creates a new array with the elements from the iterable in reverse order.
365
+ * @param iterable The iterable to inspect.
366
+ * @returns A new array with the elements reversed.
367
+ */
368
+ export function reversed<T>(iterable: Iterable<T>) {
369
+ return Array.from(iterable).reverse()
370
+ }
371
+
372
+ export function sorted(args: any[]): any[]
373
+ export function sorted(args: any[], reverse: boolean): any[]
374
+ export function sorted(args: any[], reverse: boolean, comp: Comp): any[]
375
+ export function sorted(args: any[], key: Function): any[]
376
+ export function sorted(args: any[], key: Function, reverse: boolean): any[]
377
+
378
+ /**
379
+ * Creates a new sorted list from the elements in the array. This can be called many ways:
380
+ * - sorted([...], true) => reverse order
381
+ * - sorted([...], fn = (x) => any) => using a key fn
382
+ * - sorted([...], fn = (x) => any, true) => reverse order using key
383
+ * - sorted([...], false, (x, y) => number) => using custom compare
384
+ * - sorted([...], fn = (x) => any, true, (x, y) => number) => custom key, reverse and compare
385
+ *
386
+ * If `args` is an Object, returns the sorted keys.
387
+ *
388
+ *
389
+ * @param {(Array | Object)} args
390
+ * @param {(boolean | Function)} [key]
391
+ * @param {(boolean | Comp)} [reverse]
392
+ * @param {Comp} [compareFn]
393
+ * @return
394
+ */
395
+ export function sorted(args, key?: boolean | Function, reverse?: boolean | Comp, compareFn?: Comp) {
396
+ if (isObject(args)) args = Object.keys(args)
397
+ if (typeof key === 'boolean') {
398
+ if (typeof reverse === 'function') {
399
+ compareFn = reverse
400
+ reverse = false
401
+ } else {
402
+ reverse = true
403
+ }
404
+ key = id
405
+ }
406
+ const copy = Array.from(args)
407
+ const _compare = compKey(compareFn || comp, key || id)
408
+ copy.sort(_compare)
409
+ if (reverse) copy.reverse()
410
+ return copy
411
+ }
412
+
413
+ export function sort(args: any[]): any[]
414
+ export function sort(args: any[], reverse: boolean): any[]
415
+ export function sort(args: any[], compareFn: Comp): any[]
416
+ export function sort(args: any[], reverse: boolean, compareFn: Comp): any[]
417
+
418
+ /**
419
+ * Sort `args` in place. Can be called like this:
420
+ * - sort([...], true) => reverse order
421
+ * - sort([...], (x, y) => number) => using custom compare
422
+ * - sort([...], true, (x, y) => number) => reverse and using custom compare
423
+ *
424
+ * @param {any[]} args
425
+ * @param {(boolean | Comp)} [reverse]
426
+ * @param {Comp} [compareFn]
427
+ * @return
428
+ */
429
+ export function sort(args: any[], reverse?: boolean | Comp, compareFn?: Comp) {
430
+ if (typeof reverse === 'function') {
431
+ compareFn = reverse
432
+ reverse = false
433
+ }
434
+ args.sort(compareFn || comp)
435
+ if (reverse === true) args.reverse()
436
+ return args
437
+ }
438
+
439
+ /**
440
+ * Sums `initial` and the items of an iterable and returns the total.
441
+ * @param args The elements to add to `initial`.
442
+ * @param initial The initial value to add to (default `0`).
443
+ * @returns The total sum of initial and iterable elements.
444
+ * @example
445
+ ```js
446
+ sum([1, 2, 3, 4])
447
+ // => 10
448
+
449
+ sum([1, 2, 3, 4], 5)
450
+ // => 15
451
+ ```
452
+ */
453
+ export function sum(args: Iterable<number>, initial = 0) {
454
+ for (const x of args) {
455
+ initial += x
456
+ }
457
+ return initial
458
+ }
459
+
460
+ export function take<T>(n: number, iterable: Iterable<T>) {
461
+ return [...itake<T>(n, iterable)]
462
+ }
463
+
464
+ /**
465
+ * Returns a generator that takes elements from the iterable as long as the predicate is `true`.
466
+ * @param iterable The iterable to inspect.
467
+ * @param predicate A function invoked per element to check if it should be taken.
468
+ * @returns Elements
469
+ */
470
+ export function* takeWhile<T>(iterable: Iterable<T>, predicate: Predicate) {
471
+ for (const value of iterable) {
472
+ if (!predicate(value)) return
473
+ yield value
474
+ }
475
+ }
476
+
477
+ /** Returns a generator that calls `fn(index)` for each index < n */
478
+ /**
479
+ * Returns a generator that produces `fn(index)` for each `index < n`.
480
+ * @param n The number of times to invoke `fn`.
481
+ * @param fn A function used to produce the results.
482
+ */
483
+ export function* times(n: number, fn: (index: number) => any) {
484
+ for (let i = 0; i < n; i++) yield fn(i)
485
+ }
486
+
487
+ /**
488
+ * Returns a generator that aggregates elements from the iterables producing tuples with the next element from each one.
489
+ * @param args The iterables to zip
490
+ * @returns Tuples with elements from each iterator
491
+ * @example
492
+ ```js
493
+ for item of zip([1, 2, 3], ['sugar', 'spice', 'nice'])
494
+ console.log(item)
495
+ // => [1, 'sugar']
496
+ // => [2, 'spice']
497
+ // => [3, 'nice']
498
+ ```
499
+ */
500
+ export function* zip(...args: Array<Iterable<any>>) {
501
+ const iterables = args.map(iter)
502
+ while (true) {
503
+ const items = [] as any[]
504
+ for (const iterator of iterables) {
505
+ const { value, done } = iterator.next()
506
+ if (done) return
507
+ items.push(value)
508
+ }
509
+ yield items
510
+ }
511
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Math and algebraic operations.
3
+ *
4
+ * @module Math
5
+ */
6
+ /**
7
+ * Returns the absolute value of a number. The argument may be a number or an object implementing the `abs()`.
8
+ * @param x The value.
9
+ * @returns The absolute value.
10
+ */
11
+ export declare function abs(x: any): number;
12
+ /**
13
+ * Returns a tuple like `[x / y, x % y]`.
14
+ *
15
+ * @param x
16
+ * @param y
17
+ * @returns
18
+ */
19
+ export declare function divmod(x: number, y: number): number[];
20
+ export declare function log2(x: any): number;
21
+ export declare function logBase(x: any, y: any): number;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Math and algebraic operations.
3
+ *
4
+ * @module Math
5
+ */
6
+ import { call } from '../globals/index.js';
7
+ /**
8
+ * Returns the absolute value of a number. The argument may be a number or an object implementing the `abs()`.
9
+ * @param x The value.
10
+ * @returns The absolute value.
11
+ */
12
+ export function abs(x) {
13
+ const op = call(x, 'abs');
14
+ if (op != null)
15
+ return op;
16
+ return Math.abs(x);
17
+ }
18
+ /**
19
+ * Returns a tuple like `[x / y, x % y]`.
20
+ *
21
+ * @param x
22
+ * @param y
23
+ * @returns
24
+ */
25
+ export function divmod(x, y) {
26
+ return [Math.floor(x / y), x % y];
27
+ }
28
+ export function log2(x) {
29
+ if (x > 0) {
30
+ return Math.log(x) * 1.442695;
31
+ }
32
+ return Number.NaN;
33
+ }
34
+ export function logBase(x, y) {
35
+ if (x > 0 && y > 0) {
36
+ return Math.log(y) / Math.log(x);
37
+ }
38
+ return Number.NaN;
39
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Math and algebraic operations.
3
+ *
4
+ * @module Math
5
+ */
6
+
7
+ import { call, Comp, isFunc } from '../globals/index.js'
8
+ import { comp } from '../operators/index.js'
9
+
10
+ /**
11
+ * Returns the absolute value of a number. The argument may be a number or an object implementing the `abs()`.
12
+ * @param x The value.
13
+ * @returns The absolute value.
14
+ */
15
+ export function abs(x): number {
16
+ const op = call(x, 'abs')
17
+ if (op != null) return op
18
+ return Math.abs(x)
19
+ }
20
+
21
+ /**
22
+ * Returns a tuple like `[x / y, x % y]`.
23
+ *
24
+ * @param x
25
+ * @param y
26
+ * @returns
27
+ */
28
+ export function divmod(x: number, y: number) {
29
+ return [Math.floor(x / y), x % y]
30
+ }
31
+
32
+ export function log2(x) {
33
+ if (x > 0) {
34
+ return Math.log(x) * 1.442695
35
+ }
36
+ return Number.NaN
37
+ }
38
+
39
+ export function logBase(x, y) {
40
+ if (x > 0 && y > 0) {
41
+ return Math.log(y) / Math.log(x)
42
+ }
43
+ return Number.NaN
44
+ }
@@ -1,14 +1,14 @@
1
+ /**
2
+ * Generic operations for both values and objects.
3
+ *
4
+ * @module Operators
5
+ */
1
6
  export declare const id: (x: any) => any;
2
7
  export declare const bool: (x: any) => boolean;
3
8
  export declare const not: (x: any) => boolean;
4
9
  export declare const isinstance: (x: any, y: any) => boolean;
5
- export declare const all: (arr: any[], fn?: (x: any) => boolean) => boolean;
6
- export declare const any: (arr: any[], fn?: (x: any) => boolean) => boolean;
7
- export declare const and: (arr: any[], fn?: (x: any) => boolean) => boolean;
8
- export declare const or: (arr: any[], fn?: (x: any) => boolean) => boolean;
9
- export declare function comp(x: any, y: any): any;
10
+ export declare function comp(x: any, y: any): 0 | 1 | -1;
10
11
  export declare function eq(x: any, y: any): any;
11
- export declare function shallowEqual(x: any, y: any): any;
12
12
  export declare function lt(x: any, y: any): any;
13
13
  export declare function le(x: any, y: any): any;
14
14
  export declare function gt(x: any, y: any): any;
@@ -27,3 +27,5 @@ export declare function mod(x: any, y: any): any;
27
27
  * @return `x` raised to the power `y`
28
28
  */
29
29
  export declare function pow(x: any, y: any): any;
30
+ export declare function shallowEqual(obj: any, other: any): boolean;
31
+ export declare function deepEqual(obj: any, other: any, checker?: typeof eq): boolean;