@hyperfrontend/list-utils 0.0.4 → 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/CHANGELOG.md +24 -6
- package/README.md +7 -4
- package/SECURITY.md +50 -15
- package/_dependencies/@hyperfrontend/data-utils/index.cjs.js +214 -0
- package/_dependencies/@hyperfrontend/data-utils/index.esm.js +202 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/array/index.cjs.js +7 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/array/index.esm.js +5 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/date/index.cjs.js +8 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/date/index.esm.js +7 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/error/index.cjs.js +6 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/error/index.esm.js +5 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/map/index.cjs.js +6 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/map/index.esm.js +5 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/math/index.cjs.js +7 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/math/index.esm.js +5 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/object/index.cjs.js +7 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/object/index.esm.js +5 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/set/index.cjs.js +6 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/set/index.esm.js +5 -0
- package/bundle/index.iife.js +10 -134
- package/bundle/index.iife.min.js +1 -2
- package/bundle/index.umd.js +10 -134
- package/bundle/index.umd.min.js +1 -2
- package/index.cjs.js +88 -122
- package/index.d.ts +172 -8
- package/index.d.ts.map +1 -1
- package/index.esm.js +76 -110
- package/package.json +12 -7
- package/bundle/index.iife.js.map +0 -1
- package/bundle/index.iife.min.js.map +0 -1
- package/bundle/index.umd.js.map +0 -1
- package/bundle/index.umd.min.js.map +0 -1
- package/create-fifo-list.d.ts +0 -18
- package/create-fifo-list.d.ts.map +0 -1
- package/create-lifo-list.d.ts +0 -18
- package/create-lifo-list.d.ts.map +0 -1
- package/create-range.d.ts +0 -9
- package/create-range.d.ts.map +0 -1
- package/create-value-picker.d.ts +0 -12
- package/create-value-picker.d.ts.map +0 -1
- package/get-last-key-in-map.d.ts +0 -2
- package/get-last-key-in-map.d.ts.map +0 -1
- package/index.cjs.js.map +0 -1
- package/index.esm.js.map +0 -1
- package/non-empty-strings.d.ts +0 -8
- package/non-empty-strings.d.ts.map +0 -1
- package/unique-strings.d.ts +0 -8
- package/unique-strings.d.ts.map +0 -1
package/index.cjs.js
CHANGED
|
@@ -1,126 +1,39 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/object
|
|
10
|
-
*/
|
|
11
|
-
// Capture references at module initialization time
|
|
12
|
-
const _Object = globalThis.Object;
|
|
13
|
-
/**
|
|
14
|
-
* (Safe copy) Prevents modification of existing property attributes and values,
|
|
15
|
-
* and prevents the addition of new properties.
|
|
16
|
-
*/
|
|
17
|
-
const freeze = _Object.freeze;
|
|
18
|
-
|
|
19
|
-
const registeredClasses = [];
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Safe copies of Array built-in static methods.
|
|
23
|
-
*
|
|
24
|
-
* These references are captured at module initialization time to protect against
|
|
25
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
26
|
-
*
|
|
27
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/array
|
|
28
|
-
*/
|
|
29
|
-
// Capture references at module initialization time
|
|
30
|
-
const _Array = globalThis.Array;
|
|
31
|
-
/**
|
|
32
|
-
* (Safe copy) Determines whether the passed value is an Array.
|
|
33
|
-
*/
|
|
34
|
-
const isArray = _Array.isArray;
|
|
35
|
-
/**
|
|
36
|
-
* (Safe copy) Creates an array from an array-like or iterable object.
|
|
37
|
-
*/
|
|
38
|
-
const from = _Array.from;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Returns the data type of the target.
|
|
42
|
-
* Uses native `typeof` operator, however, makes distinction between `null`, `array`, and `object`.
|
|
43
|
-
* Also, when classes are registered via `registerClass`, it checks if objects are instance of any known registered class.
|
|
44
|
-
*
|
|
45
|
-
* @param target - The target to get the data type of.
|
|
46
|
-
* @returns The data type of the target.
|
|
47
|
-
*/
|
|
48
|
-
const getType = (target) => {
|
|
49
|
-
if (target === null)
|
|
50
|
-
return 'null';
|
|
51
|
-
const nativeDataType = typeof target;
|
|
52
|
-
if (nativeDataType === 'object') {
|
|
53
|
-
if (isArray(target))
|
|
54
|
-
return 'array';
|
|
55
|
-
for (const registeredClass of registeredClasses) {
|
|
56
|
-
if (target instanceof registeredClass)
|
|
57
|
-
return registeredClass.name;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return nativeDataType;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Safe copies of Error built-ins via factory functions.
|
|
65
|
-
*
|
|
66
|
-
* Since constructors cannot be safely captured via Object.assign, this module
|
|
67
|
-
* provides factory functions that use Reflect.construct internally.
|
|
68
|
-
*
|
|
69
|
-
* These references are captured at module initialization time to protect against
|
|
70
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
71
|
-
*
|
|
72
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/error
|
|
73
|
-
*/
|
|
74
|
-
// Capture references at module initialization time
|
|
75
|
-
const _Error = globalThis.Error;
|
|
76
|
-
const _Reflect$1 = globalThis.Reflect;
|
|
77
|
-
/**
|
|
78
|
-
* (Safe copy) Creates a new Error using the captured Error constructor.
|
|
79
|
-
* Use this instead of `new Error()`.
|
|
80
|
-
*
|
|
81
|
-
* @param message - Optional error message.
|
|
82
|
-
* @param options - Optional error options.
|
|
83
|
-
* @returns A new Error instance.
|
|
84
|
-
*/
|
|
85
|
-
const createError = (message, options) => _Reflect$1.construct(_Error, [message, options]);
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Safe copies of Set built-in via factory function.
|
|
89
|
-
*
|
|
90
|
-
* Since constructors cannot be safely captured via Object.assign, this module
|
|
91
|
-
* provides a factory function that uses Reflect.construct internally.
|
|
92
|
-
*
|
|
93
|
-
* These references are captured at module initialization time to protect against
|
|
94
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
95
|
-
*
|
|
96
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/set
|
|
97
|
-
*/
|
|
98
|
-
// Capture references at module initialization time
|
|
99
|
-
const _Set = globalThis.Set;
|
|
100
|
-
const _Reflect = globalThis.Reflect;
|
|
101
|
-
/**
|
|
102
|
-
* (Safe copy) Creates a new Set using the captured Set constructor.
|
|
103
|
-
* Use this instead of `new Set()`.
|
|
104
|
-
*
|
|
105
|
-
* @param iterable - Optional iterable of values.
|
|
106
|
-
* @returns A new Set instance.
|
|
107
|
-
*/
|
|
108
|
-
const createSet = (iterable) => _Reflect.construct(_Set, iterable ? [iterable] : []);
|
|
3
|
+
const index_cjs_js$2 = require('./_dependencies/@hyperfrontend/data-utils/index.cjs.js');
|
|
4
|
+
const index_cjs_js$4 = require('./_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/array/index.cjs.js');
|
|
5
|
+
const index_cjs_js$3 = require('./_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/error/index.cjs.js');
|
|
6
|
+
const index_cjs_js$1 = require('./_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/object/index.cjs.js');
|
|
7
|
+
const index_cjs_js = require('./_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/set/index.cjs.js');
|
|
109
8
|
|
|
110
9
|
/**
|
|
111
10
|
* Creates a FIFO (First-In-First-Out) list.
|
|
112
11
|
*
|
|
113
12
|
* @template T The type of elements in the list, must be an object.
|
|
114
13
|
* @returns {FifoList<T>} A FIFO list with methods to manipulate and query the list.
|
|
14
|
+
* @example Creating and using FIFO queue
|
|
15
|
+
* ```typescript
|
|
16
|
+
* interface Task { id: number; name: string }
|
|
17
|
+
* const queue = createFifoList<Task>()
|
|
18
|
+
*
|
|
19
|
+
* queue.push({ id: 1, name: 'first' })
|
|
20
|
+
* queue.push({ id: 2, name: 'second' })
|
|
21
|
+
*
|
|
22
|
+
* queue.pull() // => { id: 1, name: 'first' }
|
|
23
|
+
* queue.pull() // => { id: 2, name: 'second' }
|
|
24
|
+
* queue.pull() // => undefined (the list is empty)
|
|
25
|
+
* ```
|
|
26
|
+
* @throws {Error} When the pushed item is a primitive rather than an object.
|
|
27
|
+
* @throws {Error} When the pushed item is already in the list. Use has() to check first if a duplicate is expected.
|
|
115
28
|
*/
|
|
116
29
|
function createFifoList() {
|
|
117
|
-
const list = createSet();
|
|
30
|
+
const list = index_cjs_js.createSet();
|
|
118
31
|
const push = (item) => {
|
|
119
|
-
if (getType(item) !== 'object') {
|
|
120
|
-
throw createError('A fifo list only non-primitive values');
|
|
32
|
+
if (index_cjs_js$2.getType(item) !== 'object') {
|
|
33
|
+
throw index_cjs_js$3.createError('A fifo list only supports non-primitive values');
|
|
121
34
|
}
|
|
122
35
|
if (list.has(item)) {
|
|
123
|
-
throw createError('Cannot
|
|
36
|
+
throw index_cjs_js$3.createError('Cannot push an item that already exists in the list');
|
|
124
37
|
}
|
|
125
38
|
list.add(item);
|
|
126
39
|
};
|
|
@@ -130,7 +43,7 @@ function createFifoList() {
|
|
|
130
43
|
return value;
|
|
131
44
|
};
|
|
132
45
|
const map = (callback) => {
|
|
133
|
-
return from(list.values()).map(callback);
|
|
46
|
+
return index_cjs_js$4.from(list.values()).map(callback);
|
|
134
47
|
};
|
|
135
48
|
const forEach = (callback) => {
|
|
136
49
|
list.forEach(callback);
|
|
@@ -149,7 +62,7 @@ function createFifoList() {
|
|
|
149
62
|
size,
|
|
150
63
|
clear,
|
|
151
64
|
};
|
|
152
|
-
return freeze(result);
|
|
65
|
+
return index_cjs_js$1.freeze(result);
|
|
153
66
|
}
|
|
154
67
|
|
|
155
68
|
/**
|
|
@@ -157,24 +70,35 @@ function createFifoList() {
|
|
|
157
70
|
*
|
|
158
71
|
* @template T The type of elements in the list, must be an object.
|
|
159
72
|
* @returns {LifoList<T>} A LIFO list with methods to manipulate and query the list.
|
|
73
|
+
* @example Creating and using LIFO stack
|
|
74
|
+
* ```typescript
|
|
75
|
+
* interface HistoryEntry { url: string; timestamp: number }
|
|
76
|
+
* const history = createLifoList<HistoryEntry>()
|
|
77
|
+
*
|
|
78
|
+
* history.push({ url: '/home', timestamp: 1 })
|
|
79
|
+
* history.push({ url: '/about', timestamp: 2 })
|
|
80
|
+
*
|
|
81
|
+
* history.pull() // => { url: '/about', timestamp: 2 }
|
|
82
|
+
* history.pull() // => { url: '/home', timestamp: 1 }
|
|
83
|
+
* ```
|
|
160
84
|
*/
|
|
161
85
|
function createLifoList() {
|
|
162
|
-
const list = createSet();
|
|
86
|
+
const list = index_cjs_js.createSet();
|
|
163
87
|
const push = (item) => {
|
|
164
|
-
if (getType(item) !== 'object') {
|
|
165
|
-
throw createError('A lifo list only supports non-primitive values');
|
|
88
|
+
if (index_cjs_js$2.getType(item) !== 'object') {
|
|
89
|
+
throw index_cjs_js$3.createError('A lifo list only supports non-primitive values');
|
|
166
90
|
}
|
|
167
91
|
list.add(item);
|
|
168
92
|
};
|
|
169
93
|
const pull = () => {
|
|
170
|
-
const lastItem = from(list).pop();
|
|
94
|
+
const lastItem = index_cjs_js$4.from(list).pop();
|
|
171
95
|
if (lastItem) {
|
|
172
96
|
list.delete(lastItem);
|
|
173
97
|
}
|
|
174
98
|
return lastItem;
|
|
175
99
|
};
|
|
176
100
|
const map = (callback) => {
|
|
177
|
-
return from(list).map(callback);
|
|
101
|
+
return index_cjs_js$4.from(list).map(callback);
|
|
178
102
|
};
|
|
179
103
|
const forEach = (callback) => {
|
|
180
104
|
list.forEach(callback);
|
|
@@ -195,7 +119,7 @@ function createLifoList() {
|
|
|
195
119
|
size,
|
|
196
120
|
clear,
|
|
197
121
|
};
|
|
198
|
-
return freeze(result);
|
|
122
|
+
return index_cjs_js$1.freeze(result);
|
|
199
123
|
}
|
|
200
124
|
|
|
201
125
|
/**
|
|
@@ -204,6 +128,14 @@ function createLifoList() {
|
|
|
204
128
|
* @param start - The starting number of the range
|
|
205
129
|
* @param end - The ending number of the range (inclusive)
|
|
206
130
|
* @returns An array containing all numbers from start to end
|
|
131
|
+
* @example Generating number ranges
|
|
132
|
+
* ```typescript
|
|
133
|
+
* createRange(1, 5)
|
|
134
|
+
* // => [1, 2, 3, 4, 5]
|
|
135
|
+
*
|
|
136
|
+
* createRange(3, 3)
|
|
137
|
+
* // => [3]
|
|
138
|
+
* ```
|
|
207
139
|
*/
|
|
208
140
|
function createRange(start, end) {
|
|
209
141
|
const range = [];
|
|
@@ -218,10 +150,20 @@ function createRange(start, end) {
|
|
|
218
150
|
*
|
|
219
151
|
* @param values - The array of string values to cycle through
|
|
220
152
|
* @returns A ValuePicker instance with current and next methods
|
|
153
|
+
* @example Cycling through values
|
|
154
|
+
* ```typescript
|
|
155
|
+
* const colorPicker = createValuePicker(['red', 'green', 'blue'])
|
|
156
|
+
*
|
|
157
|
+
* colorPicker.current() // => 'red'
|
|
158
|
+
* colorPicker.next() // => 'red' (first call initializes)
|
|
159
|
+
* colorPicker.next() // => 'green'
|
|
160
|
+
* colorPicker.next() // => 'blue'
|
|
161
|
+
* colorPicker.next() // => 'red' (cycles back)
|
|
162
|
+
* ```
|
|
221
163
|
*/
|
|
222
164
|
function createValuePicker(values) {
|
|
223
165
|
if (!values || !values.length) {
|
|
224
|
-
throw createError('Expected values not to be an empty list.');
|
|
166
|
+
throw index_cjs_js$3.createError('Expected values not to be an empty list.');
|
|
225
167
|
}
|
|
226
168
|
let index = -1;
|
|
227
169
|
/**
|
|
@@ -251,7 +193,22 @@ function createValuePicker(values) {
|
|
|
251
193
|
};
|
|
252
194
|
}
|
|
253
195
|
|
|
254
|
-
|
|
196
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
197
|
+
/**
|
|
198
|
+
* Retrieves the final key from a Map by converting keys to an array.
|
|
199
|
+
*
|
|
200
|
+
* @param map - The Map to extract the last key from
|
|
201
|
+
* @returns The key that was inserted last, or undefined if map is empty
|
|
202
|
+
* @example Getting last key from Map
|
|
203
|
+
* ```typescript
|
|
204
|
+
* const userRoles = new Map<string, string>()
|
|
205
|
+
* userRoles.set('alice', 'admin')
|
|
206
|
+
* userRoles.set('bob', 'editor')
|
|
207
|
+
* userRoles.set('charlie', 'viewer')
|
|
208
|
+
*
|
|
209
|
+
* getLastKeyInMap(userRoles) // => 'charlie'
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
255
212
|
const getLastKeyInMap = (map) => {
|
|
256
213
|
const items = [...map.keys()];
|
|
257
214
|
return items[items.length - 1];
|
|
@@ -262,6 +219,11 @@ const getLastKeyInMap = (map) => {
|
|
|
262
219
|
*
|
|
263
220
|
* @param values - The array of strings to filter
|
|
264
221
|
* @returns A new array containing only non-empty strings with content
|
|
222
|
+
* @example Filtering empty strings
|
|
223
|
+
* ```typescript
|
|
224
|
+
* nonEmptyStrings(['hello', '', ' ', 'world', null as unknown as string])
|
|
225
|
+
* // => ['hello', 'world']
|
|
226
|
+
* ```
|
|
265
227
|
*/
|
|
266
228
|
function nonEmptyStrings(values) {
|
|
267
229
|
return values.filter((value) => ![undefined, null, ''].includes(value) && value.trim() !== '');
|
|
@@ -272,9 +234,14 @@ function nonEmptyStrings(values) {
|
|
|
272
234
|
*
|
|
273
235
|
* @param values - The array of strings to deduplicate
|
|
274
236
|
* @returns A new array containing only unique strings
|
|
237
|
+
* @example Deduplicating strings
|
|
238
|
+
* ```typescript
|
|
239
|
+
* uniqueStrings(['apple', 'banana', 'apple', 'cherry', 'banana'])
|
|
240
|
+
* // => ['apple', 'banana', 'cherry']
|
|
241
|
+
* ```
|
|
275
242
|
*/
|
|
276
243
|
function uniqueStrings(values) {
|
|
277
|
-
return from(createSet(values));
|
|
244
|
+
return index_cjs_js$4.from(index_cjs_js.createSet(values));
|
|
278
245
|
}
|
|
279
246
|
|
|
280
247
|
exports.createFifoList = createFifoList;
|
|
@@ -284,4 +251,3 @@ exports.createValuePicker = createValuePicker;
|
|
|
284
251
|
exports.getLastKeyInMap = getLastKeyInMap;
|
|
285
252
|
exports.nonEmptyStrings = nonEmptyStrings;
|
|
286
253
|
exports.uniqueStrings = uniqueStrings;
|
|
287
|
-
//# sourceMappingURL=index.cjs.js.map
|
package/index.d.ts
CHANGED
|
@@ -1,8 +1,172 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
/**
|
|
2
|
+
* A FIFO (First-In-First-Out) list interface.
|
|
3
|
+
*/
|
|
4
|
+
interface FifoList<T extends object> {
|
|
5
|
+
/** Adds an item to the end of the list */
|
|
6
|
+
push(item: T): void;
|
|
7
|
+
/** Removes and returns the first item in the list, or undefined when the list is empty */
|
|
8
|
+
pull(): T | undefined;
|
|
9
|
+
/** Maps each item using the provided callback */
|
|
10
|
+
map<U>(callback: (item: T) => U): U[];
|
|
11
|
+
/** Iterates over each item in the list */
|
|
12
|
+
forEach(callback: (item: T) => void): void;
|
|
13
|
+
/** Removes a specific item from the list */
|
|
14
|
+
remove(item: T): boolean;
|
|
15
|
+
/** Checks if an item exists in the list */
|
|
16
|
+
has(item: T): boolean;
|
|
17
|
+
/** Returns the number of items in the list */
|
|
18
|
+
size(): number;
|
|
19
|
+
/** Removes all items from the list */
|
|
20
|
+
clear(): void;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates a FIFO (First-In-First-Out) list.
|
|
24
|
+
*
|
|
25
|
+
* @template T The type of elements in the list, must be an object.
|
|
26
|
+
* @returns {FifoList<T>} A FIFO list with methods to manipulate and query the list.
|
|
27
|
+
* @example Creating and using FIFO queue
|
|
28
|
+
* ```typescript
|
|
29
|
+
* interface Task { id: number; name: string }
|
|
30
|
+
* const queue = createFifoList<Task>()
|
|
31
|
+
*
|
|
32
|
+
* queue.push({ id: 1, name: 'first' })
|
|
33
|
+
* queue.push({ id: 2, name: 'second' })
|
|
34
|
+
*
|
|
35
|
+
* queue.pull() // => { id: 1, name: 'first' }
|
|
36
|
+
* queue.pull() // => { id: 2, name: 'second' }
|
|
37
|
+
* queue.pull() // => undefined (the list is empty)
|
|
38
|
+
* ```
|
|
39
|
+
* @throws {Error} When the pushed item is a primitive rather than an object.
|
|
40
|
+
* @throws {Error} When the pushed item is already in the list. Use has() to check first if a duplicate is expected.
|
|
41
|
+
*/
|
|
42
|
+
declare function createFifoList<T extends object>(): FifoList<T>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A LIFO (Last-In-First-Out) list interface.
|
|
46
|
+
*/
|
|
47
|
+
interface LifoList<T extends object> {
|
|
48
|
+
/** Adds an item to the list */
|
|
49
|
+
push(item: T): void;
|
|
50
|
+
/** Removes and returns the last item in the list */
|
|
51
|
+
pull(): T | undefined;
|
|
52
|
+
/** Maps each item using the provided callback */
|
|
53
|
+
map<U>(callback: (item: T) => U): U[];
|
|
54
|
+
/** Iterates over each item in the list */
|
|
55
|
+
forEach(callback: (item: T) => void): void;
|
|
56
|
+
/** Removes a specific item from the list */
|
|
57
|
+
remove(item: T): boolean;
|
|
58
|
+
/** Checks if an item exists in the list */
|
|
59
|
+
has(item: T): boolean;
|
|
60
|
+
/** Returns the number of items in the list */
|
|
61
|
+
size(): number;
|
|
62
|
+
/** Removes all items from the list */
|
|
63
|
+
clear(): void;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Creates a LIFO (Last-In-First-Out) list.
|
|
67
|
+
*
|
|
68
|
+
* @template T The type of elements in the list, must be an object.
|
|
69
|
+
* @returns {LifoList<T>} A LIFO list with methods to manipulate and query the list.
|
|
70
|
+
* @example Creating and using LIFO stack
|
|
71
|
+
* ```typescript
|
|
72
|
+
* interface HistoryEntry { url: string; timestamp: number }
|
|
73
|
+
* const history = createLifoList<HistoryEntry>()
|
|
74
|
+
*
|
|
75
|
+
* history.push({ url: '/home', timestamp: 1 })
|
|
76
|
+
* history.push({ url: '/about', timestamp: 2 })
|
|
77
|
+
*
|
|
78
|
+
* history.pull() // => { url: '/about', timestamp: 2 }
|
|
79
|
+
* history.pull() // => { url: '/home', timestamp: 1 }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare function createLifoList<T extends object>(): LifoList<T>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Interface for cycling through a list of values.
|
|
86
|
+
*/
|
|
87
|
+
interface ValuePicker {
|
|
88
|
+
/** Returns the current value without advancing. */
|
|
89
|
+
current: () => string;
|
|
90
|
+
/** Advances and returns the next value, cycling at end. */
|
|
91
|
+
next: () => string;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Creates a value picker that cycles through a list of string values.
|
|
95
|
+
*
|
|
96
|
+
* @param values - The array of string values to cycle through
|
|
97
|
+
* @returns A ValuePicker instance with current and next methods
|
|
98
|
+
* @example Cycling through values
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const colorPicker = createValuePicker(['red', 'green', 'blue'])
|
|
101
|
+
*
|
|
102
|
+
* colorPicker.current() // => 'red'
|
|
103
|
+
* colorPicker.next() // => 'red' (first call initializes)
|
|
104
|
+
* colorPicker.next() // => 'green'
|
|
105
|
+
* colorPicker.next() // => 'blue'
|
|
106
|
+
* colorPicker.next() // => 'red' (cycles back)
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
declare function createValuePicker(values: string[]): ValuePicker;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Creates an array of numbers within a specified range (inclusive).
|
|
113
|
+
*
|
|
114
|
+
* @param start - The starting number of the range
|
|
115
|
+
* @param end - The ending number of the range (inclusive)
|
|
116
|
+
* @returns An array containing all numbers from start to end
|
|
117
|
+
* @example Generating number ranges
|
|
118
|
+
* ```typescript
|
|
119
|
+
* createRange(1, 5)
|
|
120
|
+
* // => [1, 2, 3, 4, 5]
|
|
121
|
+
*
|
|
122
|
+
* createRange(3, 3)
|
|
123
|
+
* // => [3]
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
declare function createRange(start: number, end: number): number[];
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Retrieves the final key from a Map by converting keys to an array.
|
|
130
|
+
*
|
|
131
|
+
* @param map - The Map to extract the last key from
|
|
132
|
+
* @returns The key that was inserted last, or undefined if map is empty
|
|
133
|
+
* @example Getting last key from Map
|
|
134
|
+
* ```typescript
|
|
135
|
+
* const userRoles = new Map<string, string>()
|
|
136
|
+
* userRoles.set('alice', 'admin')
|
|
137
|
+
* userRoles.set('bob', 'editor')
|
|
138
|
+
* userRoles.set('charlie', 'viewer')
|
|
139
|
+
*
|
|
140
|
+
* getLastKeyInMap(userRoles) // => 'charlie'
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
declare const getLastKeyInMap: <K = any>(map: Map<K, any>) => K;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Filters out empty, null, undefined, and whitespace-only strings from an array.
|
|
147
|
+
*
|
|
148
|
+
* @param values - The array of strings to filter
|
|
149
|
+
* @returns A new array containing only non-empty strings with content
|
|
150
|
+
* @example Filtering empty strings
|
|
151
|
+
* ```typescript
|
|
152
|
+
* nonEmptyStrings(['hello', '', ' ', 'world', null as unknown as string])
|
|
153
|
+
* // => ['hello', 'world']
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
declare function nonEmptyStrings(values: string[]): string[];
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Removes duplicate strings from an array, preserving insertion order.
|
|
160
|
+
*
|
|
161
|
+
* @param values - The array of strings to deduplicate
|
|
162
|
+
* @returns A new array containing only unique strings
|
|
163
|
+
* @example Deduplicating strings
|
|
164
|
+
* ```typescript
|
|
165
|
+
* uniqueStrings(['apple', 'banana', 'apple', 'cherry', 'banana'])
|
|
166
|
+
* // => ['apple', 'banana', 'cherry']
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
declare function uniqueStrings(values: string[]): string[];
|
|
170
|
+
|
|
171
|
+
export { createFifoList, createLifoList, createRange, createValuePicker, getLastKeyInMap, nonEmptyStrings, uniqueStrings };
|
|
172
|
+
export type { FifoList, LifoList, ValuePicker };
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../../../../libs/utils/list/src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../../../../libs/utils/list/src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,YAAY,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAClD,YAAY,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAClD,YAAY,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA"}
|