diff --git a/LICENSE b/LICENSE index 7f9614f..4242fac 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 Catgirl.Enterprises +Copyright (c) 2024 Catgirl Enterprises 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: diff --git a/funkyArray.ts b/funkyArray.ts new file mode 100644 index 0000000..317cc4c --- /dev/null +++ b/funkyArray.ts @@ -0,0 +1,80 @@ +// Copyright (c) Catgirl Enterprises - Licensed under MIT +// Check LICENSE in repository root for more information. + +export default class FunkyArray { + private items:Map = new Map(); + private itemKeys:Array = new Array(); + + private _getKeys() : Array { + const keyArray = new Array(); + let result:IteratorResult; + const iterator = this.items.keys(); + while (!(result = iterator.next()).done) { + keyArray.push(result.value); + } + return keyArray; + } + + public set(key:T, item:TT, regenerate:boolean = true) : TT { + this.items.set(key, item); + if (regenerate) { + this.itemKeys = this._getKeys(); + } + return item; + } + + public remove(key:T, regenerate:boolean = true) { + const success = this.items.delete(key); + if (regenerate) { + this.itemKeys = this._getKeys(); + } + return success; + } + + public removeFirst(regenerate:boolean = true) { + const success = this.items.delete(this.items.keys().next().value); + if (regenerate) { + this.itemKeys = this._getKeys(); + } + return success; + } + + public first() : TT { + return this.items.values().next().value; + } + + public get length() : number { + return this.items.size; + } + + public get(key:T) : TT | undefined { + return this.items.get(key); + } + + public has(key:T) : boolean { + return this.itemKeys.includes(key); + } + + public get keys() : Array { + return this.itemKeys; + } + + public forEach(callback: (value:TT) => void) { + return new Promise(async (resolve, reject) => { + if (this.items.size === 0) { + return resolve(true); + } + + try { + const iterator = this.items.values(); + let result:IteratorResult; + while (!(result = iterator.next()).done) { + await callback(result.value); + } + resolve(true); + } catch (e) { + reject(e); + } + }); + } +} \ No newline at end of file diff --git a/lib/funkyArray.d.ts b/lib/funkyArray.d.ts new file mode 100644 index 0000000..6844694 --- /dev/null +++ b/lib/funkyArray.d.ts @@ -0,0 +1,14 @@ +export default class FunkyArray { + private items; + private itemKeys; + private _getKeys; + set(key: T, item: TT, regenerate?: boolean): TT; + remove(key: T, regenerate?: boolean): boolean; + removeFirst(regenerate?: boolean): boolean; + first(): TT; + get length(): number; + get(key: T): TT | undefined; + has(key: T): boolean; + get keys(): Array; + forEach(callback: (value: TT) => void): Promise; +} diff --git a/lib/funkyArray.js b/lib/funkyArray.js new file mode 100644 index 0000000..0d7e6af --- /dev/null +++ b/lib/funkyArray.js @@ -0,0 +1,74 @@ +"use strict"; +// Copyright (c) Catgirl Enterprises - Licensed under MIT +// Check LICENSE in repository root for more information. +Object.defineProperty(exports, "__esModule", { value: true }); +class FunkyArray { + constructor() { + this.items = new Map(); + this.itemKeys = new Array(); + } + _getKeys() { + const keyArray = new Array(); + let result; + const iterator = this.items.keys(); + while (!(result = iterator.next()).done) { + keyArray.push(result.value); + } + return keyArray; + } + set(key, item, regenerate = true) { + this.items.set(key, item); + if (regenerate) { + this.itemKeys = this._getKeys(); + } + return item; + } + remove(key, regenerate = true) { + const success = this.items.delete(key); + if (regenerate) { + this.itemKeys = this._getKeys(); + } + return success; + } + removeFirst(regenerate = true) { + const success = this.items.delete(this.items.keys().next().value); + if (regenerate) { + this.itemKeys = this._getKeys(); + } + return success; + } + first() { + return this.items.values().next().value; + } + get length() { + return this.items.size; + } + get(key) { + return this.items.get(key); + } + has(key) { + return this.itemKeys.includes(key); + } + get keys() { + return this.itemKeys; + } + forEach(callback) { + return new Promise(async (resolve, reject) => { + if (this.items.size === 0) { + return resolve(true); + } + try { + const iterator = this.items.values(); + let result; + while (!(result = iterator.next()).done) { + await callback(result.value); + } + resolve(true); + } + catch (e) { + reject(e); + } + }); + } +} +exports.default = FunkyArray; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..7a0f996 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,251 @@ +{ + "name": "funky-array", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "funky-array", + "version": "1.0.0", + "license": "MIT", + "devDependencies": { + "check-outdated": "^2.12.0", + "ts-node": "^10.9.2", + "typescript": "^5.5.3" + } + }, + "node_modules/@cspotcode/source-map-support": { + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/trace-mapping": "0.3.9" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.4.15", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz", + "integrity": "sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, + "node_modules/@tsconfig/node10": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.11.tgz", + "integrity": "sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@tsconfig/node12": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz", + "integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==", + "dev": true, + "license": "MIT" + }, + "node_modules/@tsconfig/node14": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz", + "integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==", + "dev": true, + "license": "MIT" + }, + "node_modules/@tsconfig/node16": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz", + "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/node": { + "version": "20.14.9", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.9.tgz", + "integrity": "sha512-06OCtnTXtWOZBJlRApleWndH4JsRVs1pDCc8dLSQp+7PpUpX3ePdHyeNSFTeSe7FtKyQkrlPvHwJOW3SLd8Oyg==", + "dev": true, + "license": "MIT", + "peer": true, + "dependencies": { + "undici-types": "~5.26.4" + } + }, + "node_modules/acorn": { + "version": "8.12.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.12.0.tgz", + "integrity": "sha512-RTvkC4w+KNXrM39/lWCUaG0IbRkWdCv7W/IOW9oU6SawyxulvkQy5HQPVTKxEjczcUvapcrw3cFx/60VN/NRNw==", + "dev": true, + "license": "MIT", + "bin": { + "acorn": "bin/acorn" + }, + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/acorn-walk": { + "version": "8.3.3", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.3.tgz", + "integrity": "sha512-MxXdReSRhGO7VlFe1bRG/oI7/mdLV9B9JJT0N8vZOhF7gFRR5l3M8W9G8JxmKV+JC5mGqJ0QvqfSOLsCPa4nUw==", + "dev": true, + "license": "MIT", + "dependencies": { + "acorn": "^8.11.0" + }, + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/arg": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", + "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==", + "dev": true, + "license": "MIT" + }, + "node_modules/check-outdated": { + "version": "2.12.0", + "resolved": "https://registry.npmjs.org/check-outdated/-/check-outdated-2.12.0.tgz", + "integrity": "sha512-kWThJFiqxAE09XSNJLLD4hWNvLhWdxFLKxOHhxB+XhGlZGyeELXP8V6R/dRrZ5vbjmp9VmoTYe0vp6egftKz7Q==", + "dev": true, + "license": "MIT", + "bin": { + "check-outdated": "check-outdated.js" + }, + "engines": { + "node": ">=10.0.0" + } + }, + "node_modules/create-require": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", + "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/diff": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", + "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.3.1" + } + }, + "node_modules/make-error": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", + "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", + "dev": true, + "license": "ISC" + }, + "node_modules/ts-node": { + "version": "10.9.2", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz", + "integrity": "sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@cspotcode/source-map-support": "^0.8.0", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "v8-compile-cache-lib": "^3.0.1", + "yn": "3.1.1" + }, + "bin": { + "ts-node": "dist/bin.js", + "ts-node-cwd": "dist/bin-cwd.js", + "ts-node-esm": "dist/bin-esm.js", + "ts-node-script": "dist/bin-script.js", + "ts-node-transpile-only": "dist/bin-transpile.js", + "ts-script": "dist/bin-script-deprecated.js" + }, + "peerDependencies": { + "@swc/core": ">=1.2.50", + "@swc/wasm": ">=1.2.50", + "@types/node": "*", + "typescript": ">=2.7" + }, + "peerDependenciesMeta": { + "@swc/core": { + "optional": true + }, + "@swc/wasm": { + "optional": true + } + } + }, + "node_modules/typescript": { + "version": "5.5.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.3.tgz", + "integrity": "sha512-/hreyEujaB0w76zKo6717l3L0o/qEUtRgdvUBvlkhoWeOVMjMuHNHk0BRBzikzuGDqNmPQbg5ifMEqsHLiIUcQ==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true, + "license": "MIT", + "peer": true + }, + "node_modules/v8-compile-cache-lib": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", + "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==", + "dev": true, + "license": "MIT" + }, + "node_modules/yn": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", + "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..e17ad80 --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "funky-array", + "version": "1.0.0", + "description": "Array that supports both iteration and key value indexing.", + "main": "./lib/funkyArray.js", + "types": "./lib/funkyArray.d.ts", + "scripts": { + "build": "tsc --build" + }, + "repository": { + "type": "git", + "url": "https://git.eusv.net/Catgirl.Enterprises/funky-array.git" + }, + "keywords": [ + "typed", + "array", + "typescript", + "types" + ], + "author": "Catgirl.Enterprises", + "license": "MIT", + "devDependencies": { + "check-outdated": "^2.12.0", + "ts-node": "^10.9.2", + "typescript": "^5.5.3" + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..3c240fd --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "module": "CommonJS", + "moduleResolution": "node", + "target": "ES2020", + "esModuleInterop": true, + "resolveJsonModule": true, + "rootDir": "./", + "outDir": "./lib/", + "strict": true, + "declaration": true + } +} \ No newline at end of file