From 7ffeea85f682a320fcab204a43870f8526ed3085 Mon Sep 17 00:00:00 2001 From: Markus Felten Date: Mon, 6 May 2019 18:51:20 +0200 Subject: [PATCH] fix: merge values array -> prevent duplicates --- README.md | 3 ++- src/util.mjs | 3 ++- tests/merge-test.mjs | 5 +++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aa4a55c7..485cbd6a 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,7 @@ Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G ## merge merge from b into a +When a and b are arrays of values only the none duplaces are appendend to a ### Parameters @@ -283,7 +284,7 @@ genreates a new object tree by removing sensible values like credentials from it ### Parameters - `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** -- `toBeRemoved` (optional, default `key=>key.match(/pass|auth|key|user/)`) +- `toBeRemoved` (optional, default `key=>key.match(/pass|auth|key|user|secret/)`) Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** object tree free of sensible data diff --git a/src/util.mjs b/src/util.mjs index 264ae23e..feab244c 100644 --- a/src/util.mjs +++ b/src/util.mjs @@ -8,6 +8,7 @@ export function createValue(value) { /** * merge from b into a + * When a and b are arrays of values only the none duplaces are appendend to a * @param {any} a * @param {any} b * @return {any} merged b into a @@ -16,7 +17,7 @@ export function merge(a, b) { if (b !== undefined) { if (Array.isArray(a)) { if (Array.isArray(b)) { - return [...a, ...b]; + return [...a,...a.filter(x => !b.includes(x))] } return [...a, b]; } diff --git a/tests/merge-test.mjs b/tests/merge-test.mjs index e0382efd..ba108e4c 100644 --- a/tests/merge-test.mjs +++ b/tests/merge-test.mjs @@ -13,3 +13,8 @@ test("merge", async t => a2: { b1: 7 } } )); + +test("merge array", async t => + t.deepEqual(await expand({ x: ["a", "b"] }, { default: { x: ["a", "b"] } }), { + x: ["a", "b"] + }));