Socket
Socket
Sign inDemoInstall

json-parse-even-better-errors

Package Overview
Dependencies
0
Maintainers
6
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.2.0 to 2.3.0

19

index.js

@@ -65,2 +65,8 @@ 'use strict'

const kIndent = Symbol.for('indent')
const kNewline = Symbol.for('newline')
// only respect indentation if we got a line break, otherwise squash it
// things other than objects and arrays aren't indented, so ignore those
const formatRE = /^\s*[{\[]((?:\r?\n)+)([\s\t]*)/
const parseJson = (txt, reviver, context) => {

@@ -70,3 +76,14 @@ const parseText = stripBOM(txt)

try {
return JSON.parse(parseText, reviver)
// get the indentation so that we can save it back nicely
// if the file starts with {" then we have an indent of '', ie, none
// otherwise, pick the indentation of the next line after the first \n
// If the pattern doesn't match, then it means no indentation.
// JSON.stringify ignores symbols, so this is reasonably safe.
const [, newline, indent] = parseText.match(formatRE) || [, '', '']
const result = JSON.parse(parseText, reviver)
if (result && typeof result === 'object') {
result[kNewline] = newline
result[kIndent] = indent
}
return result
} catch (e) {

@@ -73,0 +90,0 @@ if (typeof txt !== 'string' && !Buffer.isBuffer(txt)) {

2

package.json
{
"name": "json-parse-even-better-errors",
"version": "2.2.0",
"version": "2.3.0",
"description": "JSON.parse with context information on error",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -1,6 +0,11 @@

# json-parse-even-better-errors [![npm version](https://img.shields.io/npm/v/json-parse-even-better-errors.svg)](https://npm.im/json-parse-even-better-errors) [![license](https://img.shields.io/npm/l/json-parse-even-better-errors.svg)](https://npm.im/json-parse-even-better-errors) [![Travis](https://img.shields.io/travis/npm/json-parse-even-better-errors.svg)](https://travis-ci.org/npm/json-parse-even-better-errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/npm/json-parse-even-better-errors?svg=true)](https://ci.appveyor.com/project/npm/json-parse-even-better-errors) [![Coverage Status](https://coveralls.io/repos/github/npm/json-parse-even-better-errors/badge.svg?branch=latest)](https://coveralls.io/github/npm/json-parse-even-better-errors?branch=latest)
# json-parse-even-better-errors
[`json-parse-even-better-errors`](https://github.com/npm/json-parse-even-better-errors) is a Node.js library for
getting nicer errors out of `JSON.parse()`, including context and position of the parse errors.
[`json-parse-even-better-errors`](https://github.com/npm/json-parse-even-better-errors)
is a Node.js library for getting nicer errors out of `JSON.parse()`,
including context and position of the parse errors.
It also preserves the newline and indentation styles of the JSON data, by
putting them in the object or array in the `Symbol.for('indent')` and
`Symbol.for('newline')` properties.
## Install

@@ -33,3 +38,33 @@

* Has a `noExceptions` method that returns undefined rather than throwing.
* Attaches the newline character(s) used to the `Symbol.for('newline')`
property on objects and arrays.
* Attaches the indentation character(s) used to the `Symbol.for('indent')`
property on objects and arrays.
## Indentation
To preserve indentation when the file is saved back to disk, use
`data[Symbol.for('indent')]` as the third argument to `JSON.stringify`, and
if you want to preserve windows `\r\n` newlines, replace the `\n` chars in
the string with `data[Symbol.for('newline')]`.
For example:
```js
const txt = await readFile('./package.json', 'utf8')
const data = parseJsonEvenBetterErrors(txt)
const indent = Symbol.for('indent')
const newline = Symbol.for('newline')
// .. do some stuff to the data ..
const string = JSON.stringify(data, null, data[indent]) + '\n'
const eolFixed = data[newline] === '\n' ? string
: string.replace(/\n/g, data[newline])
await writeFile('./package.json', eolFixed)
```
Indentation is determined by looking at the whitespace between the initial
`{` and `[` and the character that follows it. If you have lots of weird
inconsistent indentation, then it won't track that or give you any way to
preserve it. Whether this is a bug or a feature is debatable ;)
### API

@@ -39,4 +74,6 @@

Works just like `JSON.parse`, but will include a bit more information when an
error happens. This throws a `JSONParseError`.
Works just like `JSON.parse`, but will include a bit more information when
an error happens, and attaches a `Symbol.for('indent')` and
`Symbol.for('newline')` on objects and arrays. This throws a
`JSONParseError`.

@@ -43,0 +80,0 @@ #### <a name="parse"></a> `parse.noExceptions(txt, reviver = null)`

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc