@common.js/yocto-queue 1.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 ADDED
@@ -0,0 +1,5 @@
1
+ # @common.js/yocto-queue
2
+
3
+ The [yocto-queue](https://www.npmjs.com/package/yocto-queue) package exported as CommonJS modules.
4
+
5
+ Exported from [yocto-queue@1.0.0](https://www.npmjs.com/package/yocto-queue/v/1.0.0) using https://github.com/etienne-martin/common.js.
package/index.d.ts ADDED
@@ -0,0 +1,54 @@
1
+ export default class Queue<ValueType> implements Iterable<ValueType> {
2
+ /**
3
+ The size of the queue.
4
+ */
5
+ readonly size: number;
6
+
7
+ /**
8
+ Tiny queue data structure.
9
+
10
+ The instance is an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols), which means you can iterate over the queue front to back with a β€œfor…of” loop, or use spreading to convert the queue to an array. Don't do this unless you really need to though, since it's slow.
11
+
12
+ @example
13
+ ```
14
+ import Queue from 'yocto-queue';
15
+
16
+ const queue = new Queue();
17
+
18
+ queue.enqueue('πŸ¦„');
19
+ queue.enqueue('🌈');
20
+
21
+ console.log(queue.size);
22
+ //=> 2
23
+
24
+ console.log(...queue);
25
+ //=> 'πŸ¦„ 🌈'
26
+
27
+ console.log(queue.dequeue());
28
+ //=> 'πŸ¦„'
29
+
30
+ console.log(queue.dequeue());
31
+ //=> '🌈'
32
+ ```
33
+ */
34
+ constructor();
35
+
36
+ [Symbol.iterator](): IterableIterator<ValueType>;
37
+
38
+ /**
39
+ Add a value to the queue.
40
+ */
41
+ enqueue(value: ValueType): void;
42
+
43
+ /**
44
+ Remove the next value in the queue.
45
+
46
+ @returns The removed value or `undefined` if the queue is empty.
47
+ */
48
+ dequeue(): ValueType | undefined;
49
+
50
+ /**
51
+ Clear the queue.
52
+ */
53
+ clear(): void;
54
+ }
package/index.js ADDED
@@ -0,0 +1,308 @@
1
+ /*
2
+ How it works:
3
+ `this.#head` is an instance of `Node` which keeps track of its current value and nests another instance of `Node` that keeps the value that comes after it. When a value is provided to `.enqueue()`, the code needs to iterate through `this.#head`, going deeper and deeper to find the last value. However, iterating through every single item is slow. This problem is solved by saving a reference to the last value as `this.#tail` so that it can reference it to add a new value.
4
+ */ "use strict";
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ Object.defineProperty(exports, "default", {
9
+ enumerable: true,
10
+ get: function() {
11
+ return Queue;
12
+ }
13
+ });
14
+ function _checkPrivateRedeclaration(obj, privateCollection) {
15
+ if (privateCollection.has(obj)) {
16
+ throw new TypeError("Cannot initialize the same private elements twice on an object");
17
+ }
18
+ }
19
+ function _classApplyDescriptorGet(receiver, descriptor) {
20
+ if (descriptor.get) {
21
+ return descriptor.get.call(receiver);
22
+ }
23
+ return descriptor.value;
24
+ }
25
+ function _classApplyDescriptorSet(receiver, descriptor, value) {
26
+ if (descriptor.set) {
27
+ descriptor.set.call(receiver, value);
28
+ } else {
29
+ if (!descriptor.writable) {
30
+ throw new TypeError("attempted to set read only private field");
31
+ }
32
+ descriptor.value = value;
33
+ }
34
+ }
35
+ function _classApplyDescriptorUpdate(receiver, descriptor) {
36
+ if (descriptor.set) {
37
+ if (!("__destrWrapper" in descriptor)) {
38
+ descriptor.__destrWrapper = {
39
+ set value (v){
40
+ descriptor.set.call(receiver, v);
41
+ },
42
+ get value () {
43
+ return descriptor.get.call(receiver);
44
+ }
45
+ };
46
+ }
47
+ return descriptor.__destrWrapper;
48
+ } else {
49
+ if (!descriptor.writable) {
50
+ throw new TypeError("attempted to set read only private field");
51
+ }
52
+ return descriptor;
53
+ }
54
+ }
55
+ function _classCallCheck(instance, Constructor) {
56
+ if (!(instance instanceof Constructor)) {
57
+ throw new TypeError("Cannot call a class as a function");
58
+ }
59
+ }
60
+ function _classExtractFieldDescriptor(receiver, privateMap, action) {
61
+ if (!privateMap.has(receiver)) {
62
+ throw new TypeError("attempted to " + action + " private field on non-instance");
63
+ }
64
+ return privateMap.get(receiver);
65
+ }
66
+ function _classPrivateFieldGet(receiver, privateMap) {
67
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
68
+ return _classApplyDescriptorGet(receiver, descriptor);
69
+ }
70
+ function _classPrivateFieldInit(obj, privateMap, value) {
71
+ _checkPrivateRedeclaration(obj, privateMap);
72
+ privateMap.set(obj, value);
73
+ }
74
+ function _classPrivateFieldSet(receiver, privateMap, value) {
75
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
76
+ _classApplyDescriptorSet(receiver, descriptor, value);
77
+ return value;
78
+ }
79
+ function _classPrivateFieldUpdate(receiver, privateMap) {
80
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "update");
81
+ return _classApplyDescriptorUpdate(receiver, descriptor);
82
+ }
83
+ function _defineProperties(target, props) {
84
+ for(var i = 0; i < props.length; i++){
85
+ var descriptor = props[i];
86
+ descriptor.enumerable = descriptor.enumerable || false;
87
+ descriptor.configurable = true;
88
+ if ("value" in descriptor) descriptor.writable = true;
89
+ Object.defineProperty(target, descriptor.key, descriptor);
90
+ }
91
+ }
92
+ function _createClass(Constructor, protoProps, staticProps) {
93
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
94
+ if (staticProps) _defineProperties(Constructor, staticProps);
95
+ return Constructor;
96
+ }
97
+ function _defineProperty(obj, key, value) {
98
+ if (key in obj) {
99
+ Object.defineProperty(obj, key, {
100
+ value: value,
101
+ enumerable: true,
102
+ configurable: true,
103
+ writable: true
104
+ });
105
+ } else {
106
+ obj[key] = value;
107
+ }
108
+ return obj;
109
+ }
110
+ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
111
+ var f, y, t, g, _ = {
112
+ label: 0,
113
+ sent: function() {
114
+ if (t[0] & 1) throw t[1];
115
+ return t[1];
116
+ },
117
+ trys: [],
118
+ ops: []
119
+ };
120
+ return g = {
121
+ next: verb(0),
122
+ "throw": verb(1),
123
+ "return": verb(2)
124
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
125
+ return this;
126
+ }), g;
127
+ function verb(n) {
128
+ return function(v1) {
129
+ return step([
130
+ n,
131
+ v1
132
+ ]);
133
+ };
134
+ }
135
+ function step(op) {
136
+ if (f) throw new TypeError("Generator is already executing.");
137
+ while(_)try {
138
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
139
+ if (y = 0, t) op = [
140
+ op[0] & 2,
141
+ t.value
142
+ ];
143
+ switch(op[0]){
144
+ case 0:
145
+ case 1:
146
+ t = op;
147
+ break;
148
+ case 4:
149
+ _.label++;
150
+ return {
151
+ value: op[1],
152
+ done: false
153
+ };
154
+ case 5:
155
+ _.label++;
156
+ y = op[1];
157
+ op = [
158
+ 0
159
+ ];
160
+ continue;
161
+ case 7:
162
+ op = _.ops.pop();
163
+ _.trys.pop();
164
+ continue;
165
+ default:
166
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
167
+ _ = 0;
168
+ continue;
169
+ }
170
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
171
+ _.label = op[1];
172
+ break;
173
+ }
174
+ if (op[0] === 6 && _.label < t[1]) {
175
+ _.label = t[1];
176
+ t = op;
177
+ break;
178
+ }
179
+ if (t && _.label < t[2]) {
180
+ _.label = t[2];
181
+ _.ops.push(op);
182
+ break;
183
+ }
184
+ if (t[2]) _.ops.pop();
185
+ _.trys.pop();
186
+ continue;
187
+ }
188
+ op = body.call(thisArg, _);
189
+ } catch (e) {
190
+ op = [
191
+ 6,
192
+ e
193
+ ];
194
+ y = 0;
195
+ } finally{
196
+ f = t = 0;
197
+ }
198
+ if (op[0] & 5) throw op[1];
199
+ return {
200
+ value: op[0] ? op[1] : void 0,
201
+ done: true
202
+ };
203
+ }
204
+ };
205
+ var Node = function Node(value) {
206
+ "use strict";
207
+ _classCallCheck(this, Node);
208
+ _defineProperty(this, "value", void 0);
209
+ _defineProperty(this, "next", void 0);
210
+ this.value = value;
211
+ };
212
+ var _head = /*#__PURE__*/ new WeakMap(), _tail = /*#__PURE__*/ new WeakMap(), _size = /*#__PURE__*/ new WeakMap();
213
+ var _iterator = Symbol.iterator;
214
+ var Queue = /*#__PURE__*/ function() {
215
+ "use strict";
216
+ function Queue() {
217
+ _classCallCheck(this, Queue);
218
+ _classPrivateFieldInit(this, _head, {
219
+ writable: true,
220
+ value: void 0
221
+ });
222
+ _classPrivateFieldInit(this, _tail, {
223
+ writable: true,
224
+ value: void 0
225
+ });
226
+ _classPrivateFieldInit(this, _size, {
227
+ writable: true,
228
+ value: void 0
229
+ });
230
+ this.clear();
231
+ }
232
+ _createClass(Queue, [
233
+ {
234
+ key: "enqueue",
235
+ value: function enqueue(value) {
236
+ var node = new Node(value);
237
+ if (_classPrivateFieldGet(this, _head)) {
238
+ _classPrivateFieldGet(this, _tail).next = node;
239
+ _classPrivateFieldSet(this, _tail, node);
240
+ } else {
241
+ _classPrivateFieldSet(this, _head, node);
242
+ _classPrivateFieldSet(this, _tail, node);
243
+ }
244
+ _classPrivateFieldUpdate(this, _size).value++;
245
+ }
246
+ },
247
+ {
248
+ key: "dequeue",
249
+ value: function dequeue() {
250
+ var current = _classPrivateFieldGet(this, _head);
251
+ if (!current) {
252
+ return;
253
+ }
254
+ _classPrivateFieldSet(this, _head, _classPrivateFieldGet(this, _head).next);
255
+ _classPrivateFieldUpdate(this, _size).value--;
256
+ return current.value;
257
+ }
258
+ },
259
+ {
260
+ key: "clear",
261
+ value: function clear() {
262
+ _classPrivateFieldSet(this, _head, undefined);
263
+ _classPrivateFieldSet(this, _tail, undefined);
264
+ _classPrivateFieldSet(this, _size, 0);
265
+ }
266
+ },
267
+ {
268
+ key: "size",
269
+ get: function get() {
270
+ return _classPrivateFieldGet(this, _size);
271
+ }
272
+ },
273
+ {
274
+ key: _iterator,
275
+ value: function value() {
276
+ var current;
277
+ return __generator(this, function(_state) {
278
+ switch(_state.label){
279
+ case 0:
280
+ current = _classPrivateFieldGet(this, _head);
281
+ _state.label = 1;
282
+ case 1:
283
+ if (!current) return [
284
+ 3,
285
+ 3
286
+ ];
287
+ return [
288
+ 4,
289
+ current.value
290
+ ];
291
+ case 2:
292
+ _state.sent();
293
+ current = current.next;
294
+ return [
295
+ 3,
296
+ 1
297
+ ];
298
+ case 3:
299
+ return [
300
+ 2
301
+ ];
302
+ }
303
+ });
304
+ }
305
+ }
306
+ ]);
307
+ return Queue;
308
+ }();
package/license ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@common.js/yocto-queue",
3
+ "version": "1.0.0",
4
+ "description": "Tiny queue data structure",
5
+ "license": "MIT",
6
+ "repository": "etienne-martin/common.js",
7
+ "funding": "https://github.com/sponsors/sindresorhus",
8
+ "type": "commonjs",
9
+ "engines": {
10
+ "node": ">=12.20"
11
+ },
12
+ "scripts": {
13
+ "//test": "xo && ava && tsd",
14
+ "test": "ava && tsd"
15
+ },
16
+ "files": [
17
+ "index.js",
18
+ "index.d.ts"
19
+ ],
20
+ "devDependencies": {
21
+ "ava": "^3.15.0",
22
+ "tsd": "^0.17.0",
23
+ "xo": "^0.44.0"
24
+ },
25
+ "homepage": "https://github.com/etienne-martin/common.js#readme",
26
+ "dependencies": {},
27
+ "main": "./index.js"
28
+ }
package/readme.md ADDED
@@ -0,0 +1,64 @@
1
+ # yocto-queue [![](https://badgen.net/bundlephobia/minzip/yocto-queue)](https://bundlephobia.com/result?p=yocto-queue)
2
+
3
+ > Tiny queue data structure
4
+
5
+ You should use this package instead of an array if you do a lot of `Array#push()` and `Array#shift()` on large arrays, since `Array#shift()` has [linear time complexity](https://medium.com/@ariel.salem1989/an-easy-to-use-guide-to-big-o-time-complexity-5dcf4be8a444#:~:text=O(N)%E2%80%94Linear%20Time) *O(n)* while `Queue#dequeue()` has [constant time complexity](https://medium.com/@ariel.salem1989/an-easy-to-use-guide-to-big-o-time-complexity-5dcf4be8a444#:~:text=O(1)%20%E2%80%94%20Constant%20Time) *O(1)*. That makes a huge difference for large arrays.
6
+
7
+ > A [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) is an ordered list of elements where an element is inserted at the end of the queue and is removed from the front of the queue. A queue works based on the first-in, first-out ([FIFO](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics))) principle.
8
+
9
+ ## Install
10
+
11
+ ```
12
+ $ npm install yocto-queue
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```js
18
+ import Queue from 'yocto-queue';
19
+
20
+ const queue = new Queue();
21
+
22
+ queue.enqueue('πŸ¦„');
23
+ queue.enqueue('🌈');
24
+
25
+ console.log(queue.size);
26
+ //=> 2
27
+
28
+ console.log(...queue);
29
+ //=> 'πŸ¦„ 🌈'
30
+
31
+ console.log(queue.dequeue());
32
+ //=> 'πŸ¦„'
33
+
34
+ console.log(queue.dequeue());
35
+ //=> '🌈'
36
+ ```
37
+
38
+ ## API
39
+
40
+ ### `queue = new Queue()`
41
+
42
+ The instance is an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols), which means you can iterate over the queue front to back with a β€œfor…of” loop, or use spreading to convert the queue to an array. Don't do this unless you really need to though, since it's slow.
43
+
44
+ #### `.enqueue(value)`
45
+
46
+ Add a value to the queue.
47
+
48
+ #### `.dequeue()`
49
+
50
+ Remove the next value in the queue.
51
+
52
+ Returns the removed value or `undefined` if the queue is empty.
53
+
54
+ #### `.clear()`
55
+
56
+ Clear the queue.
57
+
58
+ #### `.size`
59
+
60
+ The size of the queue.
61
+
62
+ ## Related
63
+
64
+ - [quick-lru](https://github.com/sindresorhus/quick-lru) - Simple β€œLeast Recently Used” (LRU) cache