-
Notifications
You must be signed in to change notification settings - Fork 117
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
base: master
Are you sure you want to change the base?
Conversation
Throw TypeError if insert receives something other than an array
There was a problem hiding this 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'); |
There was a problem hiding this comment.
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.
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.