Rethink our indexOf implementation
This commit is contained in:
parent
99343e8fc0
commit
63f6f5f3c2
@ -111,15 +111,17 @@
|
||||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
||||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
|
||||
Array.prototype.indexOf = function(val) {
|
||||
var i;
|
||||
i = this.length;
|
||||
while (i--) {
|
||||
Array.prototype.indexOf = function(val, i) {
|
||||
var len;
|
||||
i || (i = 0);
|
||||
len = this.length;
|
||||
while (i < len) {
|
||||
if (this[i] === val) {
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
return -1;
|
||||
};
|
||||
|
||||
__indexOf = [].indexOf;
|
||||
|
||||
@ -89,15 +89,17 @@
|
||||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
||||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
|
||||
Array.prototype.indexOf = function(val) {
|
||||
var i;
|
||||
i = this.length;
|
||||
while (i--) {
|
||||
Array.prototype.indexOf = function(val, i) {
|
||||
var len;
|
||||
i || (i = 0);
|
||||
len = this.length;
|
||||
while (i < len) {
|
||||
if (this[i] === val) {
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
return -1;
|
||||
};
|
||||
|
||||
__indexOf = [].indexOf;
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
# I am bad at JavaScript and if you reuse this, so are you.
|
||||
Array::indexOf = (val) ->
|
||||
i = @length
|
||||
while i--
|
||||
Array::indexOf = (val, i) ->
|
||||
i or= 0
|
||||
len = @length
|
||||
while i < len
|
||||
return i if @[i] is val
|
||||
return i
|
||||
i++
|
||||
return -1
|
||||
|
||||
# Update CoffeeScript's reference to [].indexOf
|
||||
# Reserved keywords are ignored in embedded javascript.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user