Skip to content

Commit 45bc7c8

Browse files
Merge pull request #3 from typescript-package/develop
v0.1.0
2 parents a2ea062 + 7f7fc51 commit 45bc7c8

File tree

7 files changed

+98
-113
lines changed

7 files changed

+98
-113
lines changed

package-lock.json

Lines changed: 10 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@typescript-package/collection",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"author": "wwwdev.io <dev@wwwdev.io>",
55
"description": "A lightweight TypeScript library for data collection.",
66
"license": "MIT",
@@ -9,7 +9,7 @@
99
"registry": "https://registry.npmjs.org"
1010
},
1111
"peerDependencies": {
12-
"@typedly/collection": "^1.1.0"
12+
"@typedly/collection": "^2.0.0"
1313
},
1414
"scripts": {
1515
"prepublishOnly": "npm run pkg && npm run clean",

src/adapter/lib/set.adapter.ts

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,56 @@ import { CollectionAdapter } from '@typedly/collection';
44
* @description The Set collection adapter.
55
* @export
66
* @class SetAdapter
7-
* @template Element The type of the elements in the Set.
8-
* @template [Type=Set<Element>] The type of the underlying Set collection.
9-
* @implements {CollectionAdapter<Element, Type>}
7+
* @template E The type of the elements in the Set.
8+
* @template [T=Set<E>] The type of the underlying Set collection.
9+
* @implements {CollectionAdapter<E, T>}
1010
*/
1111
export class SetAdapter<
12-
Element,
13-
Type = Set<Element>
14-
> implements CollectionAdapter<Element, Type> {
12+
E,
13+
T extends Set<E> = Set<E>
14+
> implements CollectionAdapter<E, T> {
1515
public version: string = '1.0.0';
16-
1716
get [Symbol.toStringTag](): string {
1817
return 'SetAdapter';
1918
}
20-
21-
get [Symbol.iterator](): Iterator<Element> {
22-
return (this.#collection as unknown as Set<Element>)[Symbol.iterator]();
19+
[Symbol.iterator](): Iterator<E> {
20+
return this.#collection[Symbol.iterator]();
2321
}
24-
25-
26-
protected get collection(): Type {
22+
protected get collection(): T {
2723
return this.#collection;
2824
}
29-
3025
public get size(): number {
31-
return (this.collection as unknown as Set<Element>).size;
26+
return this.collection.size;
3227
}
33-
34-
public get value(): Type {
35-
return this.collection as unknown as Type;
28+
public get value(): T {
29+
return this.collection as unknown as T;
3630
}
37-
38-
#collection: Type;
39-
40-
constructor(...collection: Element[]) {
41-
this.#collection = new Set(collection) as Type;
31+
#collection: T;
32+
constructor(...collection: E[]) {
33+
this.#collection = new Set(collection) as T;
4234
}
43-
44-
public add(element: Element): this {
45-
return (this.collection as unknown as Set<Element>).add(element), this;
35+
public add(...element: E[]): this {
36+
return element.forEach(e => this.collection.add(e)), this;
4637
}
47-
4838
public clear(): this {
49-
return (this.collection as unknown as Set<Element>).clear(), this;
39+
return this.collection.clear(), this;
5040
}
51-
5241
public destroy(): this {
5342
return this.clear(), (this.#collection = null as any), this;
5443
}
55-
56-
public delete(element: Element): boolean {
57-
return (this.collection as unknown as Set<Element>).delete(element);
44+
public delete(...element: E[]): boolean {
45+
return element.every(e => this.collection.delete(e));
5846
}
59-
60-
public forEach(callbackfn: (element: Element, element2: Element, collection: CollectionAdapter<Element, Type>) => void, thisArg?: any): this {
61-
return (this.collection as unknown as Set<Element>).forEach((value, value2) => callbackfn.call(thisArg, value, value2, this as any)), this;
47+
public forEach(callbackfn: (element: E, element2: E, collection: CollectionAdapter<E, T>) => void, thisArg?: any): this {
48+
return this.collection.forEach((value, value2) => callbackfn.call(thisArg, value, value2, this as any)), this;
6249
}
63-
64-
public has(element: Element): boolean {
65-
return (this.collection as unknown as Set<Element>).has(element);
50+
public has(...element: E[]): boolean {
51+
return element.every(e => this.collection.has(e));
6652
}
67-
6853
public lock(): this {
6954
return Object.freeze(this.collection), this;
7055
}
71-
72-
public set(value: Type): this {
56+
public set(value: T): this {
7357
return (this.#collection = value), this;
7458
}
7559
}

src/core/lib/collection.base.ts

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,7 @@ export abstract class CollectionBase<
1616
E,
1717
T,
1818
A extends CollectionAdapter<E, T>
19-
> extends CollectionCore<E, T> {
20-
21-
public get [Symbol.toStringTag](): string {
22-
return 'Collection';
23-
}
24-
25-
public get [Symbol.iterator](): Iterator<E> {
26-
return (this.#adapter.value as unknown as any)[Symbol.iterator]();
27-
}
28-
19+
> extends CollectionCore<E, T, A> {
2920
/**
3021
* @description The protected getter and setter for the adapter.
3122
* @protected
@@ -35,51 +26,20 @@ export abstract class CollectionBase<
3526
return this.#adapter;
3627
}
3728

38-
public get size() {
39-
return this.#adapter.size;
40-
}
41-
42-
public get value() {
43-
return this.#adapter.value;
44-
}
45-
4629
#adapter: A;
4730

4831
constructor(
49-
adapter: new (...args: any[]) => A,
32+
adapter: new (...elements: E[]) => A,
5033
...elements: E[]
5134
) {
5235
super();
5336
this.#adapter = new adapter(...elements) as unknown as A;
5437
}
5538

56-
public add(...element: E[]): this {
57-
return element.forEach(e => this.#adapter.add(e)), this;
58-
}
59-
public clear(): this {
60-
return this.#adapter.clear(), this;
61-
}
62-
public clone(): CollectionCore<E, T> {
63-
return new (this.constructor as any)(this.#adapter.value, this.#adapter);
64-
}
65-
public destroy(): this {
66-
return this.clear(),
39+
public override destroy(): this {
40+
return super.clear(),
41+
super.destroy(),
6742
this.#adapter = null as any,
6843
this;
6944
}
70-
public delete(...element: E[]): boolean {
71-
return element.every(e => this.#adapter.delete(e));
72-
}
73-
public forEach(callbackfn: (element: E, element2: E, collection: CollectionCore<E, T>) => void, thisArg?: any): this {
74-
return this.#adapter.forEach(callbackfn as any, thisArg), this;
75-
}
76-
public has(element: E): boolean {
77-
return this.#adapter.has(element);
78-
}
79-
public lock(): this {
80-
return this.#adapter.lock?.(), this;
81-
}
82-
public set(value: T): this {
83-
return this.#adapter.set(value), this;
84-
}
8545
}

src/core/lib/collection.core.ts

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,58 @@
11
// Interface.
2-
import { CollectionShape } from '@typedly/collection';
2+
import { CollectionAdapter, CollectionShape } from '@typedly/collection';
33
/**
44
* @description The core abstract class for `Type` collections of elements `Element` type.
55
* @export
66
* @abstract
77
* @class CollectionCore
8-
* @template Element type in collection.
9-
* @template Type of the collection.
10-
* @implements {CollectionShape<Element, Type>}
8+
* @template E type in collection.
9+
* @template T of the collection.
10+
* @template {CollectionAdapter<E, T>} A Adapter type.
11+
* @implements {CollectionShape<E, T>}
1112
*/
12-
export abstract class CollectionCore<Element, Type> implements CollectionShape<Element, Type> {
13-
abstract get [Symbol.toStringTag](): string;
14-
abstract get [Symbol.iterator](): Iterator<Element>;
15-
abstract value: Type;
16-
abstract add(...element: Element[]): this;
17-
abstract clear(): this;
18-
abstract delete(...element: Element[]): boolean;
19-
abstract destroy(): this;
20-
abstract forEach(callbackfn: (element: Element, element2: Element, collection: CollectionShape<Element, Type>) => void, thisArg?: any): void;
21-
abstract has(element: Element): boolean;
22-
abstract lock(): this;
23-
abstract set(value: Type): this;
24-
abstract readonly size: number;
13+
export abstract class CollectionCore<
14+
E,
15+
T,
16+
A extends CollectionAdapter<E, T>
17+
> implements CollectionShape<E, T> {
18+
get [Symbol.toStringTag](): string {
19+
return 'Collection';
20+
}
21+
22+
[Symbol.iterator](): Iterator<E> {
23+
return this.adapter[Symbol.iterator]();
24+
}
25+
26+
protected abstract get adapter(): A
27+
28+
public get size(): number {
29+
return this.adapter.size;
30+
}
31+
public get value(): T {
32+
return this.adapter.value;
33+
}
34+
public add(...element: E[]): this {
35+
return this.adapter.add(...element), this;
36+
}
37+
public clear(): this {
38+
return this.adapter.clear(), this;
39+
}
40+
public delete(...element: E[]): boolean {
41+
return this.adapter.delete(...element);
42+
}
43+
public destroy(): this {
44+
return this.adapter.destroy?.(), this;
45+
}
46+
public forEach(callbackfn: (element: E, element2: E, collection: CollectionShape<E, T>) => void, thisArg?: any): void {
47+
return this.adapter.forEach(callbackfn as any, thisArg);
48+
}
49+
public has(...element: E[]): boolean {
50+
return this.adapter.has(...element);
51+
}
52+
public lock(): this {
53+
return this.adapter.lock?.(), this;
54+
}
55+
public set(value: T): this {
56+
return this.adapter.set(value), this;
57+
}
2558
}

src/lib/collection.class.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { CollectionAdapter } from "@typedly/collection";
77
* @export
88
* @class Collection
99
* @template E The type of elements in the collection.
10-
* @template {new (...args: any[]) => CollectionAdapter<E, any>} A The type of the adapter.
10+
* @template {new (...elements: E[]) => CollectionAdapter<E, any>} A The type of the adapter.
1111
* @extends {CollectionBase<E, any, InstanceType<A>>}
1212
*/
1313
export class Collection<
1414
E,
15-
A extends new (...args: any[]) => CollectionAdapter<E, any>
15+
A extends new (...elements: E[]) => CollectionAdapter<E, any>
1616
> extends CollectionBase<E, any, InstanceType<A>> {
1717
constructor(adapter: A, ...elements: E[]) {
1818
super(adapter as any, ...elements);

src/test/collection.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ collection.add(27, 29, 31, 33);
88
// Deletes.
99
collection.delete(29, 31);
1010

11+
for (const element of collection) {
12+
console.log(`element: `, element);
13+
}
14+
1115
console.log(`size: `, collection.size); // Output: 5
1216

1317
describe("Collection SetAdapter", () => {

0 commit comments

Comments
 (0)