@monstermann/fn
Version:
A utility library for TypeScript.
28 lines (26 loc) • 696 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
import { cloneArray } from "@monstermann/remmi";
//#region src/array/union.ts
/**
* `union(target, source)`
*
* Returns a new array containing all unique elements from both `target` and `source`. Elements from `source` that are not already in `target` are added to the result.
*
* ```ts
* union([1, 2, 3], [3, 4, 5]); // [1, 2, 3, 4, 5]
* ```
*
* ```ts
* pipe([1, 2, 3], union([3, 4, 5])); // [1, 2, 3, 4, 5]
* ```
*/
const union = dfdlT((target, source) => {
let result;
for (const item of source) if (!target.includes(item)) {
result ??= cloneArray(target);
result.push(item);
}
return result ?? target;
}, 2);
//#endregion
export { union };