json-parser
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "json-parser", | ||
"version": "0.1.0", | ||
"description": "JSON parser to parse the AST of JSON object even with comments.", | ||
"version": "0.1.1", | ||
"description": "JSON parser to parse JSON object and MAINTAIN comments.", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
# json-parser [![NPM version](https://badge.fury.io/js/json-parser.svg)](http://badge.fury.io/js/json-parser) [![Build Status](https://travis-ci.org/kaelzhang/node-json-parser.svg?branch=master)](https://travis-ci.org/kaelzhang/node-json-parser) [![Dependency Status](https://gemnasium.com/kaelzhang/node-json-parser.svg)](https://gemnasium.com/kaelzhang/node-json-parser) | ||
JSON parser to parse the AST of JSON object even with comments. | ||
JSON parser to parse JSON object and MAINTAIN comments. | ||
@@ -13,8 +13,45 @@ ## Install | ||
content | ||
``` | ||
/** | ||
blah | ||
*/ | ||
// comment at top | ||
{ | ||
// comment for a | ||
/* block comment */ | ||
"a": 1 // comment at right | ||
} | ||
// comment at bottom | ||
``` | ||
```js | ||
var json_parser = require('json-parser'); | ||
var parser = require('json-parser'); | ||
var object = parser.parse(content); | ||
console.log(object); | ||
``` | ||
And the result will be: | ||
```js | ||
{ | ||
// Comments at the top of the file | ||
'//^': ['/**\n blah\n */', '// comment at top'], | ||
// Comments at the bottom of the file | ||
'//$': ['// comment at bottom'], | ||
// Comment for a property is the value of `'// <prop>'` | ||
'// a': [ | ||
['// comment for a', '/* block comment */'], | ||
['// comment at right'] | ||
], | ||
// The real value | ||
a: 1 | ||
} | ||
``` | ||
## License | ||
MIT |
'use strict'; | ||
var expect = require('chai').expect; | ||
var json_parser = require('../'); | ||
var parser = require('../'); | ||
// var a = parser.parse('//top\n{// top a\n/* abc */"a":1,//right\n/* bcd */"b":{"a":1}}//bottom'); | ||
// // var a = parser.parse('{/*top*/"a":1,//right\n/*abc*/"b":{"a":1}}'); | ||
// console.log(a); | ||
var cases = [ | ||
{ | ||
d: 'comment at the top', | ||
s: '//top\n{"a":1}', | ||
e: function (obj) { | ||
expect(obj.a).to.equal(1); | ||
expect(obj['//^']).to.deep.equal(['//top']); | ||
} | ||
}, | ||
{ | ||
d: 'multiple comments at the top, both line and block', | ||
s: '//top\n/*abc*/{"a":1}', | ||
e: function (obj) { | ||
expect(obj.a).to.equal(1); | ||
expect(obj['//^']).to.deep.equal(['//top', '/*abc*/']); | ||
} | ||
}, | ||
{ | ||
d: 'comment at the bottom', | ||
s: '{"a":1}\n//bot', | ||
e: function (obj) { | ||
expect(obj.a).to.equal(1); | ||
expect(obj['//$']).to.deep.equal(['//bot']); | ||
} | ||
}, | ||
{ | ||
d: 'multiple comments at the bottom, both line and block', | ||
s: '{"a":1}\n//top\n/*abc*/', | ||
e: function (obj) { | ||
expect(obj.a).to.equal(1); | ||
expect(obj['//$']).to.deep.equal(['//top', '/*abc*/']); | ||
} | ||
}, | ||
{ | ||
d: 'comment for properties', | ||
s: '{//a\n"a":1}', | ||
e: function (obj) { | ||
expect(obj.a).to.equal(1); | ||
expect('// a' in obj).to.equal(true); | ||
expect(obj['// a']).to.deep.equal([['//a']]); | ||
} | ||
}, | ||
{ | ||
d: 'comment for properties, multiple at the top', | ||
s: '{//a\n/*b*/"a":1}', | ||
e: function (obj) { | ||
expect(obj.a).to.equal(1); | ||
expect('// a' in obj).to.equal(true); | ||
expect(obj['// a']).to.deep.equal([['//a', '/*b*/']]); | ||
} | ||
}, | ||
{ | ||
d: 'comment for properties, both top and right', | ||
s: '{//a\n"a":1//b\n}', | ||
e: function (obj) { | ||
expect(obj.a).to.equal(1); | ||
expect('// a' in obj).to.equal(true); | ||
expect(obj['// a']).to.deep.equal([['//a'], ['//b']]); | ||
} | ||
} | ||
] | ||
describe("parse()", function(){ | ||
cases.forEach(function (c) { | ||
it(c.d, function(){ | ||
c.e(parser.parse(c.s)); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
9439
262
57