-
Notifications
You must be signed in to change notification settings - Fork 74
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
Implement merge #154
Open
srph
wants to merge
1
commit into
tjmehta:master
Choose a base branch
from
srph:implement-merge
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Implement merge #154
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @module 101/put | ||
*/ | ||
|
||
var clone = require("./clone"); | ||
var assign = require("./assign"); | ||
var keypather = require('keypather')(); | ||
|
||
/** | ||
* A merge-version of `put`. | ||
* When only key and val are specified, merge returns a partial function which accepts obj. | ||
* @function module:101/merge | ||
* @param {*} [obj] - object which will be cloned and assigned with {key: value} | ||
* @param {string|number} key - key of the value being merged on obj | ||
* @param {*} val - value of the key being merged on obj | ||
* @return {*|function} New obj with new value set or Partial-function merge (which accepts obj) and returns a new obj with val set | ||
*/ | ||
|
||
module.exports = merge; | ||
|
||
function merge (obj, key, val) { | ||
if (arguments.length === 1) { | ||
val = obj | ||
return function(obj) { | ||
var set = clone(obj) | ||
return assign(set, val); | ||
} | ||
} | ||
|
||
if (arguments.length === 2) { | ||
val = key; | ||
key = obj; | ||
return function(obj) { | ||
var set = clone(obj); | ||
keypather.set(set, key, val) | ||
return set; | ||
} | ||
} | ||
|
||
var set = clone(obj); | ||
var old = keypather.get(set, key); | ||
var merged = assign(old, val) | ||
keypather.set(set, key, merged); | ||
return set; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
var Lab = require('lab'); | ||
var lab = exports.lab = Lab.script(); | ||
|
||
var describe = lab.describe; | ||
var it = lab.it; | ||
var Code = require('code'); | ||
var expect = Code.expect; | ||
|
||
var clone = require('../clone'); | ||
// var put = require('../put'); | ||
// var set = require('../set'); | ||
|
||
var merge = require('../merge'); | ||
|
||
describe('merge', function () { | ||
it('should merge the value with the current value on a new object', function(done) { | ||
var obj = { | ||
comments: [{ | ||
replies: [{ | ||
text: 'Hey, swag' | ||
}] | ||
}] | ||
}; | ||
|
||
var original = clone(obj); | ||
var expected = clone(obj); | ||
expected.comments[0].replies[0].text = 'Hi, swag'; | ||
|
||
const merged = merge(obj, 'comments.0.replies.0', { text: 'Hi, swag' }); | ||
expect(merged).to.deep.equal(expected); | ||
expect(merged).not.to.equal(obj); | ||
done(); | ||
}); | ||
|
||
it('should merge a value when used with array functions', function(done) { | ||
var obj = [ | ||
{ id: 2, online: false }, | ||
{ id: 3, online: false } | ||
]; | ||
|
||
var expected = clone(obj); | ||
expected[0].online = true; | ||
expected[1].online = true; | ||
|
||
var merged = obj.map(merge({ online: true })); | ||
expect(merged).to.deep.equal(expected); | ||
expect(merged).not.to.deep.equal(obj); | ||
done(); | ||
}); | ||
|
||
it('should merge a key-value set on when used with array functions', function(done) { | ||
var obj = [ | ||
{ id: 2, user: { online: false } }, | ||
{ id: 3, user: { online: false } } | ||
]; | ||
|
||
var expected = clone(obj); | ||
expected[0].user.online = true; | ||
expected[1].user.online = true; | ||
|
||
var merged = obj.map(merge('user', { online: true })); | ||
expect(merged).to.deep.equal(expected); | ||
expect(merged).not.to.deep.equal(obj); | ||
done(); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests can't be ran without
--leaks
. I can remove this once I'm done with most items.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to update lab. Happy to switch off too. I've been having this problem with other projects. You can leave the leak flag..