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
Stamp function accept refs #96
Conversation
…rged into the refs when possible.
@@ -464,7 +468,8 @@ MyStamp.create().originalStamp === MyStamp; // true | |||
|
|||
### stamp.props() ### | |||
|
|||
Take n objects and deep merge them to the properties. Creates new stamp. | |||
Take n objects and deep merge them safely to the properties. Creates new stamp. | |||
Note: the merge algorythm will not change any existing `refs` data of a resulting object instance. |
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.
algorithm
Fixed comments above. Thanks guys! |
I don't think ignoring existing properties is a good idea.
We need a name for this arg. It should be referred to as I am starting to question if the I realize this would be a serious breaking change to backward compatibility and therefore probably not wise but I wanted to see what others thought. Would this make sense if backward compatibility were not an issue ? I would like to see a way to circumvent the settings merge behavior. I have some ideas I will put in a PR. |
What "ignoring" are you referring to? The code does not ignore anything. |
@unstoppablecarl #50 might give you some insight maybe. |
I assume this means properties matching |
We discusses this here and agreed that the |
Please, do not use any new terminology to replace the existing. What do you mean by |
|
These are |
Nope, they will be not. "props" will be merged into an instantiated object which already has all the var instance = Object.create(...);
_.mixin(instance, refs);
_.merge(instance, props); |
I didn't completely understand the intended behavior. I get it now that I did some testing. This is the only strange behavior I found but I think it is acceptable given the way js works. var stamp = stampit().props({
connection: 'my connection',
});
var instance = stamp({connection: undefined});
console.log(instance); // { connection: 'my connection' } |
I'll merge this tomorrow if no objections. |
t.equal(o2.deep.bar, 'bar'); | ||
t.equal(o2.deep.baz, 'baz'); | ||
t.equal(o1.shallow1, 'leave me as is', 'A conflicting shallow reference must not be touched by props'); | ||
t.equal(o1.shallow2, 'merge me!', 'A non conflicting shallow reference must be merged form props'); |
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.
Typo? Should be "merged from props."
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.
👍 fixed
@unstoppablecarl The strange behavior you found can be easily fixed. Your code: var stamp = stampit().props({
connection: 'my connection',
});
var instance = stamp({connection: undefined});
console.log(instance); // { connection: 'my connection' } Replacing var stamp = stampit().props({
connection: 'my connection',
});
var instance = stamp({connection: null});
console.log(instance); // { connection: null } |
Since Eric has done his part I'm merging this in. @ericelliott, please publish to NPM. Meanwhile, I'll work on the Advanced Examples. |
That is a confusing example. Normally you only want to override a property if the property is set in the override object. By passing in As @koresar pointed out, if you want to override it with a null value, you should pass |
I understand all of that. As I said before I think it is acceptable given the way js works. |
:) |
@ericelliott @troutowicz @unstoppablecarl Please, review.
The main change is the object instantiation. See
function Factory()
.Now all the references mixed into an object instance as is was before. But then the
props
are safely merged into the instance. "Safely" means that no refs data will be overriden, but props will be merged to the existing properties where possible. See the unit test of this PR.In my opinion this is the very best approach for all existing user cases I have ever met so far.