@sapphire-sh/utils 1.11.0 → 1.13.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/lib/queue.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  export declare class Queue<T> {
2
- private storage;
3
2
  private head;
4
3
  private tail;
5
- private increaseHead;
6
- private increaseTail;
4
+ private _size;
5
+ constructor();
7
6
  enqueue(element: T): void;
8
7
  dequeue(): T | undefined;
9
8
  get size(): number;
9
+ isEmpty(): boolean;
10
+ peek(): T | undefined;
11
+ has(element: T): boolean;
10
12
  }
package/lib/queue.js CHANGED
@@ -1,47 +1,67 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Queue = void 0;
4
+ class QueueNode {
5
+ constructor(value) {
6
+ this.value = value;
7
+ this._next = null;
8
+ }
9
+ get next() {
10
+ return this._next;
11
+ }
12
+ setNext(newNode) {
13
+ this._next = newNode;
14
+ }
15
+ }
4
16
  class Queue {
5
17
  constructor() {
6
- this.storage = {};
7
- this.head = 0;
8
- this.tail = 0;
9
- }
10
- increaseHead() {
11
- if (this.head === Number.MAX_SAFE_INTEGER) {
12
- this.head = 0;
13
- }
14
- else {
15
- this.head += 1;
16
- }
18
+ this.head = null;
19
+ this.tail = null;
20
+ this._size = 0;
17
21
  }
18
- increaseTail() {
19
- if (this.tail === Number.MAX_SAFE_INTEGER) {
20
- this.tail = 0;
22
+ enqueue(element) {
23
+ const newNode = new QueueNode(element);
24
+ if (this.isEmpty() || !this.tail) {
25
+ this.head = newNode;
26
+ this.tail = newNode;
21
27
  }
22
28
  else {
23
- this.tail += 1;
29
+ this.tail.setNext(newNode);
30
+ this.tail = newNode;
24
31
  }
25
- }
26
- enqueue(element) {
27
- this.storage[this.tail] = element;
28
- this.increaseTail();
32
+ this._size += 1;
29
33
  }
30
34
  dequeue() {
31
- if (this.size === 0) {
35
+ if (this.isEmpty() || !this.head) {
32
36
  return;
33
37
  }
34
- const element = this.storage[this.head];
35
- delete this.storage[this.head];
36
- this.increaseHead();
37
- return element;
38
+ const value = this.head.value;
39
+ this.head = this.head.next;
40
+ this._size -= 1;
41
+ if (this.isEmpty()) {
42
+ this.tail = null;
43
+ }
44
+ return value;
38
45
  }
39
46
  get size() {
40
- const size = this.tail - this.head;
41
- if (size < 0) {
42
- return size + Number.MAX_SAFE_INTEGER;
47
+ return this._size;
48
+ }
49
+ isEmpty() {
50
+ return this._size === 0;
51
+ }
52
+ peek() {
53
+ var _a;
54
+ return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
55
+ }
56
+ has(element) {
57
+ let node = this.head;
58
+ while (node !== null) {
59
+ if (node.value === element) {
60
+ return true;
61
+ }
62
+ node = node.next;
43
63
  }
44
- return size;
64
+ return false;
45
65
  }
46
66
  }
47
67
  exports.Queue = Queue;
package/lib/sleep.js CHANGED
@@ -2,6 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.sleep = void 0;
4
4
  const sleep = (ms) => {
5
- return new Promise((resolve) => window.setTimeout(resolve, ms));
5
+ return new Promise((resolve) => globalThis.setTimeout(resolve, ms));
6
6
  };
7
7
  exports.sleep = sleep;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire-sh/utils",
3
- "version": "1.11.0",
3
+ "version": "1.13.0",
4
4
  "description": "@sapphire-sh/utils",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",