Merge pull request #1571 from MichaelBlume/fix-ternary

shorten one of the ternaries
This commit is contained in:
Jeremy Ashkenas
2011-08-04 21:02:16 -07:00
2 changed files with 20 additions and 7 deletions

View File

@@ -817,7 +817,15 @@
varPart += ", " + this.step;
}
_ref2 = ["" + idx + " <" + this.equals, "" + idx + " >" + this.equals], lt = _ref2[0], gt = _ref2[1];
condPart = this.stepNum ? condPart = +this.stepNum > 0 ? "" + lt + " " + this.toVar : "" + gt + " " + this.toVar : known ? ((_ref3 = [+this.fromNum, +this.toNum], from = _ref3[0], to = _ref3[1], _ref3), condPart = from <= to ? "" + lt + " " + to : "" + gt + " " + to) : (cond = "" + this.fromVar + " <= " + this.toVar, condPart = "" + cond + " ? " + lt + " " + this.toVar + " : " + gt + " " + this.toVar);
if (this.stepNum) {
condPart = +this.stepNum > 0 ? "" + lt + " " + this.toVar : "" + gt + " " + this.toVar;
} else if (known) {
_ref3 = [+this.fromNum, +this.toNum], from = _ref3[0], to = _ref3[1];
condPart = from <= to ? "" + lt + " " + to : "" + gt + " " + to;
} else {
cond = "" + this.fromVar + " <= " + this.toVar;
condPart = "" + cond + " ? " + lt + " " + this.toVar + " : " + gt + " " + this.toVar;
}
stepPart = this.stepVar ? "" + idx + " += " + this.stepVar : known ? from <= to ? "" + idx + "++" : "" + idx + "--" : "" + cond + " ? " + idx + "++ : " + idx + "--";
return "" + varPart + "; " + condPart + "; " + stepPart;
};
@@ -1856,7 +1864,12 @@
Existence.prototype.compileNode = function(o) {
var cmp, cnj, code, _ref2;
code = this.expression.compile(o, LEVEL_OP);
code = IDENTIFIER.test(code) && !o.scope.check(code) ? ((_ref2 = this.negated ? ['===', '||'] : ['!==', '&&'], cmp = _ref2[0], cnj = _ref2[1], _ref2), "typeof " + code + " " + cmp + " \"undefined\" " + cnj + " " + code + " " + cmp + " null") : "" + code + " " + (this.negated ? '==' : '!=') + " null";
if (IDENTIFIER.test(code) && !o.scope.check(code)) {
_ref2 = this.negated ? ['===', '||'] : ['!==', '&&'], cmp = _ref2[0], cnj = _ref2[1];
code = "typeof " + code + " " + cmp + " \"undefined\" " + cnj + " " + code + " " + cmp + " null";
} else {
code = "" + code + " " + (this.negated ? '==' : '!=') + " null";
}
if (o.level <= LEVEL_COND) {
return code;
} else {

View File

@@ -659,7 +659,7 @@ exports.Range = class Range extends Base
[lt, gt] = ["#{idx} <#{@equals}", "#{idx} >#{@equals}"]
# Generate the condition.
condPart = if @stepNum
if @stepNum
condPart = if +@stepNum > 0 then "#{lt} #{@toVar}" else "#{gt} #{@toVar}"
else if known
[from, to] = [+@fromNum, +@toNum]
@@ -1451,12 +1451,12 @@ exports.Existence = class Existence extends Base
compileNode: (o) ->
code = @expression.compile o, LEVEL_OP
code = if IDENTIFIER.test(code) and not o.scope.check code
[cmp, cnj] = if @negated then ['===', '||'] else ['!==', '&&']
"typeof #{code} #{cmp} \"undefined\" #{cnj} #{code} #{cmp} null"
if IDENTIFIER.test(code) and not o.scope.check code
[cmp, cnj] = if @negated then ['===', '||'] else ['!==', '&&']
code = "typeof #{code} #{cmp} \"undefined\" #{cnj} #{code} #{cmp} null"
else
# do not use strict equality here; it will break existing code
"#{code} #{if @negated then '==' else '!='} null"
code = "#{code} #{if @negated then '==' else '!='} null"
if o.level <= LEVEL_COND then code else "(#{code})"
#### Parens