Skip to content

Instantly share code, notes, and snippets.

@jimmed
Last active August 29, 2015 14:01
Show Gist options
  • Save jimmed/075adc9fd9fb5514fc7d to your computer and use it in GitHub Desktop.
Save jimmed/075adc9fd9fb5514fc7d to your computer and use it in GitHub Desktop.
Array::indexOfArr
Array.prototype.indexOfArr = function(search) {
var found = -1;
this.some(function(v, x) {
if(search.length > this.length - x) {
return true;
}
found = x;
this.some.call(search, function(sv, y) {
if(sv !== this[x+y]) {
found = -1;
return true;
}
}, this);
return found !== -1
}, this);
return found
};
Array::indexOfArr = (search) ->
found = -1
@some (v, x) ~>
return true if search.length > this.length - x
found := x
@some.call search, (sv, y) ~>
if sv isnt @[x+y]
found := -1
yes
found isnt -1
found
[1,2,3,4,5,6,7,8,9,10].indexOfArr([3,4,5,6]) // => 2
[1,2,3,4,5,6,7,8,9,10].indexOfArr([3,4,6]) // => -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment