Skip to content

Commit 9dd1a19

Browse files
committed
feat(init): Initial Commit
0 parents  commit 9dd1a19

File tree

11 files changed

+7031
-0
lines changed

11 files changed

+7031
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/node_modules
2+
/.nyc_output
3+
/test/cache

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
sudo: false
3+
node_js:
4+
- "7"
5+
- "6"
6+
- "4"

ISSUE_TEMPLATE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--
2+
⚠️🚨 BEFORE FILING AN ISSUE: 🚨⚠️
3+
4+
👉🏼 CONTRIBUTING.md 👈🏼 (the "contribution guidelines" up there ☝🏼)
5+
6+
I PROMISE IT'S A VERY VERY SHORT READ.🙇🏼
7+
-->

LICENSE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
To the extent possible under law, maintainers for this project have waived all copyright and related or neighboring rights to this project.
2+
3+
For more information on this waiver, see: https://creativecommons.org/publicdomain/zero/1.0/

PULL_REQUEST_TEMPLATE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--
2+
⚠️🚨 BEFORE FILING A PR: 🚨⚠️
3+
4+
👉🏼 CONTRIBUTING.md 👈🏼 (the "contribution guidelines" up there ☝🏼)
5+
6+
I PROMISE IT'S A VERY VERY SHORT READ.🙇🏼
7+
-->

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 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)
2+
3+
[`json-parse-better-errors`](https://github.com/zkat/json-parse-better-errors) is a Node.js library for managing
4+
local key and content address caches. It's really fast, really good at
5+
concurrency, and it will never give you corrupted data, even if cache files
6+
get corrupted or manipulated.
7+
8+
It was originally written to be used as [npm](https://npm.im)'s local cache, but
9+
can just as easily be used on its own
10+
11+
_Translations: [español](README.es.md)_
12+
13+
## Install
14+
15+
`$ npm install --save json-parse-better-errors`
16+
17+
## Table of Contents
18+
19+
* [Example](#example)
20+
* [Features](#features)
21+
* [Contributing](#contributing)
22+
* [API](#api)
23+
* [`parse`](#parse)
24+
25+
### Example
26+
27+
```javascript
28+
const parseJson = require('json-parse-better-errors')
29+
30+
parseJson('"foo"')
31+
parseJson('garbage') // more useful error message
32+
```
33+
34+
### Features
35+
36+
* Like JSON.parse, but the errors are better.
37+
38+
### Contributing
39+
40+
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.
41+
42+
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.
43+
44+
Please refer to the [Changelog](CHANGELOG.md) for project history details, too.
45+
46+
Happy hacking!
47+
48+
### API
49+
50+
#### <a name="parse"></a> `> parse(txt, ?reviver, ?context=20)`
51+
52+
Works just like `JSON.parse`, but will include a bit more information when an
53+
error happens.

appveyor.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
environment:
2+
matrix:
3+
- nodejs_version: "7"
4+
- nodejs_version: "6"
5+
- nodejs_version: "4"
6+
7+
platform:
8+
- x64
9+
10+
install:
11+
- ps: Install-Product node $env:nodejs_version $env:platform
12+
- npm config set spin false
13+
- npm install
14+
15+
test_script:
16+
- npm test
17+
18+
matrix:
19+
fast_finish: true
20+
21+
build: off

index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
module.exports = parseJson
4+
function parseJson (txt, reviver, context) {
5+
context = context || 20
6+
try {
7+
return JSON.parse(txt, reviver)
8+
} catch (e) {
9+
const syntaxErr = e.message.match(/^Unexpected token.*position\s+(\d+)/i)
10+
const errIdx = syntaxErr
11+
? +syntaxErr[1]
12+
: e.message.match(/^Unexpected end of JSON.*/i)
13+
? txt.length - 1
14+
: null
15+
if (errIdx != null) {
16+
const start = errIdx <= context
17+
? 0
18+
: errIdx - context
19+
const end = errIdx + context >= txt.length
20+
? txt.length
21+
: errIdx + context
22+
e.message += ` while parsing near '${
23+
start === 0 ? '' : '...'
24+
}${txt.slice(start, end)}${
25+
end === txt.length ? '' : '...'
26+
}'`
27+
} else {
28+
e.message += ` while parsing '${txt.slice(0, context * 2)}'`
29+
}
30+
throw e
31+
}
32+
}

0 commit comments

Comments
 (0)