forked from zkat/json-parse-better-errors
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: This is the first commit!
- Loading branch information
0 parents
commit 562c977
Showing
12 changed files
with
7,042 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/node_modules | ||
/.nyc_output | ||
/test/cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: node_js | ||
sudo: false | ||
node_js: | ||
- "7" | ||
- "6" | ||
- "4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Change Log | ||
|
||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. | ||
|
||
<a name="0.1.0"></a> | ||
# 0.1.0 (2017-08-15) | ||
|
||
|
||
### Features | ||
|
||
* **init:** Initial Commit ([9dd1a19](https://github.com/zkat/json-parse-better-errors/commit/9dd1a19)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<!-- | ||
⚠️🚨 BEFORE FILING AN ISSUE: 🚨⚠️ | ||
|
||
👉🏼 CONTRIBUTING.md 👈🏼 (the "contribution guidelines" up there ☝🏼) | ||
|
||
I PROMISE IT'S A VERY VERY SHORT READ.🙇🏼 | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
To the extent possible under law, maintainers for this project have waived all copyright and related or neighboring rights to this project. | ||
|
||
For more information on this waiver, see: https://creativecommons.org/publicdomain/zero/1.0/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<!-- | ||
⚠️🚨 BEFORE FILING A PR: 🚨⚠️ | ||
|
||
👉🏼 CONTRIBUTING.md 👈🏼 (the "contribution guidelines" up there ☝🏼) | ||
|
||
I PROMISE IT'S A VERY VERY SHORT READ.🙇🏼 | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# json-parse-better-errors [![npm version](https://img.shields.io/npm/v/json-parse-better-errors.svg)](https://npm.im/json-parse-better-errors) [![license](https://img.shields.io/npm/l/json-parse-better-errors.svg)](https://npm.im/json-parse-better-errors) [![Travis](https://img.shields.io/travis/zkat/json-parse-better-errors.svg)](https://travis-ci.org/zkat/json-parse-better-errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/zkat/json-parse-better-errors?svg=true)](https://ci.appveyor.com/project/zkat/json-parse-better-errors) [![Coverage Status](https://coveralls.io/repos/github/zkat/json-parse-better-errors/badge.svg?branch=latest)](https://coveralls.io/github/zkat/json-parse-better-errors?branch=latest) | ||
|
||
[`json-parse-better-errors`](https://github.com/zkat/json-parse-better-errors) is a Node.js library for managing | ||
local key and content address caches. It's really fast, really good at | ||
concurrency, and it will never give you corrupted data, even if cache files | ||
get corrupted or manipulated. | ||
|
||
It was originally written to be used as [npm](https://npm.im)'s local cache, but | ||
can just as easily be used on its own | ||
|
||
_Translations: [español](README.es.md)_ | ||
|
||
## Install | ||
|
||
`$ npm install --save json-parse-better-errors` | ||
|
||
## Table of Contents | ||
|
||
* [Example](#example) | ||
* [Features](#features) | ||
* [Contributing](#contributing) | ||
* [API](#api) | ||
* [`parse`](#parse) | ||
|
||
### Example | ||
|
||
```javascript | ||
const parseJson = require('json-parse-better-errors') | ||
|
||
parseJson('"foo"') | ||
parseJson('garbage') // more useful error message | ||
``` | ||
|
||
### Features | ||
|
||
* Like JSON.parse, but the errors are better. | ||
|
||
### Contributing | ||
|
||
The json-parse-better-errors team enthusiastically welcomes contributions and project participation! There's a bunch of things you can do if you want to contribute! The [Contributor Guide](CONTRIBUTING.md) has all the information you need for everything from reporting bugs to contributing entire new features. Please don't hesitate to jump in if you'd like to, or even ask us questions if something isn't clear. | ||
|
||
All participants and maintainers in this project are expected to follow [Code of Conduct](CODE_OF_CONDUCT.md), and just generally be excellent to each other. | ||
|
||
Please refer to the [Changelog](CHANGELOG.md) for project history details, too. | ||
|
||
Happy hacking! | ||
|
||
### API | ||
|
||
#### <a name="parse"></a> `> parse(txt, ?reviver, ?context=20)` | ||
|
||
Works just like `JSON.parse`, but will include a bit more information when an | ||
error happens. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
environment: | ||
matrix: | ||
- nodejs_version: "7" | ||
- nodejs_version: "6" | ||
- nodejs_version: "4" | ||
|
||
platform: | ||
- x64 | ||
|
||
install: | ||
- ps: Install-Product node $env:nodejs_version $env:platform | ||
- npm config set spin false | ||
- npm install | ||
|
||
test_script: | ||
- npm test | ||
|
||
matrix: | ||
fast_finish: true | ||
|
||
build: off |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
module.exports = parseJson | ||
function parseJson (txt, reviver, context) { | ||
context = context || 20 | ||
try { | ||
return JSON.parse(txt, reviver) | ||
} catch (e) { | ||
const syntaxErr = e.message.match(/^Unexpected token.*position\s+(\d+)/i) | ||
const errIdx = syntaxErr | ||
? +syntaxErr[1] | ||
: e.message.match(/^Unexpected end of JSON.*/i) | ||
? txt.length - 1 | ||
: null | ||
if (errIdx != null) { | ||
const start = errIdx <= context | ||
? 0 | ||
: errIdx - context | ||
const end = errIdx + context >= txt.length | ||
? txt.length | ||
: errIdx + context | ||
e.message += ` while parsing near '${ | ||
start === 0 ? '' : '...' | ||
}${txt.slice(start, end)}${ | ||
end === txt.length ? '' : '...' | ||
}'` | ||
} else { | ||
e.message += ` while parsing '${txt.slice(0, context * 2)}'` | ||
} | ||
throw e | ||
} | ||
} |
Oops, something went wrong.