commit ee58c126fb29155360316905a17e0e742b7c2d83
parent c45685e4ea29aef9a89d28c1fcf595300a57a596
Author: Ben Eater <eater@khanacademy.org>
Date: Tue, 14 Jan 2014 17:37:16 -0800
Add an Array.indexOf polyfill and don't depend on underscore.js
Auditors: alpert
Diffstat:
2 files changed, 32 insertions(+), 11 deletions(-)
diff --git a/Parser.js b/Parser.js
@@ -363,7 +363,7 @@ Parser.prototype.parseNucleus = function(pos) {
if (group) {
return new ParseResult(
new ParseNode("sizing", {
- size: "size" + (_.indexOf(sizeFuncs, nucleus.type) + 1),
+ size: "size" + (sizeFuncs.indexOf(nucleus.type) + 1),
value: group.result
}),
group.position);
diff --git a/utils.js b/utils.js
@@ -1,17 +1,38 @@
-function fastContains(list, elem) {
- return list.indexOf(elem) !== -1;
-}
+// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
+if (!Array.prototype.indexOf) {
+ Array.prototype.indexOf = function (searchElement, fromIndex) {
+ if ( this === undefined || this === null ) {
+ throw new TypeError( '"this" is null or not defined' );
+ }
+
+ var length = this.length >>> 0; // Hack to convert object.length to a UInt32
+
+ fromIndex = +fromIndex || 0;
+
+ if (Math.abs(fromIndex) === Infinity) {
+ fromIndex = 0;
+ }
-function slowContains(list, elem) {
- for (var i = 0; i < list.length; i++) {
- if (list[i] === elem) {
- return true;
+ if (fromIndex < 0) {
+ fromIndex += length;
+ if (fromIndex < 0) {
+ fromIndex = 0;
+ }
}
- }
- return false;
+
+ for (;fromIndex < length; fromIndex++) {
+ if (this[fromIndex] === searchElement) {
+ return fromIndex;
+ }
+ }
+
+ return -1;
+ };
}
-var contains = Array.prototype.indexOf ? fastContains : slowContains;
+var contains = function(list, elem) {
+ return list.indexOf(elem) !== -1;
+};
var setTextContent;