Skip to content

Commit

Permalink
fix: merge values array -> prevent duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
arlac77 committed May 6, 2019
1 parent ef1d452 commit 7ffeea8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
}
Expand Down
5 changes: 5 additions & 0 deletions tests/merge-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}));

0 comments on commit 7ffeea8

Please sign in to comment.