-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
31 lines (31 loc) · 1.04 KB
/
mod.ts
File metadata and controls
31 lines (31 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { UniqueCollector } from "./collector.ts";
export { UniqueCollector } from "./collector.ts";
/**
* Return unique array elements without any duplicated elements.
* @template {unknown} T
* @param {...readonly T[]} items Arrays that need to have unique elements.
* @returns {T[]} An array with unique elements.
* @example
* ```ts
* uniqueArray([{ foo: "bar" }, { foo: "bar" }, { bar: "gaz" }]);
* //=> [{ foo: "bar" }, { bar: "gaz" }]
* ```
*/
export function uniqueArray<T>(...items: readonly (readonly T[])[]): T[] {
const collector: UniqueCollector<T> = new UniqueCollector<T>();
for (const item of items) {
for (const element of item) {
collector.collect(element);
}
}
return Array.from(collector.values());
}
export default uniqueArray;
/**
* Determine whether the array is contain unique elements.
* @param {readonly unknown[]} item Item that need to determine.
* @returns {boolean} Determine result.
*/
export function isArrayUnique(item: readonly unknown[]): boolean {
return (item.length === uniqueArray(item).length);
}