Skip to content

Commit a95206e

Browse files
committed
Fmt
1 parent 53c6e95 commit a95206e

File tree

7 files changed

+111
-103
lines changed

7 files changed

+111
-103
lines changed

SECURITY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
| **Versions** | **Release Date** | **Long Term Support Date** | **End Of Life Date** |
66
|:-:|:-:|:-:|:-:|
7-
| v4.X.X | 2024-01-11 | 2024-01-11 | *Unknown* |
7+
| v5.X.X | 2025-07-22 | 2025-07-22 | *Unknown* |
8+
| v4.X.X | 2024-01-11 | 2024-01-11 | 2026-02-28 |
89

910
> [!NOTE]
1011
> - The date format is according to the specification ISO 8601.

assert.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

collector.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { notDeepStrictEqual } from "node:assert";
2+
/**
3+
* Collect unique elements into the collector.
4+
*/
5+
export class UniqueCollector<T> {
6+
get [Symbol.toStringTag](): string {
7+
return "UniqueCollector";
8+
}
9+
#bin: Set<T> = new Set<T>();
10+
#count: bigint = 0n;
11+
[Symbol.iterator](): SetIterator<T> {
12+
return this.#bin[Symbol.iterator]();
13+
}
14+
/**
15+
* Collect the element into the collector.
16+
* @param {T} o Element that need to collect.
17+
* @returns {boolean} Whether the element is collected (i.e.: element is unique).
18+
*/
19+
collect(o: T): boolean {
20+
this.#count += 1n;
21+
const isExist: boolean = this.has(o);
22+
if (!isExist) {
23+
this.#bin.add(o);
24+
}
25+
return !isExist;
26+
}
27+
/**
28+
* Get the number of collects in the collector.
29+
* @returns {bigint}
30+
*/
31+
getCount(): bigint {
32+
return this.#count;
33+
}
34+
/**
35+
* Get the number of unique elements in the collector.
36+
* @returns {number}
37+
*/
38+
getSize(): number {
39+
return this.#bin.size;
40+
}
41+
/**
42+
* Check whether the collector has the element.
43+
* @param {T} o Element that need to check.
44+
* @returns {boolean} Determine result.
45+
*/
46+
has(o: T): boolean {
47+
for (const value of this.values()) {
48+
if (value === o) {
49+
return true;
50+
}
51+
try {
52+
notDeepStrictEqual(value, o);
53+
} catch {
54+
return true;
55+
}
56+
}
57+
return false;
58+
}
59+
/**
60+
* Iterate the elements in the collector.
61+
* @returns {SetIterator<T>}
62+
*/
63+
values(): SetIterator<T> {
64+
return this.#bin.values();
65+
}
66+
}
67+
export default UniqueCollector;

jsr.jsonc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
22
"$schema": "https://deno.land/x/deno/cli/schemas/config-file.v1.json",
33
"name": "@hugoalh/unique-array",
4-
"version": "4.1.1",
4+
"version": "5.0.0",
55
"exports": {
6-
"./assert": "./assert.ts",
7-
"./uniquify": "./uniquify.ts",
6+
"./collector": "./collector.ts",
87
".": "./mod.ts"
98
},
109
"imports": {

uniquify.test.ts renamed to mod.test.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
import { deepStrictEqual } from "node:assert";
2-
import {
3-
uniqueArray,
4-
uniqueIterate,
5-
uniqueIterateSync
6-
} from "./uniquify.ts";
7-
Deno.test("Array 0/0", { permissions: "none" }, () => {
2+
import { uniqueArray } from "./mod.ts";
3+
Deno.test("0/0", { permissions: "none" }, () => {
84
deepStrictEqual(uniqueArray([]), []);
95
});
10-
Deno.test("Array 1/2 1", { permissions: "none" }, () => {
6+
Deno.test("1/2 1", { permissions: "none" }, () => {
117
deepStrictEqual(uniqueArray([
128
{ type: { id: "_ETGENUS" } },
139
{ type: { id: "_ETGENUS" } }
1410
]), [
1511
{ type: { id: "_ETGENUS" } }
1612
]);
1713
});
18-
Deno.test("Array 1/2 2", { permissions: "none" }, () => {
14+
Deno.test("1/2 2", { permissions: "none" }, () => {
1915
deepStrictEqual(uniqueArray([
2016
new Set([1, 2, 3]),
2117
new Set([1, 2, 3])
2218
]), [
2319
new Set([1, 2, 3])
2420
]);
2521
});
26-
Deno.test("Array 2/2", { permissions: "none" }, () => {
22+
Deno.test("2/2", { permissions: "none" }, () => {
2723
deepStrictEqual(uniqueArray([
2824
{
2925
id: "_1p7ZED73OF98VbT1SzSkjn",
@@ -52,7 +48,7 @@ Deno.test("Array 2/2", { permissions: "none" }, () => {
5248
}
5349
]);
5450
});
55-
Deno.test("Array 2/3 1", { permissions: "none" }, () => {
51+
Deno.test("2/3 1", { permissions: "none" }, () => {
5652
deepStrictEqual(uniqueArray([
5753
{ foo: "bar" },
5854
{ foo: "bar" },
@@ -62,7 +58,7 @@ Deno.test("Array 2/3 1", { permissions: "none" }, () => {
6258
{ bar: "gaz" }
6359
]);
6460
});
65-
Deno.test("Array 2/3 2", { permissions: "none" }, () => {
61+
Deno.test("2/3 2", { permissions: "none" }, () => {
6662
deepStrictEqual(uniqueArray([
6763
new Set([1, 2, 3]),
6864
new Set([1, 2]),
@@ -72,12 +68,9 @@ Deno.test("Array 2/3 2", { permissions: "none" }, () => {
7268
new Set([1, 2])
7369
]);
7470
});
75-
Deno.test("Array 1/6", { permissions: "none" }, () => {
71+
Deno.test("1/6", { permissions: "none" }, () => {
7672
deepStrictEqual(uniqueArray([{}, {}, {}, {}, {}, {}]), [{}]);
7773
});
78-
Deno.test("Array 6/6", { permissions: "none" }, () => {
74+
Deno.test("6/6", { permissions: "none" }, () => {
7975
deepStrictEqual(uniqueArray([1, 2n, "3", false, true, null]), [1, 2n, "3", false, true, null]);
8076
});
81-
Deno.test("Iterate 5/10", { permissions: "none" }, async () => {
82-
deepStrictEqual(await Array.fromAsync(uniqueIterate([1, 2, 3], new Set([1, 2, 3, 4]).values(), uniqueIterateSync([1, 4, 5]))), [1, 2, 3, 4, 5]);
83-
});

mod.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1-
export { isArrayUnique } from "./assert.ts";
2-
export {
3-
default,
4-
uniqueArray,
5-
uniqueIterate,
6-
uniqueIterateSync
7-
} from "./uniquify.ts";
1+
import { UniqueCollector } from "./collector.ts";
2+
export { UniqueCollector } from "./collector.ts";
3+
/**
4+
* Return unique array elements without any duplicated elements.
5+
* @template {unknown} T
6+
* @param {...readonly T[]} items Arrays that need to have unique elements.
7+
* @returns {T[]} An array with unique elements.
8+
* @example
9+
* ```ts
10+
* uniqueArray([{ foo: "bar" }, { foo: "bar" }, { bar: "gaz" }]);
11+
* //=> [{ foo: "bar" }, { bar: "gaz" }]
12+
* ```
13+
*/
14+
export function uniqueArray<T>(...items: readonly (readonly T[])[]): T[] {
15+
const collector: UniqueCollector<T> = new UniqueCollector<T>();
16+
for (const item of items) {
17+
for (const element of item) {
18+
collector.collect(element);
19+
}
20+
}
21+
return Array.from(collector.values());
22+
}
23+
export default uniqueArray;
24+
/**
25+
* Determine whether the array is contain unique elements.
26+
* @param {readonly unknown[]} item Item that need to determine.
27+
* @returns {boolean} Determine result.
28+
*/
29+
export function isArrayUnique(item: readonly unknown[]): boolean {
30+
return (item.length === uniqueArray(item).length);
31+
}

uniquify.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)