mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 11:31:20 -05:00
Added the __inArray helper to clean up code and speed up searches.
This commit is contained in:
24
lib/nodes.js
24
lib/nodes.js
@@ -1478,32 +1478,25 @@
|
||||
return this.array instanceof Value && this.array.isArray();
|
||||
};
|
||||
In.prototype.compileNode = function(o) {
|
||||
var _ref2;
|
||||
_ref2 = this.object.compileReference(o, {
|
||||
precompile: true
|
||||
}), this.obj1 = _ref2[0], this.obj2 = _ref2[1];
|
||||
return this.isArray() ? this.compileOrTest(o) : this.compileLoopTest(o);
|
||||
};
|
||||
In.prototype.compileOrTest = function(o) {
|
||||
var _len, _ref2, _result, i, item, tests;
|
||||
var _len, _ref2, _ref3, _result, i, item, obj1, obj2, tests;
|
||||
_ref2 = this.object.compileReference(o, {
|
||||
precompile: true
|
||||
}), obj1 = _ref2[0], obj2 = _ref2[1];
|
||||
tests = (function() {
|
||||
_result = [];
|
||||
for (i = 0, _len = (_ref2 = this.array.base.objects).length; i < _len; i++) {
|
||||
item = _ref2[i];
|
||||
_result.push("" + (i ? this.obj2 : this.obj1) + " === " + (item.compile(o)));
|
||||
for (i = 0, _len = (_ref3 = this.array.base.objects).length; i < _len; i++) {
|
||||
item = _ref3[i];
|
||||
_result.push("" + (i ? obj2 : obj1) + " === " + (item.compile(o)));
|
||||
}
|
||||
return _result;
|
||||
}).call(this);
|
||||
return "(" + (tests.join(' || ')) + ")";
|
||||
};
|
||||
In.prototype.compileLoopTest = function(o) {
|
||||
var _ref2, _ref3, i, l, prefix;
|
||||
_ref2 = this.array.compileReference(o, {
|
||||
precompile: true
|
||||
}), this.arr1 = _ref2[0], this.arr2 = _ref2[1];
|
||||
_ref3 = [o.scope.freeVariable('i'), o.scope.freeVariable('len')], i = _ref3[0], l = _ref3[1];
|
||||
prefix = this.obj1 !== this.obj2 ? this.obj1 + '; ' : '';
|
||||
return "(function(){ " + prefix + "for (var " + i + "=0, " + l + "=" + (this.arr1) + ".length; " + i + "<" + l + "; " + i + "++) { if (" + (this.arr2) + "[" + i + "] === " + (this.obj2) + ") return true; } return false; }).call(this)";
|
||||
return "" + (utility('inArray')) + "(" + (this.array.compile(o)) + ", " + (this.object.compile(o)) + ")";
|
||||
};
|
||||
return In;
|
||||
})();
|
||||
@@ -1964,6 +1957,7 @@
|
||||
UTILITIES = {
|
||||
"extends": 'function(child, parent) {\n function ctor() { this.constructor = child; }\n ctor.prototype = parent.prototype;\n child.prototype = new ctor;\n if (typeof parent.extended === "function") parent.extended(child);\n child.__super__ = parent.prototype;\n}',
|
||||
bind: 'function(func, context) {\n return function() { return func.apply(context, arguments); };\n}',
|
||||
inArray: '(function() {\n var indexOf = Array.prototype.indexOf || function (item) {\n for (var i = this.length; i--;) if (this[i] === item) return i;\n return -1;\n }; return function(array, item) { return indexOf.call(array, item) > -1; };\n})();',
|
||||
hasProp: 'Object.prototype.hasOwnProperty',
|
||||
slice: 'Array.prototype.slice'
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user