Initial commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
# [←](../../Readme.md) Огляд Array утиліт
|
||||
|
||||
### # arrayDedup\<T>(arr: T[]): T[]
|
||||
|
||||
Видаляє дублікати з масиву об'єктів або складних значень. Порівняння відбувається через детермінований stringify (ключі об'єктів сортуються).
|
||||
|
||||
```ts
|
||||
arrayDedup([{ a: 1 }, { a: 1 }, { a: 2 }]); // → [{ a: 1 }, { a: 2 }]
|
||||
arrayDedup([1, 'x', 1, null, null]); // → [1, 'x', null]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### # arrayDedupPrimitives(array: Array\<number | string | null | undefined>): Array
|
||||
|
||||
Видаляє дублікати з масиву примітивних значень через `Set`. Швидший варіант для простих типів.
|
||||
|
||||
```ts
|
||||
arrayDedupPrimitives([1, 2, 2, 3]); // → [1, 2, 3]
|
||||
arrayDedupPrimitives(['a', 'b', 'a']); // → ['a', 'b']
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### # without\<T>(excludeValues: Iterable\<T>, values: T[]): T[]
|
||||
|
||||
Повертає масив без вказаних значень.
|
||||
|
||||
```ts
|
||||
without([2, 4], [1, 2, 3, 4, 5]); // → [1, 3, 5]
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
type Value = string | number | boolean | null | undefined | Value[] | { [key: string]: Value };
|
||||
|
||||
export function arrayDedup<T extends Value>(arr: T[]): T[] {
|
||||
const seen = new Map<string, T>();
|
||||
|
||||
for (const item of arr) {
|
||||
const key = stableStringify(item);
|
||||
if (!seen.has(key)) {
|
||||
seen.set(key, item);
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(seen.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Deterministic stringify:
|
||||
* - sorts object keys
|
||||
* - preserves arrays order
|
||||
* - distinguishes undefined/null
|
||||
*/
|
||||
function stableStringify(value: Value): string {
|
||||
if (value === undefined) return 'undefined';
|
||||
if (value === null) return 'null';
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return `[${value.map(stableStringify).join(',')}]`;
|
||||
}
|
||||
|
||||
if (typeof value === 'object') {
|
||||
const keys = Object.keys(value).sort();
|
||||
return `{${keys.map((k) => `${k}:${stableStringify(value[k])}`).join(',')}}`;
|
||||
}
|
||||
|
||||
return `${typeof value}:${String(value)}`;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export function arrayDedupPrimitives(array: Array<number | string | null | undefined>) {
|
||||
return Array.from(new Set(array));
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from './arrayDedupPrimitives';
|
||||
export * from './arrayDedup';
|
||||
export * from './without';
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Return an array of values from values excluding the values present in excludeValues .
|
||||
*
|
||||
* @param excludeValues - An array of values to exclude from the result.
|
||||
* @param values - An array of values from which to exclude the excludeValues .
|
||||
* @returns An array of values from values excluding the values present in excludeValues .
|
||||
*
|
||||
* @example
|
||||
* const exclude = [2, 4, 6];
|
||||
* const values = [1, 2, 3, 4, 5, 6];
|
||||
* const result = without(exclude, values);
|
||||
* // result: [1, 3, 5]
|
||||
*/
|
||||
export function without<T>(excludeValues: Iterable<T>, values: T[]): T[] {
|
||||
const excludeSet = new Set<T>(excludeValues);
|
||||
const result: T[] = [];
|
||||
values.forEach((value) => {
|
||||
if (excludeSet.has(value)) return;
|
||||
result.push(value);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user