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 array helper concat #331

Closed
wants to merge 4 commits into from
Closed

Add array helper concat #331

wants to merge 4 commits into from

Conversation

ericcornelissen
Copy link

As per #330, implemented a helper for arrays named concat which takes two arrays and concatenates them together. Suggestions are welcome 🙂

It seems something weird happened when I generated the docs, all forward slashes have been changed to backward slashes 😕 Maybe because I generated them on Windows?

jonschlinkert and others added 4 commits January 8, 2018 20:20
concat uses Array.prototype.concat to concatinate two arrays and return 
the result.

In spirit of the design of other helpers, I made it so that if only the 
first argument is an array, that array will be returned. If both 
arguments are an array their concatination is returned. If neither 
arguments are an array then an empty array is returned.
*/

helpers.concat = function(arrayA, arrayB) {
if (Array.isArray(arrayA) && !Array.isArray(arrayB)) {

Choose a reason for hiding this comment

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

Can I suggest an implementation that would handle more than one array? Something like:

    const args = Array.from(arguments);
    args.pop(); // Pop off options.
    return [].concat(...args)

Copy link
Author

Choose a reason for hiding this comment

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

That seems doable and a good idea. I'm not sure if functionality like that is desired by the maintainers though, so I will not implement it just yet 🙂

Just for reference, if the current implementation stays you could always do (concat array1 (concat array2 array3)). But of course this may become hard to read for a larger number of arrays.

@doowb
Copy link
Member

doowb commented Oct 3, 2018

Thanks for the PR. I'll try to review this by the weekend.

@ngouy
Copy link

ngouy commented Feb 3, 2023

(bump)

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.

5 participants