@hyperfrontend/list-utils 0.0.1

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/index.esm.js ADDED
@@ -0,0 +1,174 @@
1
+ import { getType } from '@hyperfrontend/data-utils';
2
+
3
+ /**
4
+ * Creates a FIFO (First-In-First-Out) list.
5
+ *
6
+ * @template T The type of elements in the list, must be an object.
7
+ * @returns {FifoList<T>} A FIFO list with methods to manipulate and query the list.
8
+ */
9
+ function createFifoList() {
10
+ const list = new Set();
11
+ const push = (item) => {
12
+ if (getType(item) !== 'object') {
13
+ throw new Error('A fifo list only non-primitive values');
14
+ }
15
+ if (list.has(item)) {
16
+ throw new Error('Cannot a item that already exists');
17
+ }
18
+ list.add(item);
19
+ };
20
+ const pull = () => {
21
+ const value = list.values().next().value;
22
+ list.delete(value);
23
+ return value;
24
+ };
25
+ const map = (callback) => {
26
+ return Array.from(list.values()).map(callback);
27
+ };
28
+ const forEach = (callback) => {
29
+ list.forEach(callback);
30
+ };
31
+ const remove = (item) => list.delete(item);
32
+ const has = (item) => list.has(item);
33
+ const size = () => list.size;
34
+ const clear = () => list.clear();
35
+ const result = {
36
+ push,
37
+ pull,
38
+ map,
39
+ forEach,
40
+ remove,
41
+ has,
42
+ size,
43
+ clear,
44
+ };
45
+ return Object.freeze(result);
46
+ }
47
+
48
+ /**
49
+ * Creates a LIFO (Last-In-First-Out) list.
50
+ *
51
+ * @template T The type of elements in the list, must be an object.
52
+ * @returns {LifoList<T>} A LIFO list with methods to manipulate and query the list.
53
+ */
54
+ function createLifoList() {
55
+ const list = new Set();
56
+ const push = (item) => {
57
+ if (getType(item) !== 'object') {
58
+ throw new Error('A lifo list only supports non-primitive values');
59
+ }
60
+ list.add(item);
61
+ };
62
+ const pull = () => {
63
+ const lastItem = Array.from(list).pop();
64
+ if (lastItem) {
65
+ list.delete(lastItem);
66
+ }
67
+ return lastItem;
68
+ };
69
+ const map = (callback) => {
70
+ return Array.from(list).map(callback);
71
+ };
72
+ const forEach = (callback) => {
73
+ list.forEach(callback);
74
+ };
75
+ const remove = (item) => list.delete(item);
76
+ const has = (item) => list.has(item);
77
+ const size = () => list.size;
78
+ const clear = () => {
79
+ list.clear();
80
+ };
81
+ const result = {
82
+ push,
83
+ pull,
84
+ map,
85
+ forEach,
86
+ remove,
87
+ has,
88
+ size,
89
+ clear,
90
+ };
91
+ return Object.freeze(result);
92
+ }
93
+
94
+ /**
95
+ * Creates an array of numbers within a specified range (inclusive).
96
+ *
97
+ * @param start - The starting number of the range
98
+ * @param end - The ending number of the range (inclusive)
99
+ * @returns An array containing all numbers from start to end
100
+ */
101
+ function createRange(start, end) {
102
+ const range = [];
103
+ for (let i = start; i <= end; i += 1) {
104
+ range.push(i);
105
+ }
106
+ return range;
107
+ }
108
+
109
+ /**
110
+ * Creates a value picker that cycles through a list of string values.
111
+ *
112
+ * @param values - The array of string values to cycle through
113
+ * @returns A ValuePicker instance with current and next methods
114
+ */
115
+ function createValuePicker(values) {
116
+ if (!values || !values.length) {
117
+ throw new Error('Expected values not to be an empty list.');
118
+ }
119
+ let index = -1;
120
+ /**
121
+ * Returns the current value without advancing the picker.
122
+ *
123
+ * @returns The current value in the list
124
+ */
125
+ function current() {
126
+ if (index < 0)
127
+ index = 0;
128
+ return values[index];
129
+ }
130
+ /**
131
+ * Advances to and returns the next value, cycling back to the start when reaching the end.
132
+ *
133
+ * @returns The next value in the list
134
+ */
135
+ function next() {
136
+ if (index < 0)
137
+ return current();
138
+ index = ++index < values.length ? index : 0;
139
+ return values[index];
140
+ }
141
+ return {
142
+ current: current,
143
+ next: next,
144
+ };
145
+ }
146
+
147
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
148
+ const getLastKeyInMap = (map) => {
149
+ const items = [...map.keys()];
150
+ return items[items.length - 1];
151
+ };
152
+
153
+ /**
154
+ * Filters out empty, null, undefined, and whitespace-only strings from an array.
155
+ *
156
+ * @param values - The array of strings to filter
157
+ * @returns A new array containing only non-empty strings with content
158
+ */
159
+ function nonEmptyStrings(values) {
160
+ return values.filter((value) => ![undefined, null, ''].includes(value) && value.trim() !== '');
161
+ }
162
+
163
+ /**
164
+ * Removes duplicate strings from an array, preserving insertion order.
165
+ *
166
+ * @param values - The array of strings to deduplicate
167
+ * @returns A new array containing only unique strings
168
+ */
169
+ function uniqueStrings(values) {
170
+ return Array.from(new Set(values));
171
+ }
172
+
173
+ export { createFifoList, createLifoList, createRange, createValuePicker, getLastKeyInMap, nonEmptyStrings, uniqueStrings };
174
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm.js","sources":["../../../../libs/utils/list/src/create-fifo-list.ts","../../../../libs/utils/list/src/create-lifo-list.ts","../../../../libs/utils/list/src/create-range.ts","../../../../libs/utils/list/src/create-value-picker.ts","../../../../libs/utils/list/src/get-last-key-in-map.ts","../../../../libs/utils/list/src/non-empty-strings.ts","../../../../libs/utils/list/src/unique-strings.ts"],"sourcesContent":[null,null,null,null,null,null,null],"names":[],"mappings":";;AAaA;;;;;AAKG;SACa,cAAc,GAAA;AAC5B,IAAA,MAAM,IAAI,GAAW,IAAI,GAAG,EAAE;AAE9B,IAAA,MAAM,IAAI,GAAG,CAAC,IAAO,KAAU;AAC7B,QAAA,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,QAAQ,EAAE;AAC9B,YAAA,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC;QAC1D;AACA,QAAA,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE;AAClB,YAAA,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC;QACtD;AACA,QAAA,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;AAChB,IAAA,CAAC;IAED,MAAM,IAAI,GAAG,MAAQ;QACnB,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK;AACxC,QAAA,IAAI,CAAC,MAAM,CAAI,KAAK,C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The array of strings to filter
5
+ * @returns A new array containing only non-empty strings with content
6
+ */
7
+ export declare function nonEmptyStrings(values: string[]): string[];
8
+ //# sourceMappingURL=non-empty-strings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"non-empty-strings.d.ts","sourceRoot":"","sources":["../../../../libs/utils/list/src/non-empty-strings.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAE1D"}
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@hyperfrontend/list-utils",
3
+ "version": "0.0.1",
4
+ "description": "Purpose-built collection utilities for queue management, filtering, and iteration patterns.",
5
+ "license": "MIT",
6
+ "sideEffects": false,
7
+ "engines": {
8
+ "node": ">=18.0.0",
9
+ "npm": ">=8.0.0"
10
+ },
11
+ "keywords": [
12
+ "list",
13
+ "array",
14
+ "queue",
15
+ "fifo",
16
+ "lifo",
17
+ "stack",
18
+ "collections",
19
+ "iterator",
20
+ "round-robin",
21
+ "deduplication",
22
+ "string-sanitization",
23
+ "typescript",
24
+ "immutable",
25
+ "functional",
26
+ "zero-dependencies"
27
+ ],
28
+ "main": "./index.cjs.js",
29
+ "dependencies": {
30
+ "@hyperfrontend/data-utils": "0.0.1"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/AndrewRedican/hyperfrontend.git"
35
+ },
36
+ "bugs": {
37
+ "url": "https://github.com/AndrewRedican/hyperfrontend/issues"
38
+ },
39
+ "homepage": "https://github.com/AndrewRedican/hyperfrontend#readme",
40
+ "author": {
41
+ "name": "andrew.redican.mejia@gmail.com",
42
+ "url": "https://hyperfrontend.dev"
43
+ },
44
+ "exports": {
45
+ "./package.json": "./package.json",
46
+ ".": {
47
+ "types": "./index.d.ts",
48
+ "import": "./index.esm.js",
49
+ "require": "./index.cjs.js"
50
+ },
51
+ "./bundle": {
52
+ "import": "./bundle/index.iife.min.js",
53
+ "require": "./bundle/index.iife.min.js"
54
+ }
55
+ },
56
+ "module": "./index.esm.js",
57
+ "types": "./index.d.ts",
58
+ "unpkg": "./bundle/index.umd.min.js",
59
+ "jsdelivr": "./bundle/index.umd.min.js"
60
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Removes duplicate strings from an array, preserving insertion order.
3
+ *
4
+ * @param values - The array of strings to deduplicate
5
+ * @returns A new array containing only unique strings
6
+ */
7
+ export declare function uniqueStrings(values: string[]): string[];
8
+ //# sourceMappingURL=unique-strings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unique-strings.d.ts","sourceRoot":"","sources":["../../../../libs/utils/list/src/unique-strings.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAExD"}