commit 99e7710020b27bc657ef67209f55bc9bdcab9350
parent ee58c126fb29155360316905a17e0e742b7c2d83
Author: Ben Alpert <spicyjalapeno@gmail.com>
Date: Tue, 14 Jan 2014 19:52:08 -0800
Don't change global prototype: local utils.indexOf
Auditors: eater
Diffstat:
2 files changed, 18 insertions(+), 32 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" + (sizeFuncs.indexOf(nucleus.type) + 1),
+ size: "size" + (utils.indexOf(sizeFuncs, nucleus.type) + 1),
value: group.result
}),
group.position);
diff --git a/utils.js b/utils.js
@@ -1,37 +1,22 @@
-// 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;
- }
-
- if (fromIndex < 0) {
- fromIndex += length;
- if (fromIndex < 0) {
- fromIndex = 0;
- }
- }
-
- for (;fromIndex < length; fromIndex++) {
- if (this[fromIndex] === searchElement) {
- return fromIndex;
- }
- }
-
+var nativeIndexOf = Array.prototype.indexOf;
+var indexOf = function(list, elem) {
+ if (list == null) {
return -1;
- };
-}
+ }
+ if (nativeIndexOf && list.indexOf === nativeIndexOf) {
+ return list.indexOf(elem);
+ }
+ var i = 0, l = list.length;
+ for (; i < l; i++) {
+ if (list[i] === elem) {
+ return i;
+ }
+ }
+ return -1;
+};
var contains = function(list, elem) {
- return list.indexOf(elem) !== -1;
+ return indexOf(list, elem) !== -1;
};
var setTextContent;
@@ -53,6 +38,7 @@ function clearNode(node) {
module.exports = {
contains: contains,
+ indexOf: indexOf,
setTextContent: setTextContent,
clearNode: clearNode
};