Skip to content

Commit

Permalink
Clean up some of the vars and loops.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ljharb committed Oct 25, 2015
1 parent a4d5e4e commit 0f0c91b
Showing 1 changed file with 13 additions and 35 deletions.
48 changes: 13 additions & 35 deletions RegExp.make.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -87,23 +88,16 @@ RegExp.make = (function () {
* @this {!CharRanges}
*/
CharRanges.prototype.toString = function () {
var s = '';
/** @type {!Array.<number>}. */
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);
if (span) {
if (span !== 1) { s += '-'; }
s += encodeRangeEndPoint(left + span);
}
}
return s;
return s;
}, '');
};
/**
* The minimum code-point matched or NaN.
Expand All @@ -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) {
Expand Down Expand Up @@ -160,12 +153,9 @@ RegExp.make = (function () {
/** @type {!Array.<number>} */
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) {
Expand All @@ -175,7 +165,7 @@ RegExp.make = (function () {
);
}
pastLastRight = left + span + 1;
}
});
if (pastLastRight <= MAX_CHAR_IN_RANGE) {
invertedRanges.push(
(pastLastRight << 16)
Expand Down Expand Up @@ -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.<number>} */
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} */
Expand All @@ -245,7 +229,7 @@ RegExp.make = (function () {
if (!(left > max || right < min)) {
intersection.addRange(Math.max(min, left), Math.min(max, right));
}
}
});
return intersection;
};
/**
Expand All @@ -265,19 +249,13 @@ RegExp.make = (function () {
* @this {!CharRanges}
*/
CharRanges.prototype.forEachRange = function (callback) {
/** @type {!Array.<number>} */
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;
Expand Down

0 comments on commit 0f0c91b

Please sign in to comment.