Skip to content

Commit

Permalink
merge: Add a replace merge strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
dlespiau committed Aug 14, 2019
1 parent 8aab6c1 commit f085971
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
41 changes: 41 additions & 0 deletions std/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,47 @@ export function deep(rules) {
return (a, b) => objectMerge2(a, b, rules);
}

/**
* Merge strategy merging two values by selecting the second value.
*
* **Example**:
*
* ```js
* let a = {
* k0: 1,
* o: {
* o0: 'a string',
* o1: 'this will go away!',
* },
* };
*
* let b = {
* k0: 2,
* k1: true,
* o: {
* o0: 'another string',
* },
* };
*
* mergeFull(a, b, { o: replace() });
* ```
*
* Will give the result:
*
* ```js
* {
* k0: 2,
* k1: true,
* o: {
* o0: 'another string',
* },
* }
* ```
*/
export function replace() {
return (_, b) => b;
}

function arrayMergeWithKey(a, b, mergeKey, rules) {
const r = Array.from(a);
const toAppend = [];
Expand Down
13 changes: 12 additions & 1 deletion std/merge.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
mix, patch, merge, mergeFull, deep, deepWithKey,
mix, patch, merge, mergeFull, deep, replace, deepWithKey,
} from './merge';

test('mix objects', () => {
Expand Down Expand Up @@ -199,3 +199,14 @@ test('mergeFull: pick the deep merge strategy when encountering an object as rul
expect(result.spec.containers.length).toEqual(2);
expect(result.spec.containers[1].image).toEqual('sidecar:v2');
});

test('replace: basic', () => {
const result = mergeFull(pod, sidecarImage, {
spec: {
containers: replace(),
},
});

expect(result.spec.containers.length).toEqual(1);
expect(result.spec.containers[0].name).toEqual('sidecar');
});

0 comments on commit f085971

Please sign in to comment.