Skip to content

Commit

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

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

/**
* Merge strategy merging two values by selecting the second value.
*
Expand Down
14 changes: 13 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, replace, deepWithKey,
mix, patch, merge, mergeFull, deep, first, replace, deepWithKey,
} from './merge';

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

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

expect(result.spec.containers.length).toEqual(2);
expect(result.spec.containers[0].name).toEqual('my-app');
expect(result.spec.containers[1].image).toEqual('sidecar:v1');
});

test('replace: basic', () => {
const result = mergeFull(pod, sidecarImage, {
spec: {
Expand Down

0 comments on commit 578460a

Please sign in to comment.