diff --git a/index.d.ts b/index.d.ts index 067a143..8d47d80 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,287 +1,296 @@ -declare module '@augu/collections' { - namespace collections { - /** Returns the version of this library */ - export const version: string; - - type Predicate - = (this: ThisArg, value: Value, index: Index, key: Key) => ReturnAs; - - type ReducePredicate = (this: ThisArg, acc: Acc, current: Current) => ReturnAs; - type UndetailedPredicate = (value: Value, index: Index, key: Key) => ReturnAs; - type MinimalPredicate = (this: ThisArg, value: Value) => ReturnAs; - type UndetailedMinimalPredicate = (value: Value) => ReturnAs; - - /** - * Represents a class to hold key-value pairs using [[Collection]]. This is a extension - * of [Map] to add Array-like functions and a update builder. - * - * @template K The key structure for this [[Collection]] - * @template V The value structure for this [[Collection]] - */ - export class Collection extends Map { - public ['constructor']: typeof Collection; - - /** Returns if this [[`Collection`]] is empty or not */ - public get empty(): boolean; - - /** - * Use a predicate function to filter out anything and return a new Array - * @param predicate The predicate function to filter out - * @param thisArg An additional `this` context if needed - * @returns A new Array of the values that returned `true` in the predicate function - */ - filter>(predicate: Predicate, thisArg?: ThisArg): V[]; - - /** - * Use a predicate function to map anything into a new array - * @param predicate The predicate function to map out and return a new array - * @param thisArg An additional `this` context if needed - * @returns A new Array of the values from that function - */ - map>( - predicate: Predicate, - thisArg?: ThisArg - ): S[]; - - /** - * Returns a random value from the collection - * @returns A random value or `null` if the collection is empty - */ - random(): V | null; - - /** - * Reduce the collection and return a new initial value - * @param predicate The predicate function - * @param initialValue The initial value - */ - reduce( - predicate: ReducePredicate, V, S, S>, - initialValue?: S - ): S; - - /** - * Returns the first element in the collection - */ - first(): V | undefined; - - /** - * Returns an Array of the values from the correspondant `amount` - * @param amount The amount to fetch from - */ - first(amount: number): V[]; - - /** - * Returns the last element in the collection - */ - last(): V | undefined; - - /** - * Returns an Array of the values from the correspondant `amount` - * @param amount The amount to fetch from - */ - last(amount: number): V[]; - - /** - * Returns the last element in the collection - */ - lastKey(): K | undefined; - - /** - * Returns an Array of the values from the correspondant `amount` - * @param amount The amount to fetch from - */ - lastKey(amount: number): K[]; - - /** - * Returns the first key in the collection - */ - firstKey(): K | undefined; - - /** - * Returns an Array of the keys from the correspondant `amount` - * @param amount The amount to fetch from - */ - firstKey(amount: number): K[]; - - /** - * Returns all of the values as an Array - */ - toArray(): V[]; - - /** - * Returns all of the keys as an Array - */ - toKeyArray(): K[]; - - /** - * Gets the first item in the collection and removes it (if provided) - * @param remove If we should remove it or not - */ - shift(remove?: boolean): V | undefined; - - /** - * Gets the last item in the collection and removes it(if provided) - * @param remove If we should remove it or not - */ - unshift(remove: boolean): V | undefined; - - /** - * Find a value in the collection from it's predicate function - * @param predicate The predicate function - * @param thisArg An additional `this` context if needed - * @returns The value found or `null` if not found - */ - find>( - predicate: MinimalPredicate, - thisArg?: ThisArg - ): V | null; - - /** - * Computes a value if it's absent in this Collection - * @param key The key to find - * @param insert Item to add when it doesn't exist - */ - emplace(key: K, insert: V | (() => V)): V; - - /** - * Similar to [Array.sort], which basically sorts the values of this Collection - * to return a value - * - * @param compareFn The compare function - * @returns The value - */ - sort(compareFn: (a: V, b: V) => number): V[]; - - /** - * Similar to [Array.sort], which basically sorts the values of this Collection - * to return a value - * - * @param compareFn The compare function - * @returns The value - */ - sortKeys(compareFn: (a: K, b: K) => number): K[]; - - /** - * Similar to [Array.some], this function tests whether atleast 1 item - * in the predicate function passes the test in the values cache. - * - * @param func The function to use to filter out - * @returns A boolean value if 1 item of the cache is truthy - */ - some(func: (item: V) => boolean): boolean; - - /** - * Similar to [Array.some], this functions tests whether atleast 1 key - * in the predicate function passes the test in the key cache. - * - * @param func The function to use to filter out - * @returns A boolean value if 1 item of the cache is truthy - */ - someKeys(func: (item: K) => boolean): boolean; - - /** - * Bulk add items into this [[`Collection`]] using an object - * @param obj The object to bulk-add to this [[`Collection`]] - */ - bulkAdd(obj: { [X in K]: V }): void; - } - - /** - * Represents a [[Queue]] class, which handles queue-based systems in a simple class. - * @template T The structure of this [[Queue]] instance - */ - export class Queue { - private items: T[]; - - /** - * Inserts a new element at the start of the callstack - * @notice This is for backwards compatibility for Queue.add from 0.x - * @param item The item to push - * @returns The size of this [[Queue]] - */ - public addFirst: (item: T) => number; - - /** - * Pushes a new item at the end of the callstack - * @notice This is for backwards compatibility for Queue.add from 0.x - * @param item The item to push - * @returns The size of this [[Queue]] - */ - public add: (item: T) => number; - - /** - * Represents a [[Queue]] class, which handles queue-based systems in a simple class. - * @param items The items to inject when creating a new instance - */ - constructor(items?: T[]); - - /** Returns if this [[`Queue`]] is empty or not */ - public get empty(): boolean; - - /** - * Pushes a new item at the end of the callstack - * @param item The item to push - * @returns The size of this [[Queue]] - */ - push(item: T): number; - - /** - * Inserts a new element at the start of the callstack - * @param item The item to push - * @returns The size of this [[Queue]] - */ - unshift(item: T): number; - - /** - * Returns the first item in the cache and removes it from the cache - */ - shift(): T | undefined; - - /** - * Returns the last item in the cache and removes it from the cache - */ - pop(): T | undefined; - - /** - * Finds an item in the cache or returns `undefined` if not found - * @param predicate The predicate function - */ - find(predicate: (item: T) => boolean): T | undefined; - - /** - * Returns the the queued items as an Array - */ - toArray(): T[]; - - /** - * Returns the last value of the cache - */ - last(): T | undefined; - - /** - * Returns the value or `null` if not found - * @param index The index to peek at - * @returns A value if it didn't return null - */ - get(index: number): T | null; - - /** - * Removes the item from the queue - * - * @warning Use `Queue#tick` to remove all items! - * @param item The item to remove - */ - remove(item: T | number): this; - - /** - * Checks if the key is included in the cache - * @param key The key to find - */ - includes(key: T): boolean; - - [Symbol.iterator](): IteratorResult; - } - } - - export = collections; -} +declare module '@augu/collections' { + namespace collections { + /** Returns the version of this library */ + export const version: string; + + type Predicate + = (this: ThisArg, value: Value, index: Index, key: Key) => ReturnAs; + + type ReducePredicate = (this: ThisArg, acc: Acc, current: Current) => ReturnAs; + type UndetailedPredicate = (value: Value, index: Index, key: Key) => ReturnAs; + type MinimalPredicate = (this: ThisArg, value: Value) => ReturnAs; + type UndetailedMinimalPredicate = (value: Value) => ReturnAs; + + /** + * Represents a class to hold key-value pairs using [[Collection]]. This is a extension + * of [Map] to add Array-like functions and a update builder. + * + * @template K The key structure for this [[Collection]] + * @template V The value structure for this [[Collection]] + */ + export class Collection extends Map { + public ['constructor']: typeof Collection; + + /** Returns if this [[`Collection`]] is empty or not */ + public get empty(): boolean; + + /** + * Use a predicate function to filter out anything and return a new Array + * @param predicate The predicate function to filter out + * @param thisArg An additional `this` context if needed + * @returns A new Array of the values that returned `true` in the predicate function + */ + filter>(predicate: Predicate, thisArg?: ThisArg): V[]; + + /** + * Use a predicate function to map anything into a new array + * @param predicate The predicate function to map out and return a new array + * @param thisArg An additional `this` context if needed + * @returns A new Array of the values from that function + */ + map>( + predicate: Predicate, + thisArg?: ThisArg + ): S[]; + + /** + * Returns a random value from the collection + * @returns A random value or `null` if the collection is empty + */ + random(): V | null; + + /** + * Reduce the collection and return a new initial value + * @param predicate The predicate function + * @param initialValue The initial value + */ + reduce( + predicate: ReducePredicate, V, S, S>, + initialValue?: S + ): S; + + /** + * Returns the first element in the collection + */ + first(): V | undefined; + + /** + * Returns an Array of the values from the correspondant `amount` + * @param amount The amount to fetch from + */ + first(amount: number): V[]; + + /** + * Returns the last element in the collection + */ + last(): V | undefined; + + /** + * Returns an Array of the values from the correspondant `amount` + * @param amount The amount to fetch from + */ + last(amount: number): V[]; + + /** + * Returns the last element in the collection + */ + lastKey(): K | undefined; + + /** + * Returns an Array of the values from the correspondant `amount` + * @param amount The amount to fetch from + */ + lastKey(amount: number): K[]; + + /** + * Returns the first key in the collection + */ + firstKey(): K | undefined; + + /** + * Returns an Array of the keys from the correspondant `amount` + * @param amount The amount to fetch from + */ + firstKey(amount: number): K[]; + + /** + * Returns all of the values as an Array + */ + toArray(): V[]; + + /** + * Returns all of the keys as an Array + */ + toKeyArray(): K[]; + + /** + * Gets the first item in the collection and removes it (if provided) + * @param remove If we should remove it or not + */ + shift(remove?: boolean): V | undefined; + + /** + * Gets the last item in the collection and removes it(if provided) + * @param remove If we should remove it or not + */ + unshift(remove: boolean): V | undefined; + + /** + * Find a value in the collection from it's predicate function + * @param predicate The predicate function + * @param thisArg An additional `this` context if needed + * @returns The value found or `null` if not found + */ + find>( + predicate: MinimalPredicate, + thisArg?: ThisArg + ): V | null; + + /** + * Computes a value if it's absent in this Collection + * @param key The key to find + * @param insert Item to add when it doesn't exist + */ + emplace(key: K, insert: V | (() => V)): V; + + /** + * Similar to [Array.sort], which basically sorts the values of this Collection + * to return a value + * + * @param compareFn The compare function + * @returns The value + */ + sort(compareFn: (a: V, b: V) => number): V[]; + + /** + * Similar to [Array.sort], which basically sorts the values of this Collection + * to return a value + * + * @param compareFn The compare function + * @returns The value + */ + sortKeys(compareFn: (a: K, b: K) => number): K[]; + + /** + * Similar to [Array.some], this function tests whether atleast 1 item + * in the predicate function passes the test in the values cache. + * + * @param func The function to use to filter out + * @returns A boolean value if 1 item of the cache is truthy + */ + some(func: (item: V) => boolean): boolean; + + /** + * Similar to [Array.some], this functions tests whether atleast 1 key + * in the predicate function passes the test in the key cache. + * + * @param func The function to use to filter out + * @returns A boolean value if 1 item of the cache is truthy + */ + someKeys(func: (item: K) => boolean): boolean; + + /** + * Bulk add items into this [[`Collection`]] using an object + * @param obj The object to bulk-add to this [[`Collection`]] + */ + bulkAdd(obj: { + [x: string]: V + [x: number]: V + }): void; + } + + /** + * Represents a [[Queue]] class, which handles queue-based systems in a simple class. + * @template T The structure of this [[Queue]] instance + */ + export class Queue { + private items: T[]; + + /** + * Inserts a new element at the start of the callstack + * @notice This is for backwards compatibility for Queue.add from 0.x + * @param item The item to push + * @returns The size of this [[Queue]] + */ + public addFirst: (item: T) => number; + + /** + * Pushes a new item at the end of the callstack + * @deprecated This is for backwards compatibility for Queue.add from 0.x + * @param item The item to push + * @returns The size of this [[Queue]] + */ + public add: (item: T) => number; + + /** + * Represents a [[Queue]] class, which handles queue-based systems in a simple class. + * @param items The items to inject when creating a new instance + */ + constructor(items?: T[]); + + /** Returns if this [[`Queue`]] is empty or not */ + public get empty(): boolean; + + /** + * Returns the size of the Queue + * @returns The size of this [[Queue]] + */ + public size(): number; + + /** + * Pushes a new item at the end of the callstack + * @param item The item to push + * @returns The size of this [[Queue]] + */ + push(item: T): number; + + /** + * Inserts a new element at the start of the callstack + * @param item The item to push + * @returns The size of this [[Queue]] + */ + unshift(item: T): number; + + /** + * Returns the first item in the cache and removes it from the cache + */ + shift(): T | undefined; + + /** + * Returns the last item in the cache and removes it from the cache + */ + pop(): T | undefined; + + /** + * Finds an item in the cache or returns `undefined` if not found + * @param predicate The predicate function + */ + find(predicate: (item: T) => boolean): T | undefined; + + /** + * Returns the the queued items as an Array + */ + toArray(): T[]; + + /** + * Returns the last value of the cache + */ + last(): T | undefined; + + /** + * Returns the value or `null` if not found + * @param index The index to peek at + * @returns A value if it didn't return null + */ + get(index: number): T | null; + + /** + * Removes the item from the queue + * + * @warning Use `Queue#tick` to remove all items! + * @param item The item to remove + */ + remove(item: T | number): this; + + /** + * Checks if the key is included in the cache + * @param key The key to find + */ + includes(key: T): boolean; + + [Symbol.iterator](): IteratorResult; + } + } + + export = collections; +} diff --git a/package.json b/package.json index d9ed063..3aa62ab 100644 --- a/package.json +++ b/package.json @@ -1,47 +1,47 @@ -{ - "name": "@augu/collections", - "description": "📝 Collections library made in TypeScript", - "version": "1.0.1", - "types": "index.d.ts", - "main": "build/index.js", - "funding": { - "url": "https://github.com/auguwu/collections?sponsor=1" - }, - "license": "MIT", - "files": [ - "build/", - "esm.mjs", - "index.d.ts" - ], - "author": "August ", - "repository": "https://github.com/auguwu/collections", - "exports": { - ".": [ - { - "require": "./build/index.js", - "import": "./esm.mjs" - }, - "./build/index.js" - ], - "./esm": "./esm.mjs" - }, - "devDependencies": { - "@augu/eslint-config": "1.9.0", - "@types/jest": "26.0.19", - "@types/node": "14.14.16", - "@typescript-eslint/eslint-plugin": "4.11.1", - "@typescript-eslint/parser": "4.11.1", - "eslint": "7.16.0", - "jest": "26.6.3", - "ts-jest": "26.4.4", - "typedoc": "0.20.1", - "typescript": "4.1.3" - }, - "scripts": { - "prepare": "npm run lint && rm -fr build && tsc", - "docgen": "node scripts/docgen.js", - "build": "npm run lint && npm test && rm -fr build && tsc", - "lint": "eslint src --ext .ts --fix", - "test": "jest --config jest.config.js --no-cache --i" - } -} +{ + "name": "@augu/collections", + "description": "📝 Collections library made in TypeScript", + "version": "1.0.2", + "types": "index.d.ts", + "main": "build/index.js", + "funding": { + "url": "https://github.com/auguwu/collections?sponsor=1" + }, + "license": "MIT", + "files": [ + "build/", + "esm.mjs", + "index.d.ts" + ], + "author": "August ", + "repository": "https://github.com/auguwu/collections", + "exports": { + ".": [ + { + "require": "./build/index.js", + "import": "./esm.mjs" + }, + "./build/index.js" + ], + "./esm": "./esm.mjs" + }, + "devDependencies": { + "@augu/eslint-config": "1.9.0", + "@types/jest": "26.0.19", + "@types/node": "14.14.16", + "@typescript-eslint/eslint-plugin": "4.11.1", + "@typescript-eslint/parser": "4.11.1", + "eslint": "7.16.0", + "jest": "26.6.3", + "ts-jest": "26.4.4", + "typedoc": "0.20.1", + "typescript": "4.1.3" + }, + "scripts": { + "prepare": "npm run lint && rm -fr build && tsc", + "docgen": "node scripts/docgen.js", + "build": "npm run lint && npm test && rm -fr build && tsc", + "lint": "eslint src --ext .ts --fix", + "test": "jest --config jest.config.js --no-cache --i" + } +} diff --git a/src/Queue.ts b/src/Queue.ts index 6a7b8e0..b4db25e 100644 --- a/src/Queue.ts +++ b/src/Queue.ts @@ -1,189 +1,196 @@ -/** - * Copyright (c) 2019-2021 August - * - * 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: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * 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. - */ - -import * as utils from './utils'; - -/** - * Represents a [[Queue]] class, which handles queue-based systems in a simple class. - * @template T The structure of this [[Queue]] instance - */ -export class Queue { - private items: T[]; - - /** - * Inserts a new element at the start of the callstack - * @notice This is for backwards compatibility for Queue.add from 0.x - * @param item The item to push - * @returns The size of this [[Queue]] - */ - public addFirst!: (item: T) => number; - - /** - * Pushes a new item at the end of the callstack - * @notice This is for backwards compatibility for Queue.add from 0.x - * @param item The item to push - * @returns The size of this [[Queue]] - */ - public add!: (item: T) => number; - - /** - * Represents a [[Queue]] class, which handles queue-based systems in a simple class. - * @param items The items to inject when creating a new instance - */ - constructor(items?: T[]) { - this.items = items ?? []; - - const compat = [ - 'add', - 'addFirst' - ] as const; - - for (let i = 0; i < compat.length; i++) { - let func; - switch (compat[i]) { - case 'addFirst': - func = this.unshift; - break; - - case 'add': - func = this.push; - break; - - default: - func = undefined; - break; - } - - if (func !== undefined) { - this[compat[i] as any] = function (thiz: Queue, ...args: any[]) { - return func.apply(thiz, [...args]); - }.bind(this); - } - } - } - - /** Returns if this [[`Queue`]] is empty or not */ - get empty() { - return this.items.length === 0; - } - - /** - * Pushes a new item at the end of the callstack - * @param item The item to push - * @returns The size of this [[Queue]] - */ - push(item: T) { - this.items.push(item); - return this.items.length; - } - - /** - * Inserts a new element at the start of the callstack - * @param item The item to push - * @returns The size of this [[Queue]] - */ - unshift(item: T) { - this.items.unshift(item); - return this.items.length; - } - - /** - * Returns the first item in the cache and removes it from the cache - */ - shift() { - return this.items.shift(); - } - - /** - * Returns the last item in the cache and removes it from the cache - */ - pop() { - return this.items.pop(); - } - - /** - * Finds an item in the cache or returns `undefined` if not found - * @param predicate The predicate function - */ - find(predicate: (item: T) => boolean) { - return this.items.find(predicate); - } - - /** - * Returns the the queued items as an Array - */ - toArray() { - return this.items; - } - - /** - * Returns the last value of the cache - */ - last() { - return this.items[this.items.length - 1]; - } - - /** - * Returns the value or `null` if not found - * @param index The index to peek at - * @returns A value if it didn't return null - */ - get(index: number): T | null { - if (!this.items.length) return null; - - const item = this.items[index]; - return (item === void 0 || item === null) ? null : item!; - } - - /** - * Removes the item from the queue - * - * @warning Use `Queue#tick` to remove all items! - * @param item The item to remove - */ - remove(item: T | number) { - const r = utils.removeArray(this.items, item); - this.items = r; - - return this; - } - - /** - * Checks if the key is included in the cache - * @param key The key to find - */ - includes(key: T) { - return this.items.includes(key); - } - - [Symbol.iterator]() { - let index = -1; - const items = this.toArray(); - - return { - next: () => ({ - value: items[++index], - done: index >= items.length - }) - }; - } -} +/** + * Copyright (c) 2019-2021 August + * + * 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: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * 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. + */ + +import * as utils from './utils'; + +/** + * Represents a [[Queue]] class, which handles queue-based systems in a simple class. + * @template T The structure of this [[Queue]] instance + */ +export class Queue { + private items: T[]; + + /** + * Inserts a new element at the start of the callstack + * @notice This is for backwards compatibility for Queue.add from 0.x + * @param item The item to push + * @returns The size of this [[Queue]] + */ + public addFirst!: (item: T) => number; + + /** + * Pushes a new item at the end of the callstack + * @notice This is for backwards compatibility for Queue.add from 0.x + * @param item The item to push + * @returns The size of this [[Queue]] + */ + public add!: (item: T) => number; + + /** + * Represents a [[Queue]] class, which handles queue-based systems in a simple class. + * @param items The items to inject when creating a new instance + */ + constructor(items?: T[]) { + this.items = items ?? []; + + const compat = [ + 'add', + 'addFirst' + ] as const; + + for (let i = 0; i < compat.length; i++) { + let func; + switch (compat[i]) { + case 'addFirst': + func = this.unshift; + break; + + case 'add': + func = this.push; + break; + + default: + func = undefined; + break; + } + + if (func !== undefined) { + this[compat[i] as any] = function (thiz: Queue, ...args: any[]) { + return func.apply(thiz, [...args]); + }.bind(this); + } + } + } + + /** Returns if this [[`Queue`]] is empty or not */ + get empty() { + return this.items.length === 0; + } + + /** + * Pushes a new item at the end of the callstack + * @param item The item to push + * @returns The size of this [[Queue]] + */ + push(item: T) { + this.items.push(item); + return this.items.length; + } + + /** + * Inserts a new element at the start of the callstack + * @param item The item to push + * @returns The size of this [[Queue]] + */ + unshift(item: T) { + this.items.unshift(item); + return this.items.length; + } + + /** + * Returns the first item in the cache and removes it from the cache + */ + shift() { + return this.items.shift(); + } + + /** + * Returns the last item in the cache and removes it from the cache + */ + pop() { + return this.items.pop(); + } + + /** + * Finds an item in the cache or returns `undefined` if not found + * @param predicate The predicate function + */ + find(predicate: (item: T) => boolean) { + return this.items.find(predicate); + } + + /** + * Returns the the queued items as an Array + */ + toArray() { + return this.items; + } + + /** + * Returns the last value of the cache + */ + last() { + return this.items[this.items.length - 1]; + } + + /** + * Returns the value or `null` if not found + * @param index The index to peek at + * @returns A value if it didn't return null + */ + get(index: number): T | null { + if (!this.items.length) return null; + + const item = this.items[index]; + return (item === void 0 || item === null) ? null : item!; + } + + /** + * Removes the item from the queue + * + * @warning Use `Queue#tick` to remove all items! + * @param item The item to remove + */ + remove(item: T | number) { + const r = utils.removeArray(this.items, item); + this.items = r; + + return this; + } + + /** + * Checks if the key is included in the cache + * @param key The key to find + */ + includes(key: T) { + return this.items.includes(key); + } + + /** + * Returns the size of this [[Queue]] + */ + size() { + return this.items.length; + } + + [Symbol.iterator]() { + let index = -1; + const items = this.toArray(); + + return { + next: () => ({ + value: items[++index], + done: index >= items.length + }) + }; + } +}