atomic-fns 0.1.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.
- package/.editorconfig +12 -0
- package/.eslintignore +1 -0
- package/.eslintrc.json +34 -0
- package/.prettierrc +19 -0
- package/LICENSE +201 -0
- package/README.md +1 -0
- package/build/collections.d.ts +204 -0
- package/build/collections.js +545 -0
- package/build/datetime.d.ts +1 -0
- package/build/datetime.js +1 -0
- package/build/functools.d.ts +36 -0
- package/build/functools.js +79 -0
- package/build/globals.d.ts +175 -0
- package/build/globals.js +332 -0
- package/build/index.d.ts +7 -0
- package/build/index.js +7 -0
- package/build/itertools.d.ts +16 -0
- package/build/itertools.js +102 -0
- package/build/math.d.ts +16 -0
- package/build/math.js +67 -0
- package/build/objects.d.ts +0 -0
- package/build/objects.js +1 -0
- package/build/operators.d.ts +29 -0
- package/build/operators.js +128 -0
- package/build/random.d.ts +5 -0
- package/build/random.js +28 -0
- package/build/strings.d.ts +0 -0
- package/build/strings.js +1 -0
- package/build/util.d.ts +0 -0
- package/build/util.js +1 -0
- package/jasmine.json +14 -0
- package/package.json +71 -0
- package/src/collections.ts +614 -0
- package/src/datetime.ts +1 -0
- package/src/functools.ts +89 -0
- package/src/globals.d.ts +5 -0
- package/src/globals.ts +377 -0
- package/src/index.ts +7 -0
- package/src/itertools.ts +113 -0
- package/src/math.ts +73 -0
- package/src/objects.ts +0 -0
- package/src/operators.ts +126 -0
- package/src/random.ts +29 -0
- package/src/strings.ts +0 -0
- package/src/util.ts +0 -0
- package/tests/collections.spec.ts +199 -0
- package/tests/globals.spec.ts +232 -0
- package/tests/itertools.spec.ts +87 -0
- package/tests/operators.spec.ts +62 -0
- package/tsconfig.json +103 -0
- package/tsconfig.node.json +18 -0
- package/tsconfig.xo.json +9 -0
|
@@ -0,0 +1,175 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
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.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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>;
|
|
@@ -0,0 +1,102 @@
|
|
|
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.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare function abs(x: number): any;
|
|
2
|
+
/**
|
|
3
|
+
* Returns a tuple like (x / y, x % y)
|
|
4
|
+
* @export
|
|
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;
|