@common.js/yocto-queue 1.0.0 → 1.2.2

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 CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  The [yocto-queue](https://www.npmjs.com/package/yocto-queue) package exported as CommonJS modules.
4
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.
5
+ Exported from [yocto-queue@1.2.2](https://www.npmjs.com/package/yocto-queue/v/1.2.2) using https://github.com/etienne-martin/common.js.
package/index.d.ts CHANGED
@@ -33,8 +33,22 @@ export default class Queue<ValueType> implements Iterable<ValueType> {
33
33
  */
34
34
  constructor();
35
35
 
36
+ /**
37
+ 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. Using the iterator will not remove the items from the queue. If you want that, use `drain()` instead.
38
+
39
+ You can also use spreading to convert the queue to an array. Don't do this unless you really need to though, since it's slow.
40
+ */
36
41
  [Symbol.iterator](): IterableIterator<ValueType>;
37
42
 
43
+ /**
44
+ Returns an iterator that dequeues items as you consume it.
45
+
46
+ This allows you to empty the queue while processing its items.
47
+
48
+ If you want to not remove items as you consume it, use the `Queue` object as an iterator.
49
+ */
50
+ drain(): IterableIterator<ValueType>;
51
+
38
52
  /**
39
53
  Add a value to the queue.
40
54
  */
@@ -47,6 +61,13 @@ export default class Queue<ValueType> implements Iterable<ValueType> {
47
61
  */
48
62
  dequeue(): ValueType | undefined;
49
63
 
64
+ /**
65
+ Get the next value in the queue without removing it.
66
+
67
+ @returns The value or `undefined` if the queue is empty.
68
+ */
69
+ peek(): ValueType | undefined;
70
+
50
71
  /**
51
72
  Clear the queue.
52
73
  */
package/index.js CHANGED
@@ -253,9 +253,24 @@ var Queue = /*#__PURE__*/ function() {
253
253
  }
254
254
  _classPrivateFieldSet(this, _head, _classPrivateFieldGet(this, _head).next);
255
255
  _classPrivateFieldUpdate(this, _size).value--;
256
+ // Clean up tail reference when queue becomes empty
257
+ if (!_classPrivateFieldGet(this, _head)) {
258
+ _classPrivateFieldSet(this, _tail, undefined);
259
+ }
256
260
  return current.value;
257
261
  }
258
262
  },
263
+ {
264
+ key: "peek",
265
+ value: function peek() {
266
+ if (!_classPrivateFieldGet(this, _head)) {
267
+ return;
268
+ }
269
+ return _classPrivateFieldGet(this, _head).value;
270
+ // TODO: Node.js 18.
271
+ // return this.#head?.value;
272
+ }
273
+ },
259
274
  {
260
275
  key: "clear",
261
276
  value: function clear() {
@@ -302,6 +317,34 @@ var Queue = /*#__PURE__*/ function() {
302
317
  }
303
318
  });
304
319
  }
320
+ },
321
+ {
322
+ key: "drain",
323
+ value: function drain() {
324
+ return __generator(this, function(_state) {
325
+ switch(_state.label){
326
+ case 0:
327
+ if (!_classPrivateFieldGet(this, _head)) return [
328
+ 3,
329
+ 2
330
+ ];
331
+ return [
332
+ 4,
333
+ this.dequeue()
334
+ ];
335
+ case 1:
336
+ _state.sent();
337
+ return [
338
+ 3,
339
+ 0
340
+ ];
341
+ case 2:
342
+ return [
343
+ 2
344
+ ];
345
+ }
346
+ });
347
+ }
305
348
  }
306
349
  ]);
307
350
  return Queue;
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "@common.js/yocto-queue",
3
- "version": "1.0.0",
4
- "description": "Tiny queue data structure",
3
+ "version": "1.2.2",
4
+ "description": "yocto-queue package exported as CommonJS modules",
5
5
  "license": "MIT",
6
6
  "repository": "etienne-martin/common.js",
7
7
  "funding": "https://github.com/sponsors/sindresorhus",
8
8
  "type": "commonjs",
9
+ "types": "./index.d.ts",
10
+ "sideEffects": false,
9
11
  "engines": {
10
12
  "node": ">=12.20"
11
13
  },
package/readme.md DELETED
@@ -1,64 +0,0 @@
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