json-parser
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -20,6 +20,8 @@ 'use strict'; | ||
var reviver; | ||
var remove_comments; | ||
function parse (code, rev) { | ||
function parse (code, rev, no_comments) { | ||
tokens = tokenize(code); | ||
reviver = rev; | ||
remove_comments = no_comments; | ||
@@ -37,3 +39,3 @@ if (!tokens.length) { | ||
if (Object(result) === result) { | ||
if (Object(result) === result && !remove_comments) { | ||
if (tokens.head_comments.length) { | ||
@@ -116,3 +118,3 @@ result['//^'] = tokens.head_comments; | ||
name = JSON.parse(current.value); | ||
if (current.comments) { | ||
if (current.comments && !remove_comments) { | ||
obj['// ' + name] = current.comments; | ||
@@ -119,0 +121,0 @@ } |
{ | ||
"name": "json-parser", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "JSON parser to parse JSON object and MAINTAIN comments.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
[![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) | ||
[![Dependency Status](https://david-dm.org/kaelzhang/node-json-parser.svg)](https://david-dm.org/kaelzhang/node-json-parser) | ||
@@ -19,2 +19,12 @@ # json-parser | ||
```js | ||
parser(text, [reviver=null,] [remove_comments=false]) | ||
``` | ||
- text `String` The string to parse as JSON. See the [JSON](http://json.org/) object for a description of JSON syntax. | ||
- reviver `function()|null` Default to `null`. It acts the same as the second parameter of [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). If a function, prescribes how the value originally produced by parsing is transformed, before being returned. | ||
- remove_comments `Boolean` If true, the parsed JSON Object won't contain comments | ||
Returns the `Object` corresponding to the given JSON text. | ||
content | ||
@@ -62,4 +72,17 @@ | ||
```js | ||
var object_no_comments = parser.parse(content, null, true); | ||
console.log(object_no_comments) | ||
``` | ||
And the result will be: | ||
```js | ||
{ | ||
a: 1 | ||
} | ||
``` | ||
## License | ||
MIT |
@@ -14,2 +14,3 @@ 'use strict'; | ||
s: '//top\n{"a":1}', | ||
o: '{"a":1}', | ||
e: function (obj) { | ||
@@ -23,2 +24,3 @@ expect(obj.a).to.equal(1); | ||
s: '//top\n/*abc*/{"a":1}', | ||
o: '{"a":1}', | ||
e: function (obj) { | ||
@@ -32,2 +34,3 @@ expect(obj.a).to.equal(1); | ||
s: '{"a":1}\n//bot', | ||
o: '{"a":1}', | ||
e: function (obj) { | ||
@@ -41,2 +44,3 @@ expect(obj.a).to.equal(1); | ||
s: '{"a":1}\n//top\n/*abc*/', | ||
o: '{"a":1}', | ||
e: function (obj) { | ||
@@ -50,2 +54,3 @@ expect(obj.a).to.equal(1); | ||
s: '{//a\n"a":1}', | ||
o: '{"a":1}', | ||
e: function (obj) { | ||
@@ -60,2 +65,3 @@ expect(obj.a).to.equal(1); | ||
s: '{//a\n/*b*/"a":1}', | ||
o: '{"a":1}', | ||
e: function (obj) { | ||
@@ -70,2 +76,3 @@ expect(obj.a).to.equal(1); | ||
s: '{//a\n"a":1//b\n}', | ||
o: '{"a":1}', | ||
e: function (obj) { | ||
@@ -79,10 +86,17 @@ expect(obj.a).to.equal(1); | ||
describe("parse()", function(){ | ||
cases.forEach(function (c) { | ||
cases.forEach(function (c) { | ||
describe("parse()", function(){ | ||
it(c.d, function(){ | ||
c.e(parser.parse(c.s)); | ||
}); | ||
it(c.d + ', removes comments', function(){ | ||
expect(parser.parse(c.s, null, true)).to.deep.equal(parser.parse(c.o)); | ||
}); | ||
}); | ||
}); | ||
var invalid = [ | ||
@@ -89,0 +103,0 @@ '{', |
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
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
11742
321
87