diff --git a/package-lock.json b/package-lock.json index 35fc94d..0e7fbef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,17 @@ { "name": "@typescript-package/collection", - "version": "0.0.1", + "version": "0.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@typescript-package/collection", - "version": "0.0.1", + "version": "0.1.0", "funding": [ + { + "type": "stripe", + "url": "https://donate.stripe.com/dR614hfDZcJE3wAcMM" + }, { "type": "individual", "url": "https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29" @@ -15,13 +19,13 @@ ], "license": "MIT", "peerDependencies": { - "@typedly/collection": "^1.1.0" + "@typedly/collection": "^2.0.0" } }, "node_modules/@typedly/collection": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@typedly/collection/-/collection-1.1.0.tgz", - "integrity": "sha512-Xt3F2LUhWi0wxIUBFEr05ZiafJRhO2Rw6Ee7kVoJCZEh7CeWFldNejd3HzogVezK9rvo8yhBBnphwlEYlE9eSg==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@typedly/collection/-/collection-2.0.0.tgz", + "integrity": "sha512-g+B2EWAPN2c4icthittXxADTZ3G6PSri5F1qNpZOteZfZCnPCRs4WcRif37ewzDAS7lUiTuyIXHueyRTMld14A==", "funding": [ { "type": "stripe", diff --git a/package.json b/package.json index d6f81f8..7d5b7e4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@typescript-package/collection", - "version": "0.0.1", + "version": "0.1.0", "author": "wwwdev.io ", "description": "A lightweight TypeScript library for data collection.", "license": "MIT", @@ -9,7 +9,7 @@ "registry": "https://registry.npmjs.org" }, "peerDependencies": { - "@typedly/collection": "^1.1.0" + "@typedly/collection": "^2.0.0" }, "scripts": { "prepublishOnly": "npm run pkg && npm run clean", diff --git a/src/adapter/lib/set.adapter.ts b/src/adapter/lib/set.adapter.ts index 17f2e47..bee6742 100644 --- a/src/adapter/lib/set.adapter.ts +++ b/src/adapter/lib/set.adapter.ts @@ -4,72 +4,56 @@ import { CollectionAdapter } from '@typedly/collection'; * @description The Set collection adapter. * @export * @class SetAdapter - * @template Element The type of the elements in the Set. - * @template [Type=Set] The type of the underlying Set collection. - * @implements {CollectionAdapter} + * @template E The type of the elements in the Set. + * @template [T=Set] The type of the underlying Set collection. + * @implements {CollectionAdapter} */ export class SetAdapter< - Element, - Type = Set -> implements CollectionAdapter { + E, + T extends Set = Set +> implements CollectionAdapter { public version: string = '1.0.0'; - get [Symbol.toStringTag](): string { return 'SetAdapter'; } - - get [Symbol.iterator](): Iterator { - return (this.#collection as unknown as Set)[Symbol.iterator](); + [Symbol.iterator](): Iterator { + return this.#collection[Symbol.iterator](); } - - - protected get collection(): Type { + protected get collection(): T { return this.#collection; } - public get size(): number { - return (this.collection as unknown as Set).size; + return this.collection.size; } - - public get value(): Type { - return this.collection as unknown as Type; + public get value(): T { + return this.collection as unknown as T; } - - #collection: Type; - - constructor(...collection: Element[]) { - this.#collection = new Set(collection) as Type; + #collection: T; + constructor(...collection: E[]) { + this.#collection = new Set(collection) as T; } - - public add(element: Element): this { - return (this.collection as unknown as Set).add(element), this; + public add(...element: E[]): this { + return element.forEach(e => this.collection.add(e)), this; } - public clear(): this { - return (this.collection as unknown as Set).clear(), this; + return this.collection.clear(), this; } - public destroy(): this { return this.clear(), (this.#collection = null as any), this; } - - public delete(element: Element): boolean { - return (this.collection as unknown as Set).delete(element); + public delete(...element: E[]): boolean { + return element.every(e => this.collection.delete(e)); } - - public forEach(callbackfn: (element: Element, element2: Element, collection: CollectionAdapter) => void, thisArg?: any): this { - return (this.collection as unknown as Set).forEach((value, value2) => callbackfn.call(thisArg, value, value2, this as any)), this; + public forEach(callbackfn: (element: E, element2: E, collection: CollectionAdapter) => void, thisArg?: any): this { + return this.collection.forEach((value, value2) => callbackfn.call(thisArg, value, value2, this as any)), this; } - - public has(element: Element): boolean { - return (this.collection as unknown as Set).has(element); + public has(...element: E[]): boolean { + return element.every(e => this.collection.has(e)); } - public lock(): this { return Object.freeze(this.collection), this; } - - public set(value: Type): this { + public set(value: T): this { return (this.#collection = value), this; } } diff --git a/src/core/lib/collection.base.ts b/src/core/lib/collection.base.ts index 1fa34b2..85e0f42 100644 --- a/src/core/lib/collection.base.ts +++ b/src/core/lib/collection.base.ts @@ -16,16 +16,7 @@ export abstract class CollectionBase< E, T, A extends CollectionAdapter -> extends CollectionCore { - - public get [Symbol.toStringTag](): string { - return 'Collection'; - } - - public get [Symbol.iterator](): Iterator { - return (this.#adapter.value as unknown as any)[Symbol.iterator](); - } - +> extends CollectionCore { /** * @description The protected getter and setter for the adapter. * @protected @@ -35,51 +26,20 @@ export abstract class CollectionBase< return this.#adapter; } - public get size() { - return this.#adapter.size; - } - - public get value() { - return this.#adapter.value; - } - #adapter: A; constructor( - adapter: new (...args: any[]) => A, + adapter: new (...elements: E[]) => A, ...elements: E[] ) { super(); this.#adapter = new adapter(...elements) as unknown as A; } - public add(...element: E[]): this { - return element.forEach(e => this.#adapter.add(e)), this; - } - public clear(): this { - return this.#adapter.clear(), this; - } - public clone(): CollectionCore { - return new (this.constructor as any)(this.#adapter.value, this.#adapter); - } - public destroy(): this { - return this.clear(), + public override destroy(): this { + return super.clear(), + super.destroy(), this.#adapter = null as any, this; } - public delete(...element: E[]): boolean { - return element.every(e => this.#adapter.delete(e)); - } - public forEach(callbackfn: (element: E, element2: E, collection: CollectionCore) => void, thisArg?: any): this { - return this.#adapter.forEach(callbackfn as any, thisArg), this; - } - public has(element: E): boolean { - return this.#adapter.has(element); - } - public lock(): this { - return this.#adapter.lock?.(), this; - } - public set(value: T): this { - return this.#adapter.set(value), this; - } } diff --git a/src/core/lib/collection.core.ts b/src/core/lib/collection.core.ts index 8baa6d4..c96e4c1 100644 --- a/src/core/lib/collection.core.ts +++ b/src/core/lib/collection.core.ts @@ -1,25 +1,58 @@ // Interface. -import { CollectionShape } from '@typedly/collection'; +import { CollectionAdapter, CollectionShape } from '@typedly/collection'; /** * @description The core abstract class for `Type` collections of elements `Element` type. * @export * @abstract * @class CollectionCore - * @template Element type in collection. - * @template Type of the collection. - * @implements {CollectionShape} + * @template E type in collection. + * @template T of the collection. + * @template {CollectionAdapter} A Adapter type. + * @implements {CollectionShape} */ -export abstract class CollectionCore implements CollectionShape { - abstract get [Symbol.toStringTag](): string; - abstract get [Symbol.iterator](): Iterator; - abstract value: Type; - abstract add(...element: Element[]): this; - abstract clear(): this; - abstract delete(...element: Element[]): boolean; - abstract destroy(): this; - abstract forEach(callbackfn: (element: Element, element2: Element, collection: CollectionShape) => void, thisArg?: any): void; - abstract has(element: Element): boolean; - abstract lock(): this; - abstract set(value: Type): this; - abstract readonly size: number; +export abstract class CollectionCore< + E, + T, + A extends CollectionAdapter +> implements CollectionShape { + get [Symbol.toStringTag](): string { + return 'Collection'; + } + + [Symbol.iterator](): Iterator { + return this.adapter[Symbol.iterator](); + } + + protected abstract get adapter(): A + + public get size(): number { + return this.adapter.size; + } + public get value(): T { + return this.adapter.value; + } + public add(...element: E[]): this { + return this.adapter.add(...element), this; + } + public clear(): this { + return this.adapter.clear(), this; + } + public delete(...element: E[]): boolean { + return this.adapter.delete(...element); + } + public destroy(): this { + return this.adapter.destroy?.(), this; + } + public forEach(callbackfn: (element: E, element2: E, collection: CollectionShape) => void, thisArg?: any): void { + return this.adapter.forEach(callbackfn as any, thisArg); + } + public has(...element: E[]): boolean { + return this.adapter.has(...element); + } + public lock(): this { + return this.adapter.lock?.(), this; + } + public set(value: T): this { + return this.adapter.set(value), this; + } } diff --git a/src/lib/collection.class.ts b/src/lib/collection.class.ts index 5d703bc..9df938b 100644 --- a/src/lib/collection.class.ts +++ b/src/lib/collection.class.ts @@ -7,12 +7,12 @@ import { CollectionAdapter } from "@typedly/collection"; * @export * @class Collection * @template E The type of elements in the collection. - * @template {new (...args: any[]) => CollectionAdapter} A The type of the adapter. + * @template {new (...elements: E[]) => CollectionAdapter} A The type of the adapter. * @extends {CollectionBase>} */ export class Collection< E, - A extends new (...args: any[]) => CollectionAdapter + A extends new (...elements: E[]) => CollectionAdapter > extends CollectionBase> { constructor(adapter: A, ...elements: E[]) { super(adapter as any, ...elements); diff --git a/src/test/collection.spec.ts b/src/test/collection.spec.ts index a45dfc7..e320bd1 100644 --- a/src/test/collection.spec.ts +++ b/src/test/collection.spec.ts @@ -8,6 +8,10 @@ collection.add(27, 29, 31, 33); // Deletes. collection.delete(29, 31); +for (const element of collection) { + console.log(`element: `, element); +} + console.log(`size: `, collection.size); // Output: 5 describe("Collection SetAdapter", () => {