@gkucmierz/utils 4.1.0 → 5.0.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 +9 -7
- package/main.mjs +5 -1
- package/package.json +1 -1
- package/src/data-structures/ConflatingQueue.mjs +2 -3
- package/src/developer-utils/type.mjs +111 -0
package/README.md
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# @gkucmierz/utils
|
|
2
2
|
|
|
3
|
-
[
|
|
3
|
+
[](https://www.npmjs.com/package/@gkucmierz/utils)
|
|
4
|
+
[](https://socket.dev/npm/package/@gkucmierz/utils)
|
|
5
|
+
[](https://gitea.7u.pl/gkucmierz/utils)
|
|
6
|
+
[](https://www.npmjs.com/package/@gkucmierz/utils)
|
|
7
|
+
[](https://gitea.7u.pl/gkucmierz/utils)
|
|
8
|
+
[](https://nodejs.org)
|
|
7
9
|
|
|
8
10
|
A collection of useful utility functions and data structures for solving algorithmic tasks, competitive programming, and everyday development.
|
|
9
11
|
|
|
@@ -75,12 +77,12 @@ This library provides a wide range of mathematical functions and data structures
|
|
|
75
77
|
- `binarySearch`: Various binary search implementations (exact, lower bound, upper bound).
|
|
76
78
|
- `naturalSearch`: Natural sorting/search utility for human-readable string comparisons.
|
|
77
79
|
- `range2array`, `array2range`: Convert between ranges and arrays.
|
|
78
|
-
- `
|
|
80
|
+
- `Type`, `is`, `of`, `same`: High-performance runtime type detection, introspection, and safe comparison system (v5.0.0).
|
|
79
81
|
|
|
80
82
|
## 📚 Documentation
|
|
81
83
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
+
- **Production Docs**: 👉 **[https://docs-utils.7u.pl](https://docs-utils.7u.pl)**
|
|
85
|
+
- **GitHub Pages Mirror**: [https://gkucmierz.github.io/utils](https://gkucmierz.github.io/utils)
|
|
84
86
|
|
|
85
87
|
## 🔗 Repository
|
|
86
88
|
- **Internal Gitea**: [https://gitea.7u.pl/gkucmierz/utils](https://gitea.7u.pl/gkucmierz/utils)
|
package/main.mjs
CHANGED
|
@@ -56,6 +56,9 @@ import {
|
|
|
56
56
|
import {
|
|
57
57
|
setSafeInterval
|
|
58
58
|
} from './src/developer-utils/set-safe-interval.mjs'
|
|
59
|
+
import {
|
|
60
|
+
Type, Types, is, of, same
|
|
61
|
+
} from './src/developer-utils/type.mjs'
|
|
59
62
|
import {
|
|
60
63
|
barycentricCoordinates
|
|
61
64
|
} from './src/geometry/barycentric.mjs'
|
|
@@ -154,6 +157,7 @@ export * from './src/developer-utils/natural-search.mjs';
|
|
|
154
157
|
export * from './src/developer-utils/rand-normal.mjs';
|
|
155
158
|
export * from './src/developer-utils/range-array.mjs';
|
|
156
159
|
export * from './src/developer-utils/set-safe-interval.mjs';
|
|
160
|
+
export * from './src/developer-utils/type.mjs';
|
|
157
161
|
export * from './src/geometry/barycentric.mjs';
|
|
158
162
|
export * from './src/geometry/math3d.mjs';
|
|
159
163
|
export * from './src/geometry/matrix.mjs';
|
|
@@ -182,5 +186,5 @@ export * from './src/string-arrays/copy-case.mjs';
|
|
|
182
186
|
export * from './src/string-arrays/format-big-number.mjs';
|
|
183
187
|
|
|
184
188
|
export default [
|
|
185
|
-
createLangtonsAnt, createUnlimitedGrid, combinations, combinationsIterator, bin2gray, gray2bin, nChooseK, permutations, permutationsIterator, ConflatingQueue, SetCnt, Trie, Heap, ListNode, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, consumeIteratorNonBlocking, getType, measurePerformance, memoize, naturalSearch, randNormal, array2range, range2array, setSafeInterval, 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
|
|
189
|
+
createLangtonsAnt, createUnlimitedGrid, combinations, combinationsIterator, bin2gray, gray2bin, nChooseK, permutations, permutationsIterator, ConflatingQueue, SetCnt, Trie, Heap, ListNode, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, consumeIteratorNonBlocking, getType, 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
190
|
];
|
package/package.json
CHANGED
|
@@ -39,10 +39,9 @@ export class ConflatingQueue {
|
|
|
39
39
|
* Enqueues an asynchronous task associated with a specific key.
|
|
40
40
|
* If a pending task with the same key already exists, it is superseded by the new task.
|
|
41
41
|
*
|
|
42
|
-
* @template T
|
|
43
42
|
* @param {string|number|symbol} key - Unique identifier for task coalescing.
|
|
44
|
-
* @param {
|
|
45
|
-
* @returns {Promise
|
|
43
|
+
* @param {Function} taskFn - The async or sync function to execute.
|
|
44
|
+
* @returns {Promise<*|{ superseded: boolean }>} Resolves with task result or `{ superseded: true }`.
|
|
46
45
|
*/
|
|
47
46
|
enqueue(key, taskFn) {
|
|
48
47
|
if (typeof taskFn !== 'function') {
|
|
@@ -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
|
+
|