Skip to content
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

Unique initializers #89

Merged
merged 4 commits into from
May 21, 2016
Merged

Unique initializers #89

merged 4 commits into from
May 21, 2016

Conversation

koresar
Copy link
Member

@koresar koresar commented May 21, 2016

No description provided.

@@ -89,7 +89,11 @@ function mergeComposable(dstDescriptor, srcComposable) {
combineProperty('deepConfiguration', merge);
if (Array.isArray(srcDescriptor.initializers)) {
if (!Array.isArray(dstDescriptor.initializers)) dstDescriptor.initializers = [];
dstDescriptor.initializers.push.apply(dstDescriptor.initializers, srcDescriptor.initializers.filter(isFunction));
srcDescriptor.initializers.forEach(i => {
Copy link
Contributor

@danielkcz danielkcz May 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be using reduce better? Something like this without side effects and in my opinion somewhat easier to reason about.

dstDescriptor.initializers = srcDescriptor.initializers.reduce((result, init) => {
     if (isFunction(init) && !result.includes(init)) {
         result.push(init);
     }
     return result;
}, Array.isArray(dstDescriptor.initializers) ? dstDescriptor.initializers : []);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree reduce is always better. This file is designed to be as readable as possible. I find reduce less readable than the forEach in this case.

Copy link
Contributor

@danielkcz danielkcz May 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I guess it's very subjective, I even think that the line with if (!Array.isArray(dstDescriptor.initializers)) is very hard to spot in there (especially without brackets) and it's almost unclear at a first glance why it's there. With reduce you clearly see how it's used and handled. But I am not going to fight you on this, if others like your way better too, it's fine.

Copy link
Member Author

@koresar koresar May 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. The thing is too subjective. :)

Hey, do you want a commit access to the stampit-org? You'd be able to review and merge code. :)

UPD: I really need someone to merge this, and I trust your judged opinion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well if it were up to me to merge I would not do it, because of my opinion above :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Let me change the code to the reduce implementation so that you can merge it. :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Done. See the latest commit.
Also, the invitation was sent. Feel free to accept and merge the PR (use the "squash and merge" github button pls).

Upgrade to the latest version of check-compose.
srcDescriptor.initializers.forEach(i => {
if (isFunction(i) && dstDescriptor.initializers.indexOf(i) < 0) {
dstDescriptor.initializers.push(i);
dstDescriptor.initializers = srcDescriptor.initializers.reduce((result, init) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I hope you have tested it little, because I did not, I just wrote it from top of my head.

Copy link
Member Author

@koresar koresar May 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did test it. We have the special package check-compose which runs unit tests against a "compose()" implementation. I have just updated the check-compose up to version two, - added two more tests to check that initializers do not duplicate.

@danielkcz
Copy link
Contributor

Ugh, there are actually some tests? Where are those defined?

@danielkcz
Copy link
Contributor

Ah I see, well sorry about includes, I kinda expected it's available since I am using it all over my projects :)

@koresar
Copy link
Member Author

koresar commented May 21, 2016

Totally my bug. I should have checked it agains node 0.10 myself :)

@danielkcz
Copy link
Contributor

Hm, Node 0.10 is so old, if it would be up to me, I would go from 0.12 above :)

@koresar
Copy link
Member Author

koresar commented May 21, 2016

Node 0.10 is too wide spread yet. We should deal with with.
I've just pushed the fix. Should be good to merge.

@danielkcz danielkcz merged commit fce7b67 into master May 21, 2016
@danielkcz danielkcz deleted the uniq-init branch May 21, 2016 12:20
if (!Array.isArray(dstDescriptor.initializers)) dstDescriptor.initializers = [];
dstDescriptor.initializers.push.apply(dstDescriptor.initializers, srcDescriptor.initializers.filter(isFunction));
dstDescriptor.initializers = srcDescriptor.initializers.reduce((result, init) => {
if (isFunction(init) && result.indexOf(init) < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why check < 0 instead of === -1? Just curious

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using -1 always felt weird to be as some arbitrary number. You can even reason better about it like this: "index is below 0" instead of "index is equal this weird number".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@unstoppablecarl shorter :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. My instinct is to use === to be strict.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants