Socket
Socket
Sign inDemoInstall

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.2.0 to 2.0.0

36

CHANGELOG.md

@@ -1,35 +0,45 @@

### Version 1.2.0 (2018-04-22) ###
### Version 2.0.0 (2019-02-02)
- Removed: The `margins` option. Check out
[@aitodotai/json-stringify-pretty-compact] if you miss it. This package is now
purely a combination of `JSON.stringify(obj)` and
`JSON.stringify(obj, null, 2)` with no additional formatting features on top
of that.
- Added: Support for the [replacer] argument.
- Changed: Passing `undefined` to options now result in the default value being
used. This is to align with how destructuring defaults work in ES2015.
### Version 1.2.0 (2018-04-22)
- Added: TypeScript definition. Thanks to @domoritz!
### Version 1.1.0 (2018-01-12)
### Version 1.1.0 (2018-01-12) ###
- Added: The `margins` option. Thanks to @randallsquared!
### Version 1.0.4 (2017-04-29)
### Version 1.0.4 (2017-04-29) ###
- Fixed: String contents are no longer accidentally modified in some cases.
Thanks to @powellquiring!
### Version 1.0.3 (2017-03-30)
### Version 1.0.3 (2017-03-30) ###
- No code changes. Just trying to get the readme to show on npmjs.com.
### Version 1.0.2 (2016-09-08)
### Version 1.0.2 (2016-09-08) ###
- Improved: Limited npm package contents for a smaller download.
### Version 1.0.1 (2014-11-03)
### Version 1.0.1 (2014-11-03) ###
- Fixed: Commas are now accounted for when calculating the available length of a
line, so that they do not appear outside `options.maxLength`.
### Version 1.0.0 (2014-11-01)
### Version 1.0.0 (2014-11-01) ###
- Initial release.
- Initial release.
<!-- prettier-ignore-start -->
[@aitodotai/json-stringify-pretty-compact]: https://www.npmjs.com/package/@aitodotai/json-stringify-pretty-compact
[replacer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter
<!-- prettier-ignore-end -->

@@ -1,8 +0,14 @@

declare module 'json-stringify-pretty-compact' {
const stringify: (object: any, options?: {
indent?: number | string,
maxLength?: number,
margins?: boolean
}) => string;
declare module "json-stringify-pretty-compact" {
const stringify: (
object: any,
options?: {
indent?: number | string;
maxLength?: number;
replacer?:
| ((key: string, value: any) => any)
| (number | string)[]
| null;
}
) => string;
export = stringify;
}

@@ -1,92 +0,99 @@

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))
"use strict";
return (function _stringify (obj, currentIndent, reserved) {
if (obj && typeof obj.toJSON === 'function') {
obj = obj.toJSON()
// Note: This regex matches even invalid JSON strings, but since we’re
// working on the output of `JSON.stringify` we know that only valid strings
// are present (unless the user supplied a weird `options.indent` but in
// that case we don’t care since the output would be invalid anyway).
var stringOrChar = /("(?:[^\\"]|\\.)*")|[:,]/g;
module.exports = function stringify(passedObj, options) {
var indent, maxLength, replacer;
options = options || {};
indent = JSON.stringify(
[1],
undefined,
options.indent === undefined ? 2 : options.indent
).slice(2, -3);
maxLength =
indent === ""
? Infinity
: options.maxLength === undefined
? 80
: options.maxLength;
replacer = options.replacer;
return (function _stringify(obj, currentIndent, reserved) {
// prettier-ignore
var end, index, items, key, keyPart, keys, length, nextIndent, prettified, start, string, value;
if (obj && typeof obj.toJSON === "function") {
obj = obj.toJSON();
}
var string = JSON.stringify(obj)
string = JSON.stringify(obj, replacer);
if (string === undefined) {
return string
return string;
}
var length = maxLength - currentIndent.length - reserved
length = maxLength - currentIndent.length - reserved;
if (string.length <= length) {
var prettified = prettify(string, addMargin)
prettified = string.replace(stringOrChar, function(match, stringLiteral) {
return stringLiteral || match + " ";
});
if (prettified.length <= length) {
return prettified
return prettified;
}
}
if (typeof obj === 'object' && obj !== null) {
var nextIndent = currentIndent + indent
var items = []
var delimiters
var comma = function (array, index) {
return (index === array.length - 1 ? 0 : 1)
}
if (replacer != null) {
obj = JSON.parse(string);
replacer = undefined;
}
if (typeof obj === "object" && obj !== null) {
nextIndent = currentIndent + indent;
items = [];
index = 0;
if (Array.isArray(obj)) {
for (var index = 0; index < obj.length; index++) {
start = "[";
end = "]";
length = obj.length;
for (; index < length; index++) {
items.push(
_stringify(obj[index], nextIndent, comma(obj, index)) || 'null'
)
_stringify(obj[index], nextIndent, index === length - 1 ? 0 : 1) ||
"null"
);
}
delimiters = '[]'
} else {
Object.keys(obj).forEach(function (key, index, array) {
var keyPart = JSON.stringify(key) + ': '
var value = _stringify(obj[key], nextIndent,
keyPart.length + comma(array, index))
start = "{";
end = "}";
keys = Object.keys(obj);
length = keys.length;
for (; index < length; index++) {
key = keys[index];
keyPart = JSON.stringify(key) + ": ";
value = _stringify(
obj[key],
nextIndent,
keyPart.length + (index === length - 1 ? 0 : 1)
);
if (value !== undefined) {
items.push(keyPart + value)
items.push(keyPart + value);
}
})
delimiters = '{}'
}
}
if (items.length > 0) {
return [
delimiters[0],
indent + items.join(',\n' + nextIndent),
delimiters[1]
].join('\n' + currentIndent)
return [start, indent + items.join(",\n" + nextIndent), end].join(
"\n" + currentIndent
);
}
}
return string
}(obj, '', 0))
}
// Note: This regex matches even invalid JSON strings, but since we’re
// working on the output of `JSON.stringify` we know that only valid strings
// are present (unless the user supplied a weird `options.indent` but in
// that case we don’t care since the output would be invalid anyway).
var stringOrChar = /("(?:[^\\"]|\\.)*")|[:,\][}{]/g
function prettify (string, addMargin) {
var m = addMargin ? ' ' : ''
var tokens = {
'{': '{' + m,
'[': '[' + m,
'}': m + '}',
']': m + ']',
',': ', ',
':': ': '
}
return string.replace(stringOrChar, function (match, string) {
return string ? match : tokens[match]
})
}
function get (options, name, defaultValue) {
return (name in options ? options[name] : defaultValue)
}
module.exports = stringify
return string;
})(passedObj, "", 0);
};
{
"name": "json-stringify-pretty-compact",
"version": "1.2.0",
"version": "2.0.0",
"author": "Simon Lydell",

@@ -25,11 +25,18 @@ "license": "MIT",

"scripts": {
"lint": "standard",
"unit": "mocha --ui tdd",
"test": "npm run lint && npm run unit"
"eslint": "eslint .",
"eslint:fix": "npm run eslint -- --fix",
"prettier": "prettier --write \"**/*.{md,ts}\"",
"jest": "jest",
"coverage": "jest --coverage",
"test": "npm run eslint && npm run coverage",
"prepublishOnly": "npm test"
},
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^4.1.0",
"standard": "^10.0.3"
"eslint": "5.13.0",
"eslint-config-lydell": "13.0.0",
"eslint-plugin-jest": "22.2.2",
"eslint-plugin-prettier": "3.0.1",
"jest": "24.0.0",
"prettier": "1.16.3"
}
}

@@ -1,10 +0,10 @@

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/)
========
# json-stringify-pretty-compact [![Build Status][travis-badge]][travis-link]
The output of `JSON.stringify` comes in two flavors: _compact_ and _pretty._ The
The output of [JSON.stringify] comes in two flavors: _compact_ and _pretty._ The
former is usually too compact to be read by humans, while the latter sometimes
is too spacious. This module trades performance (and the “replacer” argument)
for a compromise between the two. The result is a _pretty_ compact string, where
“pretty” means both “kind of” and “nice”.
is too spacious. This module trades performance for a compromise between the
two. The result is a _pretty_ compact string, where “pretty” means both “kind
of” and “nice”.
<!-- prettier-ignore -->
```json

@@ -23,3 +23,3 @@ {

While the “pretty” mode of `JSON.stringify` puts every item of arrays and
While the “pretty” mode of [JSON.stringify] puts every item of arrays and
objects on its own line, this module puts the whole array or object on a single

@@ -30,35 +30,30 @@ line, unless the line becomes too long (the default maximum is 80 characters).

## Installation
Installation
============
```
npm install json-stringify-pretty-compact
```
`npm install json-stringify-pretty-compact`
```js
var stringify = require("json-stringify-pretty-compact")
const stringify = require("json-stringify-pretty-compact");
```
## `stringify(obj, options = {})`
Usage
=====
It’s like `JSON.stringify(obj, options.replacer, options.indent)`, except that
objects and arrays are on one line if they fit (according to
`options.maxLength`).
`stringify(obj, [options])`
---------------------------
It’s like `JSON.stringify(obj, null, options.indent)`, except that objects and
arrays are on one line if they fit (according to `options.maxLength`).
`options`:
- indent: Defaults to 2. Works exactly like the third parameter of
`JSON.stringify`.
[JSON.stringify].
- maxLength: Defaults to 80. Lines will be tried to be kept at maximum this many
characters long.
- margins: Defaults to `false`. Whether or not to add “margins” around brackets
and braces:
- `false`: `{"a": [1]}`
- `true`: `{ "a": [ 1 ] }`
- replacer: Defaults to undefined. Works exactly like the second parameter of
[JSON.stringify].
`stringify(obj, {maxLength: 0, indent: indent})` gives the exact same result as
`JSON.stringify(obj, null, indent)`.
`JSON.stringify(obj, null, indent)`. (However, if you use a `replacer`, integer
keys might be moved first.)

@@ -68,6 +63,29 @@ `stringify(obj, {maxLength: Infinity})` gives the exact same result as

**Want more options?** Check out [@aitodotai/json-stringify-pretty-compact]!
License
=======
## Development
You need Node.js 10 and npm 6.
### npm scripts
- `npm run eslint`: Run [ESLint] \(including [Prettier]).
- `npm run eslint:fix`: Autofix [ESLint] errors.
- `npm run prettier`: Run [Prettier] for files other than JS.
- `npm run jest`: Run unit tests. During development, `npm run jest -- --watch`
is nice.
- `npm run coverage`: Run unit tests with code coverage.
- `npm test`: Check that everything works.
## License
[MIT](LICENSE).
<!-- prettier-ignore-start -->
[@aitodotai/json-stringify-pretty-compact]: https://www.npmjs.com/package/@aitodotai/json-stringify-pretty-compact
[eslint]: https://eslint.org/
[json.stringify]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
[prettier]: https://prettier.io/
[travis-badge]: https://travis-ci.org/lydell/json-stringify-pretty-compact.svg?branch=master
[travis-link]: https://travis-ci.org/lydell/json-stringify-pretty-compact
<!-- prettier-ignore-end -->

Sorry, the diff of this file is not supported yet

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