From 0d3e59c229654d092e238bad904ecef560f1cbe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Mon, 8 Dec 2025 00:15:04 +0000 Subject: [PATCH 1/7] refactor(SetAdapter): improve type definitions and simplify methods. --- src/adapter/lib/set.adapter.ts | 66 +++++++++++++--------------------- 1 file changed, 25 insertions(+), 41 deletions(-) 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; } } From 93687aa3de7c70a2993176115f997556e755d617 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Mon, 8 Dec 2025 00:16:22 +0000 Subject: [PATCH 2/7] refactor(`CollectionCore`): change the core to have default functionality connected to adapter, and set Symbol.iterator to method. --- src/core/lib/collection.core.ts | 67 ++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 17 deletions(-) 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; + } } From bd4b14112234fce1b5f813dc6f269ec6a9dfeaa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Mon, 8 Dec 2025 00:17:11 +0000 Subject: [PATCH 3/7] refactor(`CollectionBase`): move default functionality to core, and assign only adapter in base. --- src/core/lib/collection.base.ts | 50 ++++----------------------------- 1 file changed, 5 insertions(+), 45 deletions(-) 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; - } } From c37de39803ca68cce592761c883639a909385e61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Mon, 8 Dec 2025 00:17:57 +0000 Subject: [PATCH 4/7] refactor(`Collection`): change the type of `A` constrainment and in docs. --- src/lib/collection.class.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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); From c547d936b1536bfdf255890ba0018b2ed03850ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Mon, 8 Dec 2025 00:21:12 +0000 Subject: [PATCH 5/7] test: use iterator. --- src/test/collection.spec.ts | 4 ++++ 1 file changed, 4 insertions(+) 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", () => { From 284f2f012636767d14f03de2b99dbc1d7938dfc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Mon, 8 Dec 2025 00:21:24 +0000 Subject: [PATCH 6/7] chore(package): update @typedly/collection. --- package-lock.json | 12 ++++++++---- package.json | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 35fc94d..420dfa3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,10 @@ "name": "@typescript-package/collection", "version": "0.0.1", "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..48a304c 100644 --- a/package.json +++ b/package.json @@ -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", From 7f7fc5120f6934096c99332a7960dffc08caa08e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Mon, 8 Dec 2025 00:31:51 +0000 Subject: [PATCH 7/7] 0.1.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 420dfa3..0e7fbef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "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", diff --git a/package.json b/package.json index 48a304c..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",