From 63f6f5f3c2c521e36ddbd40f372236eb86395706 Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Mon, 13 Jan 2014 12:08:53 -0700 Subject: [PATCH] Rethink our indexOf implementation --- builds/4chan-X.user.js | 12 +++++++----- builds/crx/script.js | 12 +++++++----- src/General/Cheats.coffee | 10 ++++++---- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index de13bb7ed..149639b03 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -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; diff --git a/builds/crx/script.js b/builds/crx/script.js index 423598008..8df8743a7 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -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; diff --git a/src/General/Cheats.coffee b/src/General/Cheats.coffee index 6b2926359..d70bd88bd 100644 --- a/src/General/Cheats.coffee +++ b/src/General/Cheats.coffee @@ -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.