Open
Conversation
Throw TypeError if insert receives something other than an array
jgonggrijp
requested changes
Aug 28, 2020
Contributor
jgonggrijp
left a comment
There was a problem hiding this comment.
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'); |
Contributor
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.