From 0f0c91b1142340f8ace8ea4f909b4576765eb6ab Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 24 Oct 2015 22:07:09 -0700 Subject: [PATCH] Clean up some of the `var`s and loops. There's zero reason in 2015 to use a loop, or "var" - this cleans some of them up. There's still a bunch left, but I figured I'd tackle just a little bit at a time. --- RegExp.make.js | 48 +++++++++++++----------------------------------- 1 file changed, 13 insertions(+), 35 deletions(-) diff --git a/RegExp.make.js b/RegExp.make.js index 4bf0f37..a6a291e 100644 --- a/RegExp.make.js +++ b/RegExp.make.js @@ -37,7 +37,8 @@ RegExp.make = (function () { if (0x20 <= n && n <= 0x7e) { return String.fromCharCode(n).replace(UNSAFE_CHARS_CHARSET, '\\$&'); } - var hex = n.toString(16); + /** @type {string} */ + const hex = n.toString(16); return '\\u0000'.substring(0, 6 - hex.length) + hex; } @@ -87,14 +88,7 @@ RegExp.make = (function () { * @this {!CharRanges} */ CharRanges.prototype.toString = function () { - var s = ''; - /** @type {!Array.}. */ - const ranges = this.ranges; - /** @type {number} */ - const n = ranges.length; - for (var i = 0; i < n; ++i) { - /** @type {number} */ - const leftAndSpan = ranges[i]; + return this.ranges.reduce(function (s, leftAndSpan) { const left = leftAndSpan >> 16; const span = leftAndSpan & 0xffff; s += encodeRangeEndPoint(left); @@ -102,8 +96,8 @@ RegExp.make = (function () { if (span !== 1) { s += '-'; } s += encodeRangeEndPoint(left + span); } - } - return s; + return s; + }, ''); }; /** * The minimum code-point matched or NaN. @@ -125,9 +119,8 @@ RegExp.make = (function () { * @return {!CharRanges} this to allow chaining. */ CharRanges.prototype.addRange = function (left, opt_right) { - var right = opt_right || left; + const right = +(opt_right || left); left = +left; - right = +right; if ('number' !== typeof left || left < 0 || right > MAX_CHAR_IN_RANGE || left > right || left % 1 || right % 1) { @@ -160,12 +153,9 @@ RegExp.make = (function () { /** @type {!Array.} */ const ranges = this.ranges; /** @type {number} */ - const n = ranges.length; - var pastLastRight = 0; + let pastLastRight = 0; const invertedRanges = []; - for (var i = 0; i < n; ++i) { - /** @type {number} */ - const leftAndSpan = ranges[i]; + ranges.forEach(function (leftAndSpan) { const left = leftAndSpan >> 16; const span = leftAndSpan & 0xFFFF; if (pastLastRight < left) { @@ -175,7 +165,7 @@ RegExp.make = (function () { ); } pastLastRight = left + span + 1; - } + }); if (pastLastRight <= MAX_CHAR_IN_RANGE) { invertedRanges.push( (pastLastRight << 16) @@ -229,14 +219,8 @@ RegExp.make = (function () { * @return {!CharRanges} a newly allocated output. Not modified in place. */ CharRanges.prototype.intersectionWithRange = function (min, max) { - /** @type {!Array.} */ - const ranges = this.ranges; const intersection = new CharRanges(); - /** @type {number} */ - const n = ranges.length; - for (var i = 0; i < n; ++i) { - /** @type {number} */ - const leftAndSpan = ranges[i]; + this.ranges.forEach(function (leftAndSpan) { const left = leftAndSpan >> 16; const span = leftAndSpan & 0xFFFF; /** @type {number} */ @@ -245,7 +229,7 @@ RegExp.make = (function () { if (!(left > max || right < min)) { intersection.addRange(Math.max(min, left), Math.min(max, right)); } - } + }); return intersection; }; /** @@ -265,19 +249,13 @@ RegExp.make = (function () { * @this {!CharRanges} */ CharRanges.prototype.forEachRange = function (callback) { - /** @type {!Array.} */ - const ranges = this.ranges; - /** @type {number} */ - const n = ranges.length; - for (var i = 0; i < n; ++i) { - /** @type {number} */ - const leftAndSpan = ranges[i]; + this.ranges.forEach(function (leftAndSpan) { const left = leftAndSpan >> 16; const span = leftAndSpan & 0xFFFF; /** @type {number} */ const right = left + span; callback(left, right); - } + }); }; CharRanges.prototype.clear = function () { this.ranges.length = 0;