From 9f829307841c4a402c8feb8c9bddbf67a006fc53 Mon Sep 17 00:00:00 2001 From: Kit Cambridge Date: Sat, 25 Feb 2017 17:02:14 -0800 Subject: [PATCH] Make `for` loops more readable. Closes #88. Closes #83. --- lib/json3.js | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/json3.js b/lib/json3.js index 2a066a8..2f73ac3 100644 --- a/lib/json3.js +++ b/lib/json3.js @@ -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. @@ -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. @@ -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) { @@ -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); @@ -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(); @@ -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(); @@ -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);