Skip to content

Commit 2960ff2

Browse files
Ondřej Bašemjancarik
Ondřej Baše
authored andcommitted
feat: 🎸 Add assignMissingKeys function
1 parent 2e183ca commit 2960ff2

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

Diff for: packages/core/src/__tests__/indexSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ describe('merkur module', () => {
44
it('should keep module interface', () => {
55
expect(merkur).toMatchInlineSnapshot(`
66
{
7+
"assignMissingKeys": [Function],
78
"bindWidgetToFunctions": [Function],
89
"createMerkur": [Function],
910
"createMerkurWidget": [Function],

Diff for: packages/core/src/__tests__/utilsSpec.js

+66
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
assignMissingKeys,
23
isFunction,
34
isUndefined,
45
setDefaultValueForUndefined,
@@ -160,4 +161,69 @@ describe('utils function', () => {
160161
}
161162
});
162163
});
164+
165+
describe('assignMissingKeys(target, ...sources)', () => {
166+
let widget;
167+
168+
beforeEach(() => {
169+
widget = {
170+
a: function (widget, a) {
171+
return a;
172+
},
173+
b: function (widget, b) {
174+
return b;
175+
},
176+
c: {},
177+
d: 'string',
178+
};
179+
});
180+
181+
it.each([
182+
null,
183+
undefined,
184+
0,
185+
42,
186+
'string',
187+
{ e: 1, f: function () {} },
188+
function () {},
189+
false,
190+
true,
191+
[],
192+
])(
193+
'should return the same result as Object.assign(widget, %p)',
194+
(source) => {
195+
const assignResult = { ...widget };
196+
Object.assign(assignResult, source);
197+
const result = assignMissingKeys(widget, source);
198+
199+
expect(result).toEqual(widget);
200+
expect(result).toEqual(assignResult);
201+
}
202+
);
203+
204+
it('should add only e, f, g keys once', () => {
205+
const source1 = {
206+
a: 42,
207+
f: function (widget, f) {
208+
return f;
209+
},
210+
};
211+
const source2 = {
212+
c: function (widget, c) {
213+
return c;
214+
},
215+
e: { prop: 1 },
216+
f: 'string',
217+
g: [],
218+
};
219+
const result = assignMissingKeys(widget, source1, source2);
220+
221+
expect(result).toEqual({
222+
...widget,
223+
e: source2.e,
224+
f: source1.f,
225+
g: source2.g,
226+
});
227+
});
228+
});
163229
});

Diff for: packages/core/src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { createMerkurWidget } from './createMerkurWidget';
22
import { createMerkur, removeMerkur, getMerkur } from './merkur';
33
import {
4+
assignMissingKeys,
45
setDefaultValueForUndefined,
56
bindWidgetToFunctions,
67
hookMethod,
78
isFunction,
89
} from './utils';
910

1011
export {
12+
assignMissingKeys,
1113
bindWidgetToFunctions,
1214
createMerkurWidget,
1315
createMerkur,

Diff for: packages/core/src/utils.js

+12
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,15 @@ export function isFunction(value) {
5757
export function isUndefined(value) {
5858
return typeof value === 'undefined';
5959
}
60+
61+
export function assignMissingKeys(target, ...sources) {
62+
sources.forEach((source) => {
63+
Object.keys(source || {}).forEach((key) => {
64+
if (!(key in target)) {
65+
target[key] = source[key];
66+
}
67+
});
68+
});
69+
70+
return target;
71+
}

0 commit comments

Comments
 (0)