atomic-fns 0.2.0 → 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/.esdoc.json +19 -1
  2. package/.eslintrc.json +1 -0
  3. package/README.md +8 -9
  4. package/package.json +9 -9
  5. package/src/collections/index.d.ts +658 -0
  6. package/src/collections/index.js +1068 -0
  7. package/src/collections/index.ts +1162 -0
  8. package/src/datetime/index.d.ts +6 -0
  9. package/src/datetime/index.js +6 -0
  10. package/src/datetime/index.ts +7 -0
  11. package/src/{functools.d.ts → functools/index.d.ts} +10 -5
  12. package/src/{functools.js → functools/index.js} +9 -4
  13. package/src/{functools.ts → functools/index.ts} +11 -5
  14. package/src/globals/index.d.ts +291 -0
  15. package/src/globals/index.js +391 -0
  16. package/src/globals/index.ts +456 -0
  17. package/src/index.d.ts +8 -7
  18. package/src/index.js +8 -7
  19. package/src/index.ts +8 -7
  20. package/src/itertools/index.d.ts +254 -0
  21. package/src/itertools/index.js +485 -0
  22. package/src/itertools/index.ts +511 -0
  23. package/src/math/index.d.ts +21 -0
  24. package/src/math/index.js +39 -0
  25. package/src/math/index.ts +44 -0
  26. package/src/{operators.d.ts → operators/index.d.ts} +8 -6
  27. package/src/{operators.js → operators/index.js} +69 -26
  28. package/src/{operators.ts → operators/index.ts} +77 -28
  29. package/src/random/index.d.ts +42 -0
  30. package/src/random/index.js +65 -0
  31. package/src/random/index.ts +66 -0
  32. package/tests/collections.spec.ts +119 -30
  33. package/tests/globals.spec.ts +91 -49
  34. package/tests/itertools.spec.ts +207 -14
  35. package/tests/operators.spec.ts +7 -29
  36. package/tests/random.spec.ts +46 -0
  37. package/tsconfig.json +19 -3
  38. package/src/collections.d.ts +0 -224
  39. package/src/collections.js +0 -570
  40. package/src/collections.ts +0 -637
  41. package/src/datetime.d.ts +0 -1
  42. package/src/datetime.js +0 -1
  43. package/src/datetime.ts +0 -1
  44. package/src/globals.d.ts +0 -175
  45. package/src/globals.js +0 -329
  46. package/src/globals.ts +0 -373
  47. package/src/itertools.d.ts +0 -16
  48. package/src/itertools.js +0 -101
  49. package/src/itertools.ts +0 -112
  50. package/src/math.d.ts +0 -16
  51. package/src/math.js +0 -67
  52. package/src/math.ts +0 -73
  53. package/src/objects.d.ts +0 -0
  54. package/src/objects.js +0 -1
  55. package/src/objects.ts +0 -0
  56. package/src/random.d.ts +0 -22
  57. package/src/random.js +0 -48
  58. package/src/random.ts +0 -49
  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
package/src/globals.ts DELETED
@@ -1,373 +0,0 @@
1
- export type Comp = (x, y) => number
2
-
3
- export type Function = (...args) => any
4
-
5
- export type Iteratee = (value, key?, arr?) => any
6
-
7
- export const True = () => true
8
-
9
- export const False = () => false
10
-
11
- /**
12
- * A custom error class that inherits from the Error object.
13
- *
14
- *
15
- * @class CustomError
16
- * @extends {Error}
17
- */
18
- export class CustomError extends Error {
19
- /**
20
- * Returns a new CustomError with the specified message.
21
- * @param message
22
- * @constructor
23
- */
24
- constructor(message?) {
25
- super(message)
26
- this.name = this.constructor.name
27
- if (typeof Error.captureStackTrace === 'function') {
28
- Error.captureStackTrace(this, this.constructor)
29
- } else {
30
- this.stack = new Error(message).stack
31
- }
32
- }
33
- }
34
-
35
- /**
36
- * Custom error to signal an invalid index access.
37
- * @extends {CustomError}
38
- */
39
- export class IndexError extends CustomError {}
40
-
41
- /**
42
- * Raised when a system function returns an I/O failure such as "file not found"
43
- * or "disk full".
44
- * @extends {CustomError}
45
- */
46
- export class IOError extends CustomError {}
47
-
48
- /**
49
- * Custom error to signal an invalid key access.
50
- * @extends {CustomError}
51
- */
52
- export class KeyError extends CustomError {}
53
-
54
- /**
55
- * Custom error to signal an invalid operation.
56
- * @extends {CustomError}
57
- */
58
- export class NotImplementedError extends CustomError {}
59
-
60
- /**
61
- * Custom error to signal an invalid value.
62
- * @extends {CustomError}
63
- */
64
- export class ValueError extends CustomError {}
65
-
66
- /**
67
- * Raised when the second argument of a division or modulo operation is zero.
68
- * @extends {CustomError}
69
- */
70
- export class ZeroDivisionError extends CustomError {}
71
-
72
- /**
73
- * Returns the true type of any value with correct detection for null, Array,
74
- * Object, Promise, Symbol, and NaN.
75
- *
76
- *
77
- * @param {*} value
78
- * @return
79
- */
80
- export function type(value) {
81
- const result = typeof value
82
- if (result === 'object') {
83
- if (value) {
84
- if (value instanceof String) return 'string'
85
- if (value instanceof Boolean) return 'boolean'
86
- if (value instanceof Function) return 'function'
87
- // IE improperly marshals typeof across execution contexts, but a
88
- // cross-context object will still return false for "instanceof Object".
89
- if (value instanceof Array) return 'array'
90
-
91
- const className = str(value)
92
- // In Firefox 3.6, attempting to access iframe window objects' length
93
- // property throws an NS_ERROR_FAILURE, so we need to special-case it
94
- // here.
95
- if (className === '[object Window]') return 'object'
96
-
97
- // We cannot always use constructor === Array or instanceof Array because
98
- // different frames have different Array objects.
99
- // Mark Miller noticed that Object.prototype.toString
100
- // allows access to the unforgeable [[Class]] property.
101
- // 15.2.4.2 Object.prototype.toString ( )
102
- // When the toString method is called, the following steps are taken:
103
- // 1. Get the [[Class]] property of this object.
104
- // 2. Compute a string value by concatenating the three strings
105
- // "[object ", Result(1), and "]".
106
- // 3. Return Result(2).
107
- // and this behavior survives the destruction of the execution context.
108
- if (className.endsWith('Array]') || Array.isArray(value)) {
109
- return 'array'
110
- }
111
- // HACK: There is still an array case that fails.
112
- // function ArrayImpostor() {}
113
- // ArrayImpostor.prototype = [];
114
- // var impostor = new ArrayImpostor;
115
- // this can be fixed by getting rid of the fast path
116
- // (value instanceof Array) and solely relying on
117
- // (value && Object.prototype.toString.vall(value) === '[object Array]')
118
- // but that would require many more function calls and is not warranted
119
- // unless closure code is receiving objects from untrusted sources.
120
-
121
- // IE in cross-window calls does not correctly marshal the function type
122
- // (it appears just as an object) so we cannot use just typeof val ==
123
- // 'function'. However, if the object has a call property, it is a
124
- // function.
125
- if (className.endsWith('Function]') || typeof value.call === 'function') {
126
- return 'function'
127
- }
128
- if (className === '[object Promise]' || value instanceof Promise) {
129
- return 'promise'
130
- }
131
- if (className === '[object Symbol]') {
132
- return 'symbol'
133
- }
134
- } else {
135
- return 'null'
136
- }
137
- } else if (result === 'function' && typeof value.call === 'undefined') {
138
- // In Safari typeof nodeList returns 'function', and on Firefox typeof
139
- // behaves similarly for HTML{Applet,Embed,Object}, Elements and RegExps. We
140
- // would like to return object for those and we can detect an invalid
141
- // function by making sure that the function object has a call method.
142
- return 'object'
143
- } else if (Number.isNaN(value)) return 'NaN'
144
- return result
145
- }
146
-
147
- /** Same as `x.toString()`. Returns `''` when x is null or undefined */
148
- export const str = (x) => (x != null ? x.toString() : '')
149
-
150
- /** Check if value is a boolean type */
151
- export const isBool = (x) => type(x) === 'boolean'
152
-
153
- /** Check if value is an object type */
154
- export const isObject = (x) => typeof x === 'object' && type(x) === 'object'
155
-
156
- /** Check if value is a string type */
157
- export const isString = (x) => type(x) === 'string'
158
-
159
- /** Check if value is an Array type */
160
- export const isArray = (x) => type(x) === 'array'
161
-
162
- /** Check if value is Array-like type.
163
- * A value is considered array-like if it's not a function and has a
164
- * `.length` number property.
165
- */
166
- export const isArrayLike = (x) => {
167
- const T = type(x)
168
- return T === 'array' || (T === 'object' && isNumber(x.length))
169
- }
170
-
171
- /** Check if value is a function type */
172
- export const isFunc = (x) => type(x) === 'function'
173
-
174
- /** Check if value is a number type */
175
- export const isNumber = (x) => type(x) === 'number'
176
-
177
- /** Check if value is a Bigint type */
178
- export const isBigint = (x) => type(x) === 'bigint'
179
-
180
- /** Check if value is NaN based on `Number.isNaN` */
181
- export const isNaN = (x) => Number.isNaN(x)
182
-
183
- /** Check if value is a Promise type */
184
- export const isPromise = (x) => type(x) === 'promise'
185
-
186
- /** Check if value is an async function type */
187
- export const isAsync = (x) => get(x, 'constructor.name') === 'AsyncFunction'
188
-
189
- /** Check if value is a generator function type */
190
- export const isGenerator = (x) => {
191
- return get(x, 'constructor.constructor.name') === 'GeneratorFunction'
192
- }
193
-
194
- /** Check if value is null or undefined */
195
- export const isNull = (x) => x == null
196
-
197
- /** Check if value is not null or undefined */
198
- export const notNull = (x) => x != null
199
-
200
- export function len(x) {
201
- if (x == null) return
202
- if (x.length !== undefined) {
203
- return x.length
204
- }
205
- if (isFunc(x.size)) {
206
- return x.size()
207
- }
208
- if (isObject(x)) {
209
- return Object.keys(x).length
210
- }
211
- }
212
-
213
- /** Returns true for objects without length or falsey values. */
214
- export const isEmpty = (x) => (len(x) === 0 ? true : !x)
215
-
216
- /** Check if value is a Symbol type */
217
- export const isSymbol = (x) => type(x) === 'symbol'
218
-
219
- /**
220
- * Generates a unique ID using random numbers.
221
- * If prefix is given, the ID is appended to it
222
- */
223
- export const uniqueId = (pre: string = '') =>
224
- // @ts-expect-error
225
- (pre || 0) + ((Math.random() * 1e10) >>> 0)
226
-
227
- /**
228
- * Checks if `obj.key` is a function, and calls it with any `args`.
229
- *
230
- *
231
- * @param {*} obj
232
- * @param {PropertyKey} key
233
- * @param {...any[]} args
234
- * @return `obj.key(...args)`
235
- */
236
- export function call(obj: any, key: PropertyKey, ...args: any[]) {
237
- if (isFunc(get(obj, key))) {
238
- return obj[key](...args)
239
- }
240
- }
241
-
242
- /** Convert number to unicode character */
243
- export const chr = (x: number) => String.fromCodePoint(x)
244
-
245
- /** Convert character to Unicode code point */
246
- export const ord = (x: string) => x.charCodeAt(0)
247
-
248
- /** `Object.keys(x)` */
249
- export const keys = (x) => Object.keys(x)
250
-
251
- /** `Object.values(x)` */
252
- export const values = (x) => Object.values(x)
253
-
254
- /**
255
- * Round `x` to the number of digits after the decimal point. If `digits` is
256
- * omitted, it returns the nearest integer to x.
257
- *
258
- * @param {number} x
259
- * @param {number} [digits=0]
260
- */
261
- export function round(x: number, digits = 0) {
262
- const base = 10 ** digits
263
- return Math.round(x * base) / base
264
- }
265
-
266
- export const floor = (x: number) => Math.floor(x)
267
-
268
- export const ceil = (x: number) => Math.ceil(x)
269
-
270
- /**
271
- * Yields elements like [index, item] from an iterable.
272
- *
273
- *
274
- * @template T
275
- * @param {Iterable<T>} iterable
276
- */
277
- export function* enumerate<T = unknown>(iterable: Iterable<T>) {
278
- let i = 0
279
- for (const item of iterable) {
280
- yield [i++, item]
281
- }
282
- }
283
-
284
- export const has = (obj, attr: PropertyKey) => obj && attr in obj
285
-
286
- /**
287
- * Check if the attribute is present in the object, or return optional default.
288
- *
289
- *
290
- * @param {*} obj
291
- * @param {PropertyKey} key
292
- * @param {*} [value=undefined]
293
- * @return The property value or default value
294
- */
295
- export function get(obj: any, key: PropertyKey | PropertyKey[], value: any = undefined) {
296
- if (obj == null) return value
297
-
298
- let paths = Array.isArray(key) ? key : [key]
299
- if (typeof key === 'string') {
300
- paths = key.split('.')
301
- }
302
-
303
- try {
304
- let result = obj
305
- for (const k of paths) {
306
- result = result[k]
307
- }
308
- return result === undefined ? value : result
309
- } catch (error) {
310
- return value
311
- }
312
- }
313
-
314
- export function del(x, attr) {
315
- if (x != null) delete x[attr]
316
- return x
317
- }
318
-
319
- export function set(self: any, attr: PropertyKey, value, writable = false, enumerable = false) {
320
- return Object.defineProperty(self, attr, {
321
- value,
322
- enumerable,
323
- writable
324
- })
325
- }
326
-
327
- export const HASH_KEY = Symbol.for('HASH_KEY')
328
-
329
- export function hash(obj) {
330
- if (typeof obj === 'string') {
331
- return hashCode(obj)
332
- }
333
-
334
- if (typeof obj === 'object') {
335
- if (obj[HASH_KEY] == null) {
336
- set(obj, HASH_KEY, uniqueId())
337
- }
338
- return obj[HASH_KEY]
339
- }
340
-
341
- return obj
342
- }
343
-
344
- /**
345
- * A hash value generator for strings
346
- *
347
- * @param {string} str
348
- * @return {number}
349
- * @see http://isthe.com/chongo/tech/comp/fnv/
350
- */
351
- export function hashCode(str: string) {
352
- const FNV1_32A_INIT = 0x811c9dc5
353
- let hval = FNV1_32A_INIT
354
- for (let i = 0; i < str.length; ++i) {
355
- hval ^= str.charCodeAt(i)
356
- hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24)
357
- }
358
- return hval >>> 0
359
- }
360
-
361
- export const bin = (n: number) => n.toString(2)
362
-
363
- export const hex = (n: number) => n.toString(16)
364
-
365
- export const oct = (n: number) => n.toString(8)
366
-
367
- export const float = (x) => parseFloat(x)
368
-
369
- export const int = (x, base = 10) => parseInt(x, base)
370
-
371
- export const list = (arr?) => (arr ? Array.from(arr) : [])
372
-
373
- export const next = (iter: Iterator<unknown>) => iter.next().value
@@ -1,16 +0,0 @@
1
- import { Comp, Function } from './globals.js';
2
- export declare function range(...args: number[]): Generator<number, void, unknown>;
3
- /** Creates a new list with the elements from the iterable in reverse order. */
4
- export declare function reversed(iterable: Iterable<any>): any[];
5
- export declare function sorted(args: any[]): any[];
6
- export declare function sorted(args: any[], reverse: boolean): any[];
7
- export declare function sorted(args: any[], reverse: boolean, comp: Comp): any[];
8
- export declare function sorted(args: any[], key: Function): any[];
9
- export declare function sorted(args: any[], key: Function, reverse: boolean): any[];
10
- export declare function sort(args: any[]): any[];
11
- export declare function sort(args: any[], reverse: boolean): any[];
12
- export declare function sort(args: any[], compareFn: Comp): any[];
13
- export declare function sort(args: any[], reverse: boolean, compareFn: Comp): any[];
14
- /** Returns a generator that calls `fn(index)` for each index < n */
15
- export declare function times(n: number, fn: (index: number) => any): Generator<any, void, unknown>;
16
- export declare function zip(...args: any[]): Generator<any, void, unknown>;
package/src/itertools.js DELETED
@@ -1,101 +0,0 @@
1
- import { isObject, len } from './globals.js';
2
- import { comp, id } from './operators.js';
3
- const compKey = (cmp, key) => (x, y) => cmp(key(x), key(y));
4
- export function* range(...args) {
5
- if (args.length === 1) {
6
- const stop = args[0];
7
- for (let i = 0; i < stop; i++) {
8
- yield i;
9
- }
10
- }
11
- else if (args.length === 2) {
12
- let [start, stop] = args;
13
- for (; start < stop; start++) {
14
- yield start;
15
- }
16
- }
17
- else {
18
- let [start, stop, step] = args;
19
- for (; start < stop; start += step) {
20
- yield start;
21
- }
22
- }
23
- }
24
- /** Creates a new list with the elements from the iterable in reverse order. */
25
- export function reversed(iterable) {
26
- return Array.from(iterable).reverse();
27
- }
28
- /**
29
- * Creates a new list from `args` and sorts it. This can be called many ways:
30
- * - sorted([...], true) => reverse order
31
- * - sorted([...], fn = (x) => any) => using a key fn
32
- * - sorted([...], fn = (x) => any, true) => reverse order using key
33
- * - sorted([...], false, (x, y) => number) => using custom compare
34
- * - sorted([...], fn = (x) => any, true, (x, y) => number) => custom key, reverse and compare
35
- *
36
- * If `args` is an Object, returns the sorted keys.
37
- *
38
- *
39
- * @param {(Array | Object)} args
40
- * @param {(boolean | Function)} [key]
41
- * @param {(boolean | Comp)} [reverse]
42
- * @param {Comp} [compareFn]
43
- * @return
44
- */
45
- export function sorted(args, key, reverse, compareFn) {
46
- if (isObject(args))
47
- args = Object.keys(args);
48
- if (typeof key === 'boolean') {
49
- if (typeof reverse === 'function') {
50
- compareFn = reverse;
51
- reverse = false;
52
- }
53
- else {
54
- reverse = true;
55
- }
56
- key = id;
57
- }
58
- const copy = Array.from(args);
59
- const _compare = compKey(compareFn || comp, key || id);
60
- copy.sort(_compare);
61
- if (reverse)
62
- copy.reverse();
63
- return copy;
64
- }
65
- /**
66
- * Sort `args` in place. Can be called like this:
67
- * - sort([...], true) => reverse order
68
- * - sort([...], (x, y) => number) => using custom compare
69
- * - sort([...], true, (x, y) => number) => reverse and using custom compare
70
- *
71
- * @param {any[]} args
72
- * @param {(boolean | Comp)} [reverse]
73
- * @param {Comp} [compareFn]
74
- * @return
75
- */
76
- export function sort(args, reverse, compareFn) {
77
- if (typeof reverse === 'function') {
78
- compareFn = reverse;
79
- reverse = false;
80
- }
81
- args.sort(compareFn || comp);
82
- if (reverse === true)
83
- args.reverse();
84
- return args;
85
- }
86
- /** Returns a generator that calls `fn(index)` for each index < n */
87
- export function* times(n, fn) {
88
- for (let i = 0; i < n; i++)
89
- yield fn(i);
90
- }
91
- export function* zip(...args) {
92
- const size = len(args[0]);
93
- for (let i = 0; i < size; i++) {
94
- const tuple = [];
95
- for (let k = 0; k < args.length; k++) {
96
- const item = args[k][i];
97
- tuple.push(item);
98
- }
99
- yield tuple;
100
- }
101
- }
package/src/itertools.ts DELETED
@@ -1,112 +0,0 @@
1
- import { Comp, Function, isObject, len } from './globals.js'
2
- import { comp, id } from './operators.js'
3
-
4
- const compKey = (cmp: Comp, key) => (x, y) => cmp(key(x), key(y))
5
-
6
- export function* range(...args: number[]) {
7
- if (args.length === 1) {
8
- const stop = args[0]
9
- for (let i = 0; i < stop; i++) {
10
- yield i
11
- }
12
- } else if (args.length === 2) {
13
- let [start, stop] = args
14
- for (; start < stop; start++) {
15
- yield start
16
- }
17
- } else {
18
- let [start, stop, step] = args
19
- for (; start < stop; start += step) {
20
- yield start
21
- }
22
- }
23
- }
24
-
25
- /** Creates a new list with the elements from the iterable in reverse order. */
26
- export function reversed(iterable: Iterable<any>) {
27
- return Array.from(iterable).reverse()
28
- }
29
-
30
- export function sorted(args: any[]): any[]
31
- export function sorted(args: any[], reverse: boolean): any[]
32
- export function sorted(args: any[], reverse: boolean, comp: Comp): any[]
33
- export function sorted(args: any[], key: Function): any[]
34
- export function sorted(args: any[], key: Function, reverse: boolean): any[]
35
-
36
- /**
37
- * Creates a new list from `args` and sorts it. This can be called many ways:
38
- * - sorted([...], true) => reverse order
39
- * - sorted([...], fn = (x) => any) => using a key fn
40
- * - sorted([...], fn = (x) => any, true) => reverse order using key
41
- * - sorted([...], false, (x, y) => number) => using custom compare
42
- * - sorted([...], fn = (x) => any, true, (x, y) => number) => custom key, reverse and compare
43
- *
44
- * If `args` is an Object, returns the sorted keys.
45
- *
46
- *
47
- * @param {(Array | Object)} args
48
- * @param {(boolean | Function)} [key]
49
- * @param {(boolean | Comp)} [reverse]
50
- * @param {Comp} [compareFn]
51
- * @return
52
- */
53
- export function sorted(args, key?: boolean | Function, reverse?: boolean | Comp, compareFn?: Comp) {
54
- if (isObject(args)) args = Object.keys(args)
55
- if (typeof key === 'boolean') {
56
- if (typeof reverse === 'function') {
57
- compareFn = reverse
58
- reverse = false
59
- } else {
60
- reverse = true
61
- }
62
- key = id
63
- }
64
- const copy = Array.from(args)
65
- const _compare = compKey(compareFn || comp, key || id)
66
- copy.sort(_compare)
67
- if (reverse) copy.reverse()
68
- return copy
69
- }
70
-
71
- export function sort(args: any[]): any[]
72
- export function sort(args: any[], reverse: boolean): any[]
73
- export function sort(args: any[], compareFn: Comp): any[]
74
- export function sort(args: any[], reverse: boolean, compareFn: Comp): any[]
75
-
76
- /**
77
- * Sort `args` in place. Can be called like this:
78
- * - sort([...], true) => reverse order
79
- * - sort([...], (x, y) => number) => using custom compare
80
- * - sort([...], true, (x, y) => number) => reverse and using custom compare
81
- *
82
- * @param {any[]} args
83
- * @param {(boolean | Comp)} [reverse]
84
- * @param {Comp} [compareFn]
85
- * @return
86
- */
87
- export function sort(args: any[], reverse?: boolean | Comp, compareFn?: Comp) {
88
- if (typeof reverse === 'function') {
89
- compareFn = reverse
90
- reverse = false
91
- }
92
- args.sort(compareFn || comp)
93
- if (reverse === true) args.reverse()
94
- return args
95
- }
96
-
97
- /** Returns a generator that calls `fn(index)` for each index < n */
98
- export function* times(n: number, fn: (index: number) => any) {
99
- for (let i = 0; i < n; i++) yield fn(i)
100
- }
101
-
102
- export function* zip(...args) {
103
- const size = len(args[0])
104
- for (let i = 0; i < size; i++) {
105
- const tuple = [] as any
106
- for (let k = 0; k < args.length; k++) {
107
- const item = args[k][i]
108
- tuple.push(item)
109
- }
110
- yield tuple
111
- }
112
- }
package/src/math.d.ts DELETED
@@ -1,16 +0,0 @@
1
- export declare function abs(x: number): any;
2
- /**
3
- * Returns a tuple like (x / y, x % y)
4
- *
5
- * @param {number} x
6
- * @param {number} y
7
- * @return {number[]}
8
- */
9
- export declare function divmod(x: number, y: number): number[];
10
- export declare function log2(x: any): number;
11
- export declare function logBase(x: any, y: any): number;
12
- /** First argument can be a comparer func */
13
- export declare function min(args: any): any;
14
- /** First argument can be a comparer func */
15
- export declare function max(args: any): any;
16
- export declare function sum(args: any): number;
package/src/math.js DELETED
@@ -1,67 +0,0 @@
1
- import { call, isFunc } from './globals.js';
2
- import { comp } from './operators.js';
3
- export function abs(x) {
4
- const op = call(x, 'abs');
5
- if (op != null)
6
- return op;
7
- return Math.abs(x);
8
- }
9
- /**
10
- * Returns a tuple like (x / y, x % y)
11
- *
12
- * @param {number} x
13
- * @param {number} y
14
- * @return {number[]}
15
- */
16
- export function divmod(x, y) {
17
- return [Math.floor(x / y), x % y];
18
- }
19
- export function log2(x) {
20
- if (x > 0) {
21
- return Math.log(x) * 1.442695;
22
- }
23
- return Number.NaN;
24
- }
25
- export function logBase(x, y) {
26
- if (x > 0 && y > 0) {
27
- return Math.log(y) / Math.log(x);
28
- }
29
- return Number.NaN;
30
- }
31
- /** First argument can be a comparer func */
32
- export function min(args) {
33
- // Uses `le` by default
34
- let cmp = comp;
35
- if (isFunc(args[0])) {
36
- cmp = args.shift();
37
- }
38
- let res = args[0];
39
- for (const x of args) {
40
- if (cmp(x, res) === -1) {
41
- res = x;
42
- }
43
- }
44
- return res;
45
- }
46
- /** First argument can be a comparer func */
47
- export function max(args) {
48
- // Uses ge by default
49
- let cmp = comp;
50
- if (isFunc(args[0])) {
51
- cmp = args.shift();
52
- }
53
- let res = args[0];
54
- for (const x of args) {
55
- if (cmp(x, res) === 1) {
56
- res = x;
57
- }
58
- }
59
- return res;
60
- }
61
- export function sum(args) {
62
- let total = 0;
63
- for (const x of args) {
64
- total += x;
65
- }
66
- return total;
67
- }