@gkucmierz/utils 4.1.1 → 5.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/README.md +1 -1
- package/main.mjs +10 -2
- package/package.json +1 -1
- package/src/data-structures/Stack.mjs +100 -0
- package/src/developer-utils/get-type.mjs +2 -16
- package/src/developer-utils/type.mjs +111 -0
package/README.md
CHANGED
|
@@ -77,7 +77,7 @@ This library provides a wide range of mathematical functions and data structures
|
|
|
77
77
|
- `binarySearch`: Various binary search implementations (exact, lower bound, upper bound).
|
|
78
78
|
- `naturalSearch`: Natural sorting/search utility for human-readable string comparisons.
|
|
79
79
|
- `range2array`, `array2range`: Convert between ranges and arrays.
|
|
80
|
-
- `
|
|
80
|
+
- `Type`, `is`, `of`, `same`: High-performance runtime type detection, introspection, and safe comparison system (v5.0.0).
|
|
81
81
|
|
|
82
82
|
## 📚 Documentation
|
|
83
83
|
|
package/main.mjs
CHANGED
|
@@ -20,6 +20,9 @@ import {
|
|
|
20
20
|
import {
|
|
21
21
|
SetCnt
|
|
22
22
|
} from './src/data-structures/SetCnt.mjs'
|
|
23
|
+
import {
|
|
24
|
+
Stack
|
|
25
|
+
} from './src/data-structures/Stack.mjs'
|
|
23
26
|
import {
|
|
24
27
|
Trie
|
|
25
28
|
} from './src/data-structures/Trie.mjs'
|
|
@@ -36,7 +39,7 @@ import {
|
|
|
36
39
|
consumeIteratorNonBlocking
|
|
37
40
|
} from './src/developer-utils/consume-iterator.mjs'
|
|
38
41
|
import {
|
|
39
|
-
|
|
42
|
+
|
|
40
43
|
} from './src/developer-utils/get-type.mjs'
|
|
41
44
|
import {
|
|
42
45
|
measurePerformance
|
|
@@ -56,6 +59,9 @@ import {
|
|
|
56
59
|
import {
|
|
57
60
|
setSafeInterval
|
|
58
61
|
} from './src/developer-utils/set-safe-interval.mjs'
|
|
62
|
+
import {
|
|
63
|
+
Type, Types, is, of, same
|
|
64
|
+
} from './src/developer-utils/type.mjs'
|
|
59
65
|
import {
|
|
60
66
|
barycentricCoordinates
|
|
61
67
|
} from './src/geometry/barycentric.mjs'
|
|
@@ -142,6 +148,7 @@ export * from './src/combinatorics/n-choose-k.mjs';
|
|
|
142
148
|
export * from './src/combinatorics/permutations.mjs';
|
|
143
149
|
export * from './src/data-structures/ConflatingQueue.mjs';
|
|
144
150
|
export * from './src/data-structures/SetCnt.mjs';
|
|
151
|
+
export * from './src/data-structures/Stack.mjs';
|
|
145
152
|
export * from './src/data-structures/Trie.mjs';
|
|
146
153
|
export * from './src/data-structures/heap.mjs';
|
|
147
154
|
export * from './src/data-structures/list-node.mjs';
|
|
@@ -154,6 +161,7 @@ export * from './src/developer-utils/natural-search.mjs';
|
|
|
154
161
|
export * from './src/developer-utils/rand-normal.mjs';
|
|
155
162
|
export * from './src/developer-utils/range-array.mjs';
|
|
156
163
|
export * from './src/developer-utils/set-safe-interval.mjs';
|
|
164
|
+
export * from './src/developer-utils/type.mjs';
|
|
157
165
|
export * from './src/geometry/barycentric.mjs';
|
|
158
166
|
export * from './src/geometry/math3d.mjs';
|
|
159
167
|
export * from './src/geometry/matrix.mjs';
|
|
@@ -182,5 +190,5 @@ export * from './src/string-arrays/copy-case.mjs';
|
|
|
182
190
|
export * from './src/string-arrays/format-big-number.mjs';
|
|
183
191
|
|
|
184
192
|
export default [
|
|
185
|
-
createLangtonsAnt, createUnlimitedGrid, combinations, combinationsIterator, bin2gray, gray2bin, nChooseK, permutations, permutationsIterator, ConflatingQueue, SetCnt, Trie, Heap, ListNode, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, consumeIteratorNonBlocking,
|
|
193
|
+
createLangtonsAnt, createUnlimitedGrid, combinations, combinationsIterator, bin2gray, gray2bin, nChooseK, permutations, permutationsIterator, ConflatingQueue, SetCnt, Stack, Trie, Heap, ListNode, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, consumeIteratorNonBlocking, measurePerformance, memoize, naturalSearch, randNormal, array2range, range2array, setSafeInterval, Type, Types, is, of, same, barycentricCoordinates, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, egcd, factors, factorsBI, gcd, gcdBI, lcm, lcmBI, lucasLehmerBI, mobius, mobiusBI, mod, modBI, phi, phiBI, powMod, powModBI, tonelliShanksBI, nelderMead, particleSwarmOptimization, simulatedAnnealing, goldenRatio, goldenRatioBI, goldenRatioStr, gpn, gpnBI, heronsFormula, heronsFormulaBI, squareRoot, squareRootBI, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, chunks, chunksAsyncIterator, chunksIterator, copyCase, formatBigNumber, formatBigNumberBI, wrapFn
|
|
186
194
|
];
|
package/package.json
CHANGED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module data-structures
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* High-performance, LIFO (Last-In-First-Out) Stack data structure.
|
|
7
|
+
* Encapsulates an internal private array (#items) to prevent index tampering
|
|
8
|
+
* and optimize V8 memory locality and fast push/pop operations.
|
|
9
|
+
* @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4IMpwIYGMDWCC+CAZjFGAgEQACA5jgK5ZggCmMAXgPT1wgA2AZwoBuYMHTYcooA|▶ Try it live in Instacode}
|
|
10
|
+
*/
|
|
11
|
+
export class Stack {
|
|
12
|
+
#items = [];
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new Stack instance.
|
|
16
|
+
* @param {Iterable} [iter] - An optional iterable object to initialize the stack with.
|
|
17
|
+
*/
|
|
18
|
+
constructor(iter) {
|
|
19
|
+
for (const el of iter ?? []) {
|
|
20
|
+
this.push(el);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Pushes one or more items onto the top of the stack.
|
|
26
|
+
* @param {...*} items - Elements to push onto the stack.
|
|
27
|
+
* @returns {Stack} The Stack instance for fluent method chaining.
|
|
28
|
+
*/
|
|
29
|
+
push(...items) {
|
|
30
|
+
this.#items.push(...items);
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Removes and returns the item from the top of the stack.
|
|
36
|
+
* @returns {*|undefined} The top item, or undefined if the stack is empty.
|
|
37
|
+
*/
|
|
38
|
+
pop() {
|
|
39
|
+
return this.#items.pop();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Returns the item from the top of the stack without removing it.
|
|
44
|
+
* @returns {*|undefined} The top item, or undefined if the stack is empty.
|
|
45
|
+
*/
|
|
46
|
+
peek() {
|
|
47
|
+
return this.#items[this.#items.length - 1];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Checks whether the stack is empty.
|
|
52
|
+
* @returns {boolean} True if the stack contains no items, false otherwise.
|
|
53
|
+
*/
|
|
54
|
+
isEmpty() {
|
|
55
|
+
return this.#items.length === 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Gets the number of items currently in the stack.
|
|
60
|
+
* @returns {number} The number of items in the stack.
|
|
61
|
+
*/
|
|
62
|
+
get size() {
|
|
63
|
+
return this.#items.length;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Gets the number of items currently in the stack (Array length compatibility alias).
|
|
68
|
+
* @returns {number} The number of items in the stack.
|
|
69
|
+
*/
|
|
70
|
+
get length() {
|
|
71
|
+
return this.#items.length;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Removes all items from the stack.
|
|
76
|
+
* @returns {Stack} The Stack instance for fluent method chaining.
|
|
77
|
+
*/
|
|
78
|
+
clear() {
|
|
79
|
+
this.#items.length = 0;
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Returns a shallow copy of the stack items as an Array from bottom to top.
|
|
85
|
+
* @returns {Array} Array of items from bottom to top.
|
|
86
|
+
*/
|
|
87
|
+
toArray() {
|
|
88
|
+
return [...this.#items];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Iterates through stack items from top to bottom (LIFO order).
|
|
93
|
+
* @returns {Iterator}
|
|
94
|
+
*/
|
|
95
|
+
*[Symbol.iterator]() {
|
|
96
|
+
for (let i = this.#items.length - 1; i >= 0; i--) {
|
|
97
|
+
yield this.#items[i];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -1,16 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Returns the type of the value as a lowercase string.
|
|
8
|
-
* More specific than the typeof operator (e.g., distinguishes 'array', 'null', 'date').
|
|
9
|
-
* @param {*} val - The value to check.
|
|
10
|
-
* @returns {string} The type of the value.
|
|
11
|
-
* @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4IOYFM4BUCeF0IC+CAZjFGAgEQACqA1gK4DGYI6MAXgPSNwgAbAM5UA3MGAZsedOKA|▶ Try it live in Instacode}
|
|
12
|
-
*/
|
|
13
|
-
export const getType = val => {
|
|
14
|
-
const str = Object.prototype.toString.call(val);
|
|
15
|
-
return str.slice(8, -1).toLowerCase();
|
|
16
|
-
};
|
|
1
|
+
// Deprecated - removed in v5.0.0 in favor of ./type.mjs
|
|
2
|
+
export {};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module developer-utils
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Robust, highly-optimized type detection, introspection, and comparison system.
|
|
7
|
+
* @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4ICoE8IFMEF8EBmMUYCARAAIDmA1gK4DGYIWMAXgPR1wgA2AzmQDcwYBmwigA|▶ Try it live in Instacode}
|
|
8
|
+
*/
|
|
9
|
+
export class Type {
|
|
10
|
+
// 1. Bottom values & Primitives
|
|
11
|
+
static UNDEFINED = 'undefined';
|
|
12
|
+
static NULL = 'null';
|
|
13
|
+
static BOOLEAN = 'boolean';
|
|
14
|
+
static NUMBER = 'number';
|
|
15
|
+
static NAN = 'nan';
|
|
16
|
+
static BIGINT = 'bigint';
|
|
17
|
+
static STRING = 'string';
|
|
18
|
+
static SYMBOL = 'symbol';
|
|
19
|
+
|
|
20
|
+
// 2. Core Objects & Callables
|
|
21
|
+
static OBJECT = 'object';
|
|
22
|
+
static ARRAY = 'array';
|
|
23
|
+
static FUNCTION = 'function';
|
|
24
|
+
|
|
25
|
+
// 3. Standard Built-ins
|
|
26
|
+
static DATE = 'date';
|
|
27
|
+
static REGEXP = 'regexp';
|
|
28
|
+
static ERROR = 'error';
|
|
29
|
+
static PROMISE = 'promise';
|
|
30
|
+
|
|
31
|
+
// 4. Collections
|
|
32
|
+
static MAP = 'map';
|
|
33
|
+
static SET = 'set';
|
|
34
|
+
static WEAKMAP = 'weakmap';
|
|
35
|
+
static WEAKSET = 'weakset';
|
|
36
|
+
static WEAKREF = 'weakref';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Returns the canonical runtime type of any value as a lowercase string.
|
|
40
|
+
* Differentiates NaN ('nan') from valid numbers ('number'), distinguishes 'array', 'null', 'date', etc.
|
|
41
|
+
* @param {*} val - Value to check.
|
|
42
|
+
* @returns {string} The canonical type string.
|
|
43
|
+
*/
|
|
44
|
+
static of(val) {
|
|
45
|
+
if (Number.isNaN(val)) return Type.NAN;
|
|
46
|
+
return Object.prototype.toString.call(val).slice(8, -1).toLowerCase();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Checks if a value matches an expected type (by string constant, Constructor, or union array).
|
|
51
|
+
* @param {*} val - Value to check.
|
|
52
|
+
* @param {string|Function|Array<string|Function>} expected - Expected type, constructor, or union array.
|
|
53
|
+
* @returns {boolean}
|
|
54
|
+
*/
|
|
55
|
+
static is(val, expected) {
|
|
56
|
+
if (Array.isArray(expected)) {
|
|
57
|
+
return expected.some(exp => Type.is(val, exp));
|
|
58
|
+
}
|
|
59
|
+
if (typeof expected === 'function') {
|
|
60
|
+
if (expected === Number) return typeof val === 'number' && !Number.isNaN(val);
|
|
61
|
+
if (expected === Boolean) return typeof val === 'boolean' || val instanceof Boolean;
|
|
62
|
+
if (expected === String) return typeof val === 'string' || val instanceof String;
|
|
63
|
+
if (expected === BigInt) return typeof val === 'bigint' || (typeof Object(val) === 'object' && val instanceof BigInt);
|
|
64
|
+
if (expected === Symbol) return typeof val === 'symbol' || (typeof Object(val) === 'object' && val instanceof Symbol);
|
|
65
|
+
if (expected === Array) return Array.isArray(val);
|
|
66
|
+
if (expected === Function) return typeof val === 'function';
|
|
67
|
+
if (expected === Object) return val !== null && (typeof val === 'object' || typeof val === 'function');
|
|
68
|
+
return val instanceof expected;
|
|
69
|
+
}
|
|
70
|
+
return Type.of(val) === expected;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Compares whether two values have the exact same canonical type.
|
|
75
|
+
* Safely prevents JavaScript traps like typeof NaN === typeof 42 or typeof null === typeof {}.
|
|
76
|
+
* @param {*} a - First value.
|
|
77
|
+
* @param {*} b - Second value.
|
|
78
|
+
* @returns {boolean} True if both values share the same type.
|
|
79
|
+
*/
|
|
80
|
+
static same(a, b) {
|
|
81
|
+
return Type.of(a) === Type.of(b);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Alias for the {@link Type} class.
|
|
87
|
+
* @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4ICoE8IFMDOCC+CAZjFGAgEQACA5gNYCuAxmCFjAF4D0DcIANjgoBuYMAzYcooA|▶ Try it live in Instacode}
|
|
88
|
+
*/
|
|
89
|
+
export const Types = Type;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Checks if a value matches an expected type.
|
|
93
|
+
* @see {@link Type.is}
|
|
94
|
+
* @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4JAZwQXwQMxlMBAIgAEBzAawFcBjMEAUxgC8B6KuEAGzSIG5goNAKA|▶ Try it live in Instacode}
|
|
95
|
+
*/
|
|
96
|
+
export const is = Type.is;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Returns the canonical runtime type of any value.
|
|
100
|
+
* @see {@link Type.of}
|
|
101
|
+
* @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4KgMwQXwWmUwIBEAAgOYDWArgMZggCmMAXgPSVwgA2AzoQNzBg6AUA|▶ Try it live in Instacode}
|
|
102
|
+
*/
|
|
103
|
+
export const of = Type.of;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Compares whether two values have the exact same canonical type.
|
|
107
|
+
* @see {@link Type.same}
|
|
108
|
+
* @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4IM4EMwFMEF8EBmMUYCARAAIDmA1gK4DGYIWMAXgPR1wgA2qZANzBgGbMKA|▶ Try it live in Instacode}
|
|
109
|
+
*/
|
|
110
|
+
export const same = Type.same;
|
|
111
|
+
|