依赖文件
This commit is contained in:
+33
@@ -0,0 +1,33 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directory
|
||||
node_modules
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
language: node_js
|
||||
sudo: false
|
||||
|
||||
node_js:
|
||||
- "5"
|
||||
- "4"
|
||||
- "0.12"
|
||||
- "0.11"
|
||||
- "0.10"
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Matteo Collina
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
# fast-json-parse
|
||||
|
||||
[](https://travis-ci.org/mcollina/fast-json-parse)
|
||||
|
||||
It is equivalent to [json-parse-safe](http://npm.im/json-parse-safe),
|
||||
but it set both the `err` and `value` property to null.
|
||||
|
||||
The reason why this is fast is that `try/catch` inhibits the functions
|
||||
in which you use them to be optimized. This assumption holds true up to
|
||||
Node 6, from Node 7 and forward this module is not useful anymore.
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
npm i fast-json-parse --save
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
You can use it as a function or via a contructor, as you prefer.
|
||||
|
||||
### function
|
||||
|
||||
```js
|
||||
'use strict'
|
||||
|
||||
var parse = require('fast-json-parse')
|
||||
var fs = require('fs')
|
||||
|
||||
var result = parse(fs.readFileSync('./package.json'))
|
||||
|
||||
if (result.err) {
|
||||
console.log('unable to parse json', result.err.message)
|
||||
} else {
|
||||
console.log('json parsed successfully', result.value)
|
||||
}
|
||||
```
|
||||
|
||||
### constructor
|
||||
|
||||
```js
|
||||
'use strict'
|
||||
|
||||
var Parse = require('fast-json-parse')
|
||||
var fs = require('fs')
|
||||
|
||||
var result = new Parse(fs.readFileSync('./package.json'))
|
||||
|
||||
if (result.err) {
|
||||
console.log('unable to parse json', result.err.message)
|
||||
} else {
|
||||
console.log('json parsed successfully', result.value)
|
||||
}
|
||||
```
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
fast-json-parse is sponsored by [nearForm](http://nearform.com).
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
'use strict'
|
||||
|
||||
var benchmark = require('benchmark')
|
||||
var suite = new benchmark.Suite()
|
||||
var fs = require('fs')
|
||||
var myData = fs.readFileSync('./package.json')
|
||||
var Parse = require('./')
|
||||
var safe = require('json-parse-safe')
|
||||
var max = 100
|
||||
|
||||
suite.add('Parse class', function parseBench () {
|
||||
var parsed = new Parse(myData)
|
||||
if (parsed.err) {
|
||||
return
|
||||
}
|
||||
|
||||
// add a loop to simulate some activity here
|
||||
for (var i = 0; i < max; i++) {
|
||||
var count = 0
|
||||
var keys = Object.keys(parsed.value)
|
||||
for (var k = 0; k < keys.length; k++) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
suite.add('Parse wrapped', function parseBench () {
|
||||
var parsed = Parse(myData)
|
||||
if (parsed.err) {
|
||||
return
|
||||
}
|
||||
|
||||
// add a loop to simulate some activity here
|
||||
for (var i = 0; i < max; i++) {
|
||||
var count = 0
|
||||
var keys = Object.keys(parsed.value)
|
||||
for (var k = 0; k < keys.length; k++) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
suite.add('json-parse-safe', function parseBench () {
|
||||
var parsed = safe(myData)
|
||||
if (parsed.error) {
|
||||
return
|
||||
}
|
||||
|
||||
// add a loop to simulate some activity here
|
||||
for (var i = 0; i < max; i++) {
|
||||
var count = 0
|
||||
var keys = Object.keys(parsed.value)
|
||||
for (var k = 0; k < keys.length; k++) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
suite.add('try catch here', function tryCatchBench () {
|
||||
var data = null
|
||||
try {
|
||||
data = JSON.parse(myData)
|
||||
} catch (err) {
|
||||
return
|
||||
}
|
||||
|
||||
// add a loop to simulate some activity here
|
||||
for (var i = 0; i < max; i++) {
|
||||
var count = 0
|
||||
var keys = Object.keys(data)
|
||||
for (var k = 0; k < keys.length; k++) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
suite.on('complete', function print () {
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
console.log(this[i].toString())
|
||||
}
|
||||
|
||||
console.log('Fastest is', this.filter('fastest').map('name')[0])
|
||||
})
|
||||
|
||||
suite.run()
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "fast-json-parse",
|
||||
"version": "1.0.3",
|
||||
"description": "Parse json safely and at max speed",
|
||||
"main": "parse.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/mcollina/fast-json-parse.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/mcollina/fast-json-parse/issues"
|
||||
},
|
||||
"homepage": "https://github.com/mcollina/fast-json-parse#readme",
|
||||
"scripts": {
|
||||
"test": "standard && tap test.js"
|
||||
},
|
||||
"pre-commit": "test",
|
||||
"keywords": [
|
||||
"parse",
|
||||
"json",
|
||||
"fast",
|
||||
"safe"
|
||||
],
|
||||
"author": "Matteo Collina <hello@matteocollina.com>",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"benchmark": "^2.1.0",
|
||||
"json-parse-safe": "^1.0.3",
|
||||
"pre-commit": "^1.1.2",
|
||||
"standard": "^6.0.5",
|
||||
"tap": "^5.5.0"
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
'use strict'
|
||||
|
||||
function Parse (data) {
|
||||
if (!(this instanceof Parse)) {
|
||||
return new Parse(data)
|
||||
}
|
||||
this.err = null
|
||||
this.value = null
|
||||
try {
|
||||
this.value = JSON.parse(data)
|
||||
} catch (err) {
|
||||
this.err = err
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Parse
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
'use strict'
|
||||
|
||||
var test = require('tap').test
|
||||
var Parse = require('./')
|
||||
|
||||
function successTests (value) {
|
||||
test('parse successfully with the function', function (t) {
|
||||
t.plan(2)
|
||||
var result = Parse(value)
|
||||
t.error(result.err)
|
||||
t.deepEqual(result.value, JSON.parse(value))
|
||||
})
|
||||
|
||||
test('parse successfully with the constructor', function (t) {
|
||||
t.plan(2)
|
||||
var result = new Parse(value)
|
||||
t.error(result.err)
|
||||
t.deepEqual(result.value, JSON.parse(value))
|
||||
})
|
||||
}
|
||||
|
||||
function failureTests (value) {
|
||||
var expectedErr
|
||||
try {
|
||||
JSON.parse(value)
|
||||
} catch (err) {
|
||||
expectedErr = err
|
||||
}
|
||||
|
||||
test('parse unsuccessfully with the function', function (t) {
|
||||
t.plan(2)
|
||||
var result = Parse(value)
|
||||
t.notOk(result.value, 'no value')
|
||||
t.equal(result.err.message, expectedErr.message)
|
||||
})
|
||||
|
||||
test('parse successfully with the constructor', function (t) {
|
||||
t.plan(2)
|
||||
var result = new Parse(value)
|
||||
t.notOk(result.value, 'no value')
|
||||
t.equal(result.err.message, expectedErr.message)
|
||||
})
|
||||
}
|
||||
|
||||
successTests('{ "object": 32 }')
|
||||
failureTests('{ "object": 32')
|
||||
Reference in New Issue
Block a user