@gkucmierz/utils 5.0.0 → 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/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
- getType
42
+
40
43
  } from './src/developer-utils/get-type.mjs'
41
44
  import {
42
45
  measurePerformance
@@ -145,6 +148,7 @@ export * from './src/combinatorics/n-choose-k.mjs';
145
148
  export * from './src/combinatorics/permutations.mjs';
146
149
  export * from './src/data-structures/ConflatingQueue.mjs';
147
150
  export * from './src/data-structures/SetCnt.mjs';
151
+ export * from './src/data-structures/Stack.mjs';
148
152
  export * from './src/data-structures/Trie.mjs';
149
153
  export * from './src/data-structures/heap.mjs';
150
154
  export * from './src/data-structures/list-node.mjs';
@@ -186,5 +190,5 @@ export * from './src/string-arrays/copy-case.mjs';
186
190
  export * from './src/string-arrays/format-big-number.mjs';
187
191
 
188
192
  export default [
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
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
190
194
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "type": "module",
5
5
  "description": "Usefull functions for solving programming tasks",
6
6
  "keywords": [
@@ -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
- * @module developer-utils
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 {};