From becac4c921e9fe10e8d669209a17d1f72c94c425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Fri, 22 Jul 2016 13:46:24 +0200 Subject: [PATCH] Use shorter zero checks for array indices and length. Keeping === 0 for mathematical algorithms seems clearer. --- src/basic/Matrix.js | 2 +- src/item/Item.js | 9 +++++---- src/item/Project.js | 2 +- src/path/CompoundPath.js | 6 +++--- src/path/Curve.js | 10 +++++----- src/path/Path.js | 12 ++++++------ src/path/PathItem.Boolean.js | 2 +- src/path/PathItem.js | 2 +- src/path/PathIterator.js | 2 +- src/path/Segment.js | 2 +- src/style/Color.js | 2 +- src/style/Gradient.js | 2 +- src/style/Style.js | 4 ++-- test/tests/CompoundPath.js | 2 +- 14 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/basic/Matrix.js b/src/basic/Matrix.js index 7506670d..c6d9d899 100644 --- a/src/basic/Matrix.js +++ b/src/basic/Matrix.js @@ -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; diff --git a/src/item/Item.js b/src/item/Item.js index 761489cd..6e45afe7 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -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(); diff --git a/src/item/Project.js b/src/item/Project.js index 70dbf97d..d4dd4e45 100644 --- a/src/item/Project.js +++ b/src/item/Project.js @@ -135,7 +135,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{ * @return Boolean */ isEmpty: function() { - return this._children.length === 0; + return !this._children.length; }, /** diff --git a/src/path/CompoundPath.js b/src/path/CompoundPath.js index 89061102..14868b8a 100644 --- a/src/path/CompoundPath.js +++ b/src/path/CompoundPath.js @@ -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]; } diff --git a/src/path/Curve.js b/src/path/Curve.js index 0f65b3a7..0e68157c 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -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) { diff --git a/src/path/Path.js b/src/path/Path.js index 9039cdca..8b36e096 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -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], diff --git a/src/path/PathItem.Boolean.js b/src/path/PathItem.Boolean.js index e9c05c5a..924172aa 100644 --- a/src/path/PathItem.Boolean.js +++ b/src/path/PathItem.Boolean.js @@ -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; diff --git a/src/path/PathItem.js b/src/path/PathItem.js index 4be526a2..394456d2 100644 --- a/src/path/PathItem.js +++ b/src/path/PathItem.js @@ -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) diff --git a/src/path/PathIterator.js b/src/path/PathIterator.js index c6e72e18..f30bed2b 100644 --- a/src/path/PathIterator.js +++ b/src/path/PathIterator.js @@ -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 diff --git a/src/path/Segment.js b/src/path/Segment.js index ff52e89d..d9d863d5 100644 --- a/src/path/Segment.js +++ b/src/path/Segment.js @@ -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; }, /** diff --git a/src/style/Color.js b/src/style/Color.js index bcf53dda..2205fa4b 100644 --- a/src/style/Color.js +++ b/src/style/Color.js @@ -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, diff --git a/src/style/Gradient.js b/src/style/Gradient.js index c88f0410..b829edcc 100644 --- a/src/style/Gradient.js +++ b/src/style/Gradient.js @@ -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; } }, diff --git a/src/style/Style.js b/src/style/Style.js index 2138e964..76e60492 100644 --- a/src/style/Style.js +++ b/src/style/Style.js @@ -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 diff --git a/test/tests/CompoundPath.js b/test/tests/CompoundPath.js index 31c19364..adc0224d 100644 --- a/test/tests/CompoundPath.js +++ b/test/tests/CompoundPath.js @@ -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]); } }