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

Add concat() method #4

Closed
wants to merge 6 commits into from
Closed

Conversation

mikebronner
Copy link

New method from a recent Laravel 5.4 patch. I'm not familiar with testing in Javascript, so I have put in some initial work to what I think it should be. Would you mind helping me a bit on this PR to flesh out the testing and make sure the implementation is working as expected?

Looking forward to your feedback :)

Thanks for creating this package! I have been thinking it would be great to have a package like this for a few years. Good to see someone better than me with Javascript took the initiative. :)

@ecrmnn
Copy link
Owner

ecrmnn commented Jun 18, 2017

Thanks for the PR @mikebronner.

No worries, I'll help you out 😄

Do you have a link to the concat implementation?
I couldn't find it in the docs.

@mikebronner
Copy link
Author

Absolutely: laravel/framework#19318

src/index.js Outdated
collection = collection.all();
}

for (let iterator = 1; iterator <= collection.count(); iterator++) {
Copy link
Owner

Choose a reason for hiding this comment

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

Use forEach instead of a loop

Copy link
Author

Choose a reason for hiding this comment

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

The problem with forEach in Javascript is that order can be messed up, right? I think keeping order is important.

https://stackoverflow.com/questions/13600922/does-javascript-array-foreach-traverse-elements-in-ascending-order

src/index.js Outdated
}

for (let iterator = 1; iterator <= collection.count(); iterator++) {
this.items.push(collection[iterator]);
Copy link
Owner

Choose a reason for hiding this comment

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

Needs logic for when item is of different type

  • Array
  • Object
  • String
  • etc.

@ecrmnn
Copy link
Owner

ecrmnn commented Jun 18, 2017

This is a port of your testConcatWithArray from the Laravel PR.
I've not tested it. You should implement this, the testConcatWithCollection and a hybrid test.

Also; what happens if the collection passed to the concat method contains several arrays?

const expected = [
  4,
  5,
  6,
  'a',
  'b',
  'c',
  'Jonny',
  'from',
  'Laroe',
  'Jonny',
  'from',
  'Laroe'
];

const firstCollection = collect([4, 5, 6]);
const secondCollection = collect(['a', 'b', 'c']);
const thirdCollection = collect([{
  who: 'Jonny'
}, {
  preposition: 'from'
}, {
  where: 'Laroe'
}]);

firstCollection
  .concat(secondCollection)
  .concat(thirdCollection)
  .concat(thirdCollection);

expect(firstCollection.count()).to.eql(12);
expect(firstCollection.all()).to.eql(expected);

@mikebronner
Copy link
Author

Sounds good, I'll work on the port test. That test is indeed already a hybrid test, is it no? Or did you mean something else?

@ecrmnn
Copy link
Owner

ecrmnn commented Jun 18, 2017

Yes it is. Sorry my bad.

@mikebronner
Copy link
Author

mikebronner commented Jun 18, 2017

@ecrmnn OK, made the requested updates. Let me know what you think. I updated the code to ignore anything that is not iterable. Laravel with throw an exception, and of course the developer will catch it. Was thinking the same would apply here. This is essentially "silently failing" now, which could be considered poor practice and the developer wouldn't be aware of the problem. Any suggestions?

Also, not sure how to resolve the conflicts. I added in the missing code, but not sure what github expects in addition.

Thanks :)

@ecrmnn
Copy link
Owner

ecrmnn commented Jun 19, 2017

The tests are failing for me? Am I missing something?
Please make the tests pass. I'll have a look later 😄

@mikebronner
Copy link
Author

@ecrmnn I don't know how to run the tests locally, I'm unfamiliar with JS testing. :(

@mikeerickson
Copy link

@mikebronner Sounds like a great opportunity to learn :-)

@mikebronner
Copy link
Author

@mikeerickson Perhaps you would like to help out on this one? I don't have the bandwidth to figure it out at the moment without some guidance on what needs to be set up. A list of commands for me to run would suffice.

@mikeerickson
Copy link

@mikebronner Most definitely, I am going to create a UMD testing workflow for the project, as well as take on the task of separating files (and tests) into separate source files.

When all done, it will be a great source for you to learn from.

@faalotaiby
Copy link

If you consider the main argument that you want to add the method concat() for, it will help the method `concat() returns a copy as a shallow that contains different copies of the elements included combined from the original arrays.

@ecrmnn ecrmnn closed this in 960a3d6 Jul 1, 2017
@mikebronner mikebronner deleted the patch-1 branch July 1, 2017 15:51
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.

4 participants