Use shorter zero checks for array indices and length.

Keeping === 0 for mathematical algorithms seems clearer.
This commit is contained in:
Jürg Lehni 2016-07-22 13:46:24 +02:00
parent 3d57216ffe
commit becac4c921
14 changed files with 30 additions and 29 deletions

View file

@ -83,7 +83,7 @@ var Matrix = Base.extend(/** @lends Matrix# */{
} else {
ok = false;
}
} else if (count === 0) {
} else if (!count) {
this.reset();
} else {
ok = false;

View file

@ -829,7 +829,7 @@ new function() { // Injection scope for various item event handlers
// If we're returning '#bounds', create a LinkedRectangle that uses
// the setBounds() setter to update the Item whenever the bounds are
// changed:
return arguments.length === 0
return !arguments.length
? new LinkedRectangle(bounds.x, bounds.y, bounds.width,
bounds.height, this, 'setBounds')
: bounds;
@ -878,7 +878,7 @@ new function() { // Injection scope for various item event handlers
var children = this._children;
// TODO: What to return if nothing is defined, e.g. empty Groups?
// Scriptographer behaves weirdly then too.
if (!children || children.length === 0)
if (!children || !children.length)
return new Rectangle();
// Call _updateBoundsCache() even when the group only holds empty /
// invisible items), so future changes in these items will cause right
@ -2667,7 +2667,8 @@ new function() { // Injection scope for hit-test functions shared with project
* @return Boolean
*/
isEmpty: function() {
return !this._children || this._children.length === 0;
var children = this._children;
return !children || !children.length;
},
/**
@ -4309,7 +4310,7 @@ new function() { // Injection scope for hit-test functions shared with project
// bounds corners, and draw the corners.
ctx.beginPath();
for (var i = 0; i < 8; i++) {
ctx[i === 0 ? 'moveTo' : 'lineTo'](coords[i], coords[++i]);
ctx[!i ? 'moveTo' : 'lineTo'](coords[i], coords[++i]);
}
ctx.closePath();
ctx.stroke();

View file

@ -135,7 +135,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
* @return Boolean
*/
isEmpty: function() {
return this._children.length === 0;
return !this._children.length;
},
/**

View file

@ -142,7 +142,7 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
if (path.isEmpty())
path.remove();
}
if (children.length === 0) { // Replace with a simple empty Path
if (!children.length) { // Replace with a simple empty Path
var path = new Path(Item.NO_INSERT);
path.copyAttributes(this);
path.insertAbove(this);
@ -301,7 +301,7 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
_draw: function(ctx, param, viewMatrix, strokeMatrix) {
var children = this._children;
// Return early if the compound path doesn't have any children:
if (children.length === 0)
if (!children.length)
return;
param = param.extend({ dontStart: true, dontFinish: true });
@ -342,7 +342,7 @@ new function() { // Injection scope for PostScript-like drawing functions
*/
function getCurrentPath(that, check) {
var children = that._children;
if (check && children.length === 0)
if (check && !children.length)
throw new Error('Use a moveTo() command first');
return children[children.length - 1];
}

View file

@ -71,7 +71,7 @@ var Curve = Base.extend(/** @lends Curve# */{
this._path = arg0;
seg1 = arg1;
seg2 = arg2;
} else if (count === 0) {
} else if (!count) {
seg1 = new Segment();
seg2 = new Segment();
} else if (count === 1) {
@ -299,7 +299,7 @@ var Curve = Base.extend(/** @lends Curve# */{
* @return {Boolean} {@true if this is the first curve}
*/
isFirst: function() {
return this._segment1._index === 0;
return !this._segment1._index;
},
/**
@ -651,7 +651,7 @@ statics: /** @lends Curve */{
tMax = 1 - tMin,
roots = [],
n = Numerical.solveQuadratic(a, b, c, roots, tMin, tMax);
if (n === 0) {
if (!n) {
curves.push(v);
} else {
roots.sort();
@ -2110,13 +2110,13 @@ new function() { // Scope for intersection using bezier fat-line clipping
if (t2 != null) { // If point is on curve
var pair = i === 0 ? [t1, t2] : [t2, t1];
// Filter out tiny overlaps.
if (pairs.length === 0 ||
if (!pairs.length ||
abs(pair[0] - pairs[0][0]) > timeEpsilon &&
abs(pair[1] - pairs[0][1]) > timeEpsilon)
pairs.push(pair);
}
// We checked 3 points but found no match, curves can't overlap.
if (i === 1 && pairs.length === 0)
if (i === 1 && !pairs.length)
break;
}
if (pairs.length !== 2) {

View file

@ -346,7 +346,7 @@ var Path = PathItem.extend(/** @lends Path# */{
outY = coords[5];
}
if (length === 0)
if (!length)
return '';
for (var i = 0; i < length; i++)
@ -364,7 +364,7 @@ var Path = PathItem.extend(/** @lends Path# */{
// taken into account.
isEmpty: function() {
return this._segments.length === 0;
return !this._segments.length;
},
_transformContent: function(matrix) {
@ -457,7 +457,7 @@ var Path = PathItem.extend(/** @lends Path# */{
}
// If it's the first segment, correct the last segment of closed
// paths too:
if (curve = curves[this._closed && start === 0 ? segments.length - 1
if (curve = curves[this._closed && !start ? segments.length - 1
: start - 1]) {
curve._segment2 = segments[start] || segments[0];
curve._changed();
@ -2303,7 +2303,7 @@ new function() { // PostScript-style drawing commands
*/
function getCurrentSegment(that) {
var segments = that._segments;
if (segments.length === 0)
if (!segments.length)
throw new Error('Use a moveTo() command first');
return segments[segments.length - 1];
}
@ -2522,7 +2522,7 @@ new function() { // PostScript-style drawing commands
pt = center.add(vector);
}
}
if (i === 0) {
if (!i) {
// Modify startSegment
current.setHandleOut(out);
} else {
@ -2864,7 +2864,7 @@ statics: {
segment._transformCoordinates(matrix, coords);
for (var j = 0; j < 6; j += 2) {
// Use different padding for points or handles
var padding = j === 0 ? joinPadding : strokePadding,
var padding = !j ? joinPadding : strokePadding,
paddingX = padding ? padding[0] : 0,
paddingY = padding ? padding[1] : 0,
x = coords[j],

View file

@ -482,7 +482,7 @@ PathItem.inject(new function() {
var curve = curves[i],
path = curve._path,
v = curve.getValues();
if (i === 0 || curves[i - 1]._path !== path) {
if (!i || curves[i - 1]._path !== path) {
// We're on a new (sub-)path, so we need to determine values of
// the last non-horizontal curve on this path.
vPrev = null;

View file

@ -169,7 +169,7 @@ var PathItem = Item.extend(/** @lends PathItem# */{
case 'l':
var move = lower === 'm';
for (var j = 0; j < length; j += 2)
this[j === 0 && move ? 'moveTo' : 'lineTo'](
this[!j && move ? 'moveTo' : 'lineTo'](
current = getPoint(j));
control = current;
if (move)

View file

@ -110,7 +110,7 @@ var PathIterator = Base.extend({
var i, j = this.index;
for (;;) {
i = j;
if (j === 0 || this.parts[--j].offset < offset)
if (!j || this.parts[--j].offset < offset)
break;
}
// Find the part that succeeds the given offset, then interpolate

View file

@ -496,7 +496,7 @@ var Segment = Base.extend(/** @lends Segment# */{
* @return {Boolean} {@true if this is the first segment}
*/
isFirst: function() {
return this._index === 0;
return !this._index;
},
/**

View file

@ -588,7 +588,7 @@ var Color = Base.extend(new function() {
// Allow implicit definition of gradients through
// stops / radial properties. Conversion happens
// here on the fly:
if (value == null && i === 0 && type === 'gradient'
if (value == null && !i && type === 'gradient'
&& 'stops' in arg) {
value = {
stops: arg.stops,

View file

@ -119,7 +119,7 @@ var Gradient = Base.extend(/** @lends Gradient# */{
var index = this._owners ? this._owners.indexOf(color) : -1;
if (index != -1) {
this._owners.splice(index, 1);
if (this._owners.length === 0)
if (!this._owners.length)
this._owners = undefined;
}
},

View file

@ -203,7 +203,7 @@ var Style = Base.extend(new function() {
// If the owner has children, walk through all of them and see if
// they all have the same style.
// If true is passed for _dontMerge, don't merge children styles
if (key in this._defaults && (!children || children.length === 0
if (key in this._defaults && (!children || !children.length
|| _dontMerge || owner instanceof CompoundPath)) {
var value = this._values[key];
if (value === undefined) {
@ -224,7 +224,7 @@ var Style = Base.extend(new function() {
} else if (children) {
for (var i = 0, l = children.length; i < l; i++) {
var childValue = children[i]._style[get]();
if (i === 0) {
if (!i) {
value = childValue;
} else if (!Base.equals(value, childValue)) {
// If there is another child with a different

View file

@ -23,7 +23,7 @@ test('moveTo() / lineTo()', function() {
for (var i = 0; i < lists.length; i++) {
var list = lists[i];
for (var j = 0; j < list.length; j++) {
path[j === 0 ? 'moveTo' : 'lineTo'](list[j]);
path[!j ? 'moveTo' : 'lineTo'](list[j]);
}
}