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

Insert function for arrays #199

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

acarrau
Copy link

@acarrau acarrau commented Jul 12, 2015

I thought an 'insert item at' function for arrays would be nice to have, avoiding the use of splice where 0 needs to be passed as the 2nd 'deleteCount' parameter.

It works the same way the _.extend function does, where the first parameter gets modified and the result is also returned.

@jgonggrijp jgonggrijp added before modules This needs to be tackled before modularization (temporary label, see #220) enhancement labels Aug 3, 2020
Copy link
Contributor

@jgonggrijp jgonggrijp left a comment

Choose a reason for hiding this comment

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

Thank you @acarrau for your submission and sorry for the late reply. I think this is a useful function to have. I would remove the tight restriction on the first argument having to be a proper array, but otherwise I think the implementation and the tests are sound. Documentation still needs to be added.

Would you like to finish the PR yourself? Otherwise, I or somebody else can take it from here. You will get the credits for the work you already did in any case.

// Inserts an item in an array at the specific index mutating the original
// array and returning it.
insert: function(array, index, item){
if (!_.isArray(array)) throw new TypeError('Expected an array as the first argument');
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this check is too restrictive, because splice also works with arguments and similar array-like objects. I think you can just remove this line.

jgonggrijp added a commit to jgonggrijp/underscore-contrib that referenced this pull request Dec 18, 2020
jgonggrijp added a commit to jgonggrijp/underscore-contrib that referenced this pull request Dec 18, 2020
jgonggrijp added a commit to jgonggrijp/underscore-contrib that referenced this pull request Dec 18, 2020
jgonggrijp added a commit to jgonggrijp/underscore-contrib that referenced this pull request Dec 18, 2020
jgonggrijp added a commit to jgonggrijp/underscore-contrib that referenced this pull request Dec 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
before modules This needs to be tackled before modularization (temporary label, see #220) enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants