atomic-fns 0.1.1 → 0.2.2

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