@augu/immutable
Advanced tools
+54
-0
@@ -175,2 +175,30 @@ "use strict"; | ||
| /** | ||
| * Returns the last element in the collection or an Array of the values from the correspondant `amount` | ||
| * @param amount The amount to fetch from | ||
| */ | ||
| lastKey(amount) { | ||
| const iter = [...this.keys()]; | ||
| if (typeof amount === 'undefined') | ||
| return iter[iter.length - 1]; | ||
| if (amount < 0) | ||
| return this.firstKey(amount * -1); | ||
| if (!amount) | ||
| return []; | ||
| return iter.slice(-amount); | ||
| } | ||
| /** | ||
| * Returns the first key in the collection or an Array of the values from the correspondant `amount` | ||
| * @param amount The amount to fetch from | ||
| */ | ||
| firstKey(amount) { | ||
| if (typeof amount === 'undefined') { | ||
| return (this.keys()).next().value; | ||
| } | ||
| if (amount < 0) | ||
| return this.lastKey(amount * -1); | ||
| amount = Math.min(amount, this.size); | ||
| const iterable = this.keys(); | ||
| return Array.from({ length: amount }, () => iterable.next().value); | ||
| } | ||
| /** | ||
| * Deletes all elements from the collection | ||
@@ -196,2 +224,28 @@ */ | ||
| } | ||
| /** | ||
| * Gets the first item in the collection and removes it (if provided) | ||
| * @param remove If we should remove it or not | ||
| */ | ||
| shift(remove = false) { | ||
| const item = this.first(); | ||
| const key = this.firstKey(); | ||
| if (item === undefined || key === undefined) | ||
| return null; | ||
| if (remove) | ||
| this.delete(key); | ||
| return item; | ||
| } | ||
| /** | ||
| * Gets the last item in the collection and removes it(if provided) | ||
| * @param remove If we should remove it or not | ||
| */ | ||
| unshift(remove = false) { | ||
| const item = this.last(); | ||
| const key = this.lastKey(); | ||
| if (item === undefined || key === undefined) | ||
| return null; | ||
| if (remove) | ||
| this.delete(key); | ||
| return item; | ||
| } | ||
| /** Make this class immutable */ | ||
@@ -198,0 +252,0 @@ freeze() { |
+34
-1
@@ -25,3 +25,2 @@ declare module '@augu/immutable' { | ||
| /** Returns the version of the library */ | ||
@@ -122,2 +121,36 @@ export const version: string; | ||
| /** | ||
| * Returns the first element in the collection | ||
| */ | ||
| public firstKey(): T | undefined; | ||
| /** | ||
| * Returns an Array of the keys from the correspondant `amount` | ||
| * @param amount The amount to fetch from | ||
| */ | ||
| public firstKey(amount: number): T[]; | ||
| /** | ||
| * Returns the last key in the collection | ||
| */ | ||
| public lastKey(): T | undefined; | ||
| /** | ||
| * Returns an Array of the keys from the correspondant `amount` | ||
| * @param amount The amount to fetch from | ||
| */ | ||
| public lastKey(amount: number): T[]; | ||
| /** | ||
| * Gets the first item in the collection and removes it (if provided) | ||
| * @param remove If we should remove it or not | ||
| */ | ||
| public shift(remove?: boolean): T | null; | ||
| /** | ||
| * Gets the last item in the collection and removes it(if provided) | ||
| * @param remove If we should remove it or not | ||
| */ | ||
| public unshift(remove?: boolean): T | null; | ||
| /** | ||
| * Make this Collection immutable, all items will never be removed or added | ||
@@ -124,0 +157,0 @@ */ |
+5
-5
| { | ||
| "name": "@augu/immutable", | ||
| "description": "📝 Collections library made in TypeScript", | ||
| "version": "0.3.0", | ||
| "version": "0.3.1", | ||
| "types": "index.d.ts", | ||
@@ -25,8 +25,8 @@ "main": "build/index.js", | ||
| "@types/jest": "26.0.4", | ||
| "@typescript-eslint/eslint-plugin": "3.3.0", | ||
| "@typescript-eslint/parser": "3.3.0", | ||
| "eslint": "7.2.0", | ||
| "@typescript-eslint/eslint-plugin": "3.6.1", | ||
| "@typescript-eslint/parser": "3.6.1", | ||
| "eslint": "7.4.0", | ||
| "jest": "26.1.0", | ||
| "typescript": "3.9.6", | ||
| "ts-jest": "26.1.0" | ||
| "ts-jest": "26.1.2" | ||
| }, | ||
@@ -33,0 +33,0 @@ "scripts": { |
+16
-0
@@ -39,2 +39,18 @@ # @augu/immutable | ||
| ### TimedQueue | ||
| ```ts | ||
| import { TimedQueue } from '@augu/immutable'; | ||
| const queue = new TimedQueue({ | ||
| itemCount: 1, // emit only 1 item | ||
| every: 3000, // stop the timer at 3 seconds | ||
| time: 1000 // every time to "sleep" until enqueueing | ||
| }); | ||
| queue.on('tick', console.log); | ||
| queue.add(['a', 'b', 'c']); // adds 'a', 'b', and 'c' to the queue | ||
| queue.start(); // starts the timer | ||
| ``` | ||
| ## Maintainers | ||
@@ -41,0 +57,0 @@ <table> |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
42288
8.21%1118
7.92%71
29.09%10
66.67%