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

Skip to content

throw error in mergeWith method if no merger is passed (#1503) #1543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions __tests__/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ describe('merge', () => {
);
});

it('throws typeError without merge function', () => {
const m1 = Map({ a: 1, b: 2, c: 3 });
const m2 = Map({ d: 10, b: 20, e: 30 });
expect(() => m1.mergeWith(1, m2)).toThrowError(TypeError);
});

it('provides key as the third argument of merge function', () => {
const m1 = Map({ id: 'temp', b: 2, c: 3 });
const m2 = Map({ id: 10, b: 20, e: 30 });
Expand Down
11 changes: 8 additions & 3 deletions src/functional/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ export function mergeWithSources(collection, sources, merger) {
);
}
if (isImmutable(collection)) {
return collection.mergeWith
? collection.mergeWith(merger, ...sources)
: collection.concat(...sources);
const mergerIsFunction = merger instanceof Function;
const shouldMergeWith = collection.mergeWith && mergerIsFunction;
if (shouldMergeWith) {
return collection.mergeWith(merger, ...sources);
} else if (collection.merge && !mergerIsFunction) {
return collection.merge(...sources);
}
return collection.concat(...sources);
}
const isArray = Array.isArray(collection);
let merged = collection;
Expand Down
5 changes: 4 additions & 1 deletion src/methods/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export function merge(...iters) {
}

export function mergeWith(merger, ...iters) {
return mergeIntoKeyedWith(this, iters, merger);
if (merger instanceof Function) {
return mergeIntoKeyedWith(this, iters, merger);
}
throw new TypeError('Invalid merger: Expected Function');
}

function mergeIntoKeyedWith(collection, collections, merger) {
Expand Down
0