-
Notifications
You must be signed in to change notification settings - Fork 16
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
Concat Descriptor Properties (proposal) #66
Comments
Any code samples? |
Thanks for the update @unstoppablecarl |
I gave it a deep thought. Maybe we should reconsider using the "lodash/merge" function. Let's just use Let me copy&paste the example from the link above: function customizer(objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
var object = {
'fruits': ['apple'],
'vegetables': ['beet']
};
var other = {
'fruits': ['banana'],
'vegetables': ['carrot']
};
_.mergeWith(object, other, customizer);
// → { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] } So, my proposal: do not add any new Descriptor Properties. But replace the "merge" with the "mergeWith" code above.
|
So you are proposing that we assume all arrays are intended to be concatenated when composing? I think that is just as bad as intending that they are replaced. Probably worse as I think you would more often want replacement. You need to have the option for both. This is why we have |
Nope. That's not what I meant. Let me get to a computer to show some code. On Wed, 17 Feb 2016 06:33 Carl Olsen notifications@github.com wrote:
|
let deep1 = compose({deepProperties: { key: ["a", "b", "c"] } });
let deep2 = compose({deepProperties: { key: [42, 42] } });
let composed = compose(deep1, deep2);
console.log(composed.compose.deepProperties); // { key: [ 42, 42, 'c' ] } In other words, array indexes are like POJO keys. let deep1 = compose({deepProperties: { key: {0: "a", 1: "b", 2: "c"} } });
let deep2 = compose({deepProperties: { key: {0: 42, 1: 42} } });
let composed = compose(deep1, deep2);
console.log(composed.compose.deepProperties); // { key: { '0': 42, '1': 42, '2': 'c' } } I find it useless. For the past 9 months since stampit v2 was released I have never ever used arrays inside the deepProperties just because of that.
Proposal: arrays inside the |
I was unaware of merging of objects in arrays in I would very much like to see this behavior removed from the spec. I think this merge behavior is much more specific and obscure than the array concatenation I am proposing.
As I have said before: I think the default behavior should be to replace and concatenate should be an option. I think concatenation would just be a different surprising behavior of I think for completeness the spec should include the following simple reasonably common low level object composition behaviors: Core
Instance
Static (same abilities as instance but merged into stamp instead of instance)
|
Use
It is not surprising to "underscore/lodash" users though. We are using
WAT? :) Then what's the difference? |
Recursive |
@unstoppablecarl could you elaborate on |
This is what I would expect merge to do with arrays by default. function merge(obj1, obj2){
return _.mergeWith(obj1, obj2, function(v1, v2){
if (_.isArray(v2)) {
return v2;
}
});
} I just discovered _.merge({foo: 'bar'}, ['a','b','c']); // {0: "a", 1: "b", 2: "c", foo: "bar"} I do not think I would ever want this result. |
Even stranger: _.merge({foo: {bar: 'bam'}}, {foo: ['a','b','c']}); // {foo: ['a','b','c']} So this should not affect stampit as it should never be merging arrays unless they are properties of an object. |
Asked the question here: lodash/lodash#2064 |
I like the idea of passing a customizer for specialized merge behavior, rather than bloating the spec further, but where is the customizer decided? In any stamp? What happens if different stamps propose different customizers? |
I think #69 is a better solution to this problem. |
Agreed. |
Concat Descriptor Properties Proposal
Add descriptor properties:
concatProperties
concatStaticProperties
(naming consistent with
deepProperties
anddeepStaticProperties
).Reasoning
The descriptor spec currently has many different properties on it for handling different types of composition. I think concatenating arrays when composing is a low level behavior equivalent in specificity of use case to
deepProperties
anddeepStaticProperties
. Array concatenation is the the only basic / low level way of composing I can think of that is absent in the descriptor spec.Spec Changes
Add descriptor properties:
concatProperties
concatStaticProperties
Example Code
Created Branch:
https://github.com/stampit-org/stamp-specification/tree/concat-properties
Implemented proposed changes: https://github.com/stampit-org/stamp-specification/blob/concat-properties/examples/compose.js
Tested Use Case:
The text was updated successfully, but these errors were encountered: