Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-stringify-pretty-compact

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-stringify-pretty-compact - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

changelog.md

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"')

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc