json-stringify-pretty-compact
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -0,1 +1,6 @@ | ||
### Version 1.1.0 (2018-01-12) ### | ||
- Added: The `margins` option. Thanks to @randallsquared! | ||
### Version 1.0.4 (2017-04-29) ### | ||
@@ -2,0 +7,0 @@ |
21
index.js
@@ -1,7 +0,5 @@ | ||
// Copyright 2014, 2016 Simon Lydell | ||
// X11 (“MIT”) Licensed. (See LICENSE.) | ||
function stringify (obj, options) { | ||
options = options || {} | ||
var indent = JSON.stringify([1], null, get(options, 'indent', 2)).slice(2, -3) | ||
var addMargin = get(options, 'margins', false) | ||
var maxLength = (indent === '' ? Infinity : get(options, 'maxLength', 80)) | ||
@@ -23,3 +21,3 @@ | ||
if (string.length <= length) { | ||
var prettified = prettify(string) | ||
var prettified = prettify(string, addMargin) | ||
if (prettified.length <= length) { | ||
@@ -74,7 +72,16 @@ return prettified | ||
// that case we don’t care since the output would be invalid anyway). | ||
var stringOrChar = /("(?:[^\\"]|\\.)*")|[:,]/g | ||
var stringOrChar = /("(?:[^\\"]|\\.)*")|[:,\][}{]/g | ||
function prettify (string) { | ||
function prettify (string, addMargin) { | ||
var m = addMargin ? ' ' : '' | ||
var tokens = { | ||
'{': '{' + m, | ||
'[': '[' + m, | ||
'}': m + '}', | ||
']': m + ']', | ||
',': ', ', | ||
':': ': ' | ||
} | ||
return string.replace(stringOrChar, function (match, string) { | ||
return string ? match : match + ' ' | ||
return string ? match : tokens[match] | ||
}) | ||
@@ -81,0 +88,0 @@ } |
{ | ||
"name": "json-stringify-pretty-compact", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"author": "Simon Lydell", | ||
@@ -29,6 +29,6 @@ "license": "MIT", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"mocha": "^3.2.0", | ||
"standard": "^9.0.2" | ||
"chai": "^4.1.2", | ||
"mocha": "^4.1.0", | ||
"standard": "^10.0.3" | ||
} | ||
} |
@@ -55,2 +55,6 @@ Overview [![Build Status](https://travis-ci.org/lydell/json-stringify-pretty-compact.svg?branch=master)](https://travis-ci.org/lydell/json-stringify-pretty-compact) [![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) | ||
characters long. | ||
- margins: Defaults to `false`. Whether or not to add “margins” around brackets | ||
and braces: | ||
- `false`: `{"a": [1]}` | ||
- `true`: `{ "a": [ 1 ] }` | ||
@@ -67,2 +71,2 @@ `stringify(obj, {maxLength: 0, indent: indent})` gives the exact same result as | ||
[The X11 (“MIT”) License](LICENSE). | ||
[MIT](LICENSE). |
Sorry, the diff of this file is not supported yet
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
7390
79
71