@lowentry/utils 1.17.1 → 1.18.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowentry/utils",
3
- "version": "1.17.1",
3
+ "version": "1.18.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -0,0 +1,145 @@
1
+ class LinkedListNode
2
+ {
3
+ /** @type {*} */
4
+ value;
5
+
6
+ /** @type {LinkedListNode|null} */
7
+ next = null;
8
+
9
+ /** @type {LinkedListNode|null} */
10
+ previous = null;
11
+
12
+ /**
13
+ * @param {*} value
14
+ */
15
+ constructor(value)
16
+ {
17
+ this.value = value;
18
+ }
19
+ }
20
+
21
+ export class LinkedList
22
+ {
23
+ /** @type {LinkedListNode|null} */
24
+ #head = null;
25
+
26
+ /** @type {LinkedListNode|null} */
27
+ #tail = null;
28
+
29
+ /** @type {number} */
30
+ #size = 0;
31
+
32
+ constructor()
33
+ {
34
+ }
35
+
36
+ /**
37
+ * Returns the number of elements in the list.
38
+ *
39
+ * @returns {number}
40
+ */
41
+ get size()
42
+ {
43
+ return this.#size;
44
+ }
45
+
46
+ /**
47
+ * Adds a new value to the beginning of the list.
48
+ *
49
+ * @param {*} value
50
+ */
51
+ unshift(value)
52
+ {
53
+ const newNode = new LinkedListNode(value);
54
+ if(this.#head === null)
55
+ {
56
+ this.#head = newNode;
57
+ this.#tail = newNode;
58
+ }
59
+ else
60
+ {
61
+ newNode.next = this.#head;
62
+ this.#head.previous = newNode;
63
+ this.#head = newNode;
64
+ }
65
+ this.#size++;
66
+ }
67
+
68
+ /**
69
+ * Adds a new value to the end of the list.
70
+ *
71
+ * @param {*} value
72
+ */
73
+ push(value)
74
+ {
75
+ const newNode = new LinkedListNode(value);
76
+ if(this.#tail === null)
77
+ {
78
+ this.#head = newNode;
79
+ this.#tail = newNode;
80
+ }
81
+ else
82
+ {
83
+ newNode.previous = this.#tail;
84
+ this.#tail.next = newNode;
85
+ this.#tail = newNode;
86
+ }
87
+ this.#size++;
88
+ }
89
+
90
+ /**
91
+ * Removes the first value from the list and returns it.
92
+ *
93
+ * @returns {*|undefined}
94
+ */
95
+ shift()
96
+ {
97
+ if(this.#head === null)
98
+ {
99
+ return undefined;
100
+ }
101
+
102
+ const value = this.#head.value;
103
+ this.#head = this.#head.next;
104
+
105
+ if(this.#head !== null)
106
+ {
107
+ this.#head.previous = null;
108
+ }
109
+ else
110
+ {
111
+ this.#tail = null;
112
+ }
113
+
114
+ this.#size--;
115
+ return value;
116
+ }
117
+
118
+ /**
119
+ * Removes the last value from the list and returns it.
120
+ *
121
+ * @returns {*|undefined}
122
+ */
123
+ pop()
124
+ {
125
+ if(this.#tail === null)
126
+ {
127
+ return undefined;
128
+ }
129
+
130
+ const value = this.#tail.value;
131
+ this.#tail = this.#tail.previous;
132
+
133
+ if(this.#tail !== null)
134
+ {
135
+ this.#tail.next = null;
136
+ }
137
+ else
138
+ {
139
+ this.#head = null;
140
+ }
141
+
142
+ this.#size--;
143
+ return value;
144
+ }
145
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * SerializableMap class extends the native Map to provide a JSON representation.
3
+ *
4
+ * This class can only have string keys, as JSON does not support non-string keys.
5
+ */
6
+ export class SerializableMap extends Map
7
+ {
8
+ /**
9
+ * Returns a JSON representation of the map.
10
+ *
11
+ * @returns {object}
12
+ */
13
+ toJSON()
14
+ {
15
+ return Object.fromEntries(this);
16
+ }
17
+ }
package/src/index.js CHANGED
@@ -1,2 +1,4 @@
1
1
  export {LeUtils} from './LeUtils.js';
2
2
  export {ISSET, IS_ARRAY, ARRAY, IS_OBJECT, OBJECT, STRING, STRING_ANY, INT, INT_ANY, FLOAT, FLOAT_ANY, INT_LAX, INT_LAX_ANY, FLOAT_LAX, FLOAT_LAX_ANY} from './LeTypes.js';
3
+ export {LinkedList} from './classes/LinkedList.js';
4
+ export {SerializableMap} from './classes/SerializableMap.js';