atomic-fns 0.2.2 → 0.3.0

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