Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Commit

Permalink
Make for loops more readable. Closes #88. Closes #83.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kit Cambridge committed Feb 26, 2017
1 parent b6256f0 commit 9f82930
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions lib/json3.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,11 @@
}
}
// Manually invoke the callback for each non-enumerable property.
for (length = members.length; property = members[--length]; hasProperty.call(object, property) && callback(property));
for (length = dontEnums.length; property = dontEnums[--length];) {
if (hasProperty.call(object, property)) {
callback(property);
}
}
};
} else {
// No bugs detected; use the standard `for...in` algorithm.
Expand Down Expand Up @@ -382,7 +386,7 @@
};

// For environments with `JSON.stringify` but buggy date serialization,
// we override the native `Date#toJSON` implementation with a
// we override the native `Date#toJSON` implementation with a
// spec-compliant one.
if (has("json-stringify") && !has("date-serialization")) {
// Internal: the `Date#toJSON` implementation used to override the native one.
Expand Down Expand Up @@ -525,7 +529,13 @@
} else if (className == arrayClass) {
// Convert the property names array into a makeshift set.
properties = {};
for (var index = 0, length = filter.length, value; index < length; value = filter[index++], ((className = getClass.call(value)), className == stringClass || className == numberClass) && (properties[value] = 1));
for (var index = 0, length = filter.length, value; index < length;) {
value = filter[index++];
className = getClass.call(value);
if (className == "[object String]" || className == "[object Number]") {
properties[value] = 1;
}
}
}
}
if (width) {
Expand All @@ -534,7 +544,12 @@
// Convert the `width` to an integer and create a string containing
// `width` number of space characters.
if ((width -= width % 1) > 0) {
for (whitespace = "", width > 10 && (width = 10); whitespace.length < width; whitespace += " ");
if (width > 10) {
width = 10;
}
for (whitespace = ""; whitespace.length < width;) {
whitespace += " ";
}
}
} else if (className == stringClass) {
whitespace = width.length <= 10 ? width : width.slice(0, 10);
Expand Down Expand Up @@ -682,7 +697,12 @@
if (source.charCodeAt(Index) == 46) {
position = ++Index;
// Parse the decimal component.
for (; position < length && ((charCode = source.charCodeAt(position)), charCode >= 48 && charCode <= 57); position++);
for (; position < length; position++) {
charCode = source.charCodeAt(position);
if (charCode < 48 || charCode > 57) {
break;
}
}
if (position == Index) {
// Illegal trailing decimal.
abort();
Expand All @@ -700,7 +720,12 @@
Index++;
}
// Parse the exponential component.
for (position = Index; position < length && ((charCode = source.charCodeAt(position)), charCode >= 48 && charCode <= 57); position++);
for (position = Index; position < length; position++) {
charCode = source.charCodeAt(position);
if (charCode < 48 || charCode > 57) {
break;
}
}
if (position == Index) {
// Illegal empty exponent.
abort();
Expand Down Expand Up @@ -842,7 +867,9 @@
// because its `Object#hasOwnProperty` implementation returns `false`
// for array indices (e.g., `![1, 2, 3].hasOwnProperty("0")`).
if (getClass.call(value) == arrayClass) {
for (length = value.length; length--; update(value, length, callback));
for (length = value.length; length--;) {
update(getClass, forOwn, value, length, callback);
}
} else {
forOwn(value, function (property) {
update(value, property, callback);
Expand Down

0 comments on commit 9f82930

Please sign in to comment.