json-stringify-pretty-compact
Advanced tools
Comparing version 1.0.0 to 1.0.1
16
index.js
@@ -9,3 +9,3 @@ // Copyright 2014 Simon Lydell | ||
return (function _stringify(obj, currentIndent, prev) { | ||
return (function _stringify(obj, currentIndent, reserved) { | ||
if (obj && typeof obj.toJSON === "function") { | ||
@@ -21,3 +21,3 @@ obj = obj.toJSON() | ||
var length = maxLength - currentIndent.length - prev | ||
var length = maxLength - currentIndent.length - reserved | ||
@@ -35,12 +35,18 @@ if (string.length <= length) { | ||
var delimiters | ||
var comma = function(array, index) { | ||
return (index === array.length - 1 ? 0 : 1) | ||
} | ||
if (Array.isArray(obj)) { | ||
for (var index = 0; index < obj.length; index++) { | ||
items.push(_stringify(obj[index], nextIndent, 0) || "null") | ||
items.push( | ||
_stringify(obj[index], nextIndent, comma(obj, index)) || "null" | ||
) | ||
} | ||
delimiters = "[]" | ||
} else { | ||
Object.keys(obj).forEach(function(key) { | ||
Object.keys(obj).forEach(function(key, index, array) { | ||
var keyPart = JSON.stringify(key) + ": " | ||
var value = _stringify(obj[key], nextIndent, keyPart.length) | ||
var value = _stringify(obj[key], nextIndent, | ||
keyPart.length + comma(array, index)) | ||
if (value !== undefined) { | ||
@@ -47,0 +53,0 @@ items.push(keyPart + value) |
{ | ||
"name": "json-stringify-pretty-compact", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"author": "Simon Lydell", | ||
@@ -16,3 +16,5 @@ "license": "MIT", | ||
"compact", | ||
"indent" | ||
"indent", | ||
"format", | ||
"formatter" | ||
], | ||
@@ -19,0 +21,0 @@ "scripts": { |
@@ -151,2 +151,55 @@ // Copyright 2014 Simon Lydell | ||
test("account for commas in objects", function() { | ||
var obj = { | ||
"a": [1, 2, 3], | ||
"b": [1, 2, 3] | ||
} | ||
testStringify(obj, [ | ||
'{', | ||
' "a": [1, 2, 3],', | ||
' "b": [1, 2, 3]', | ||
'}' | ||
], {maxLength: 17}) | ||
testStringify(obj, [ | ||
'{', | ||
' "a": [', | ||
' 1,', | ||
' 2,', | ||
' 3', | ||
' ],', | ||
' "b": [1, 2, 3]', | ||
'}' | ||
], {maxLength: 16}) | ||
}) | ||
test("account for commas in arrays", function() { | ||
var obj = [ | ||
[1, 2, 3], | ||
[1, 2, 3] | ||
] | ||
testStringify(obj, [ | ||
'[', | ||
' [1, 2, 3],', | ||
' [1, 2, 3]', | ||
']' | ||
], {maxLength: 12}) | ||
testStringify(obj, [ | ||
'[', | ||
' [', | ||
' 1,', | ||
' 2,', | ||
' 3', | ||
' ],', | ||
' [1, 2, 3]', | ||
']' | ||
], {maxLength: 11}) | ||
}) | ||
test("Date", function() { | ||
@@ -153,0 +206,0 @@ testStringify(new Date(1337), '"1970-01-01T00:00:01.337Z"') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16256
9
424