8000 throw error in mergeWith method if no merger is passed (#1503) · immutable-js/immutable-js@2916161 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2916161

Browse files
author
Brandon Lawrence
committed
throw error in mergeWith method if no merger is passed (#1503)
1 parent e65e5af commit 2916161

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

__tests__/merge.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ describe('merge', () => {
4040
);
4141
});
4242

43+
it('throws typeError without merge function', () => {
44+
const m1 = Map({ a: 1, b: 2, c: 3 });
45+
const m2 = Map({ d: 10, b: 20, e: 30 });
46+
expect(() => m1.mergeWith(1, m2)).toThrowError(TypeError);
47+
});
48+
4349
it('provides key as the third argument of merge function', () => {
4450
const m1 = Map({ id: 'temp', b: 2, c: 3 });
4551
const m2 = Map({ id: 10, b: 20, e: 30 });

src/functional/merge.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export function mergeWithSources(collection, sources, merger) {
3838
);
3939
}
4040
if (isImmutable(collection)) {
41-
return collection.mergeWith
41+
const shouldMergeWith = collection.mergeWith && merger instanceof Function;
42+
return shouldMergeWith
4243
? collection.mergeWith(merger, ...sources)
4344
: collection.concat(...sources);
4445
}

src/methods/merge.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export function merge(...iters) {
1414
}
1515

1616
export function mergeWith(merger, ...iters) {
17-
return mergeIntoKeyedWith(this, iters, merger);
17+
if (merger instanceof Function) {
18+
return mergeIntoKeyedWith(this, iters, merger);
19+
}
20+
throw new TypeError('Invalid merger: Expected Function');
1821
}
1922

2023
function mergeIntoKeyedWith(collection, collections, merger) {

0 commit comments

Comments
 (0)
0