comment-json
Advanced tools
Comparing version 0.1.1 to 0.1.2
'use strict'; | ||
exports.stringify = require('./lib/stringify'); | ||
exports.parse = require('./lib/parse'); |
{ | ||
"name": "comment-json", | ||
"version": "0.1.1", | ||
"description": "Parse and stringify JSON file with documments", | ||
"version": "0.1.2", | ||
"description": "Parse and stringify JSON file with domments", | ||
"main": "index.js", | ||
@@ -30,5 +30,9 @@ "scripts": { | ||
"devDependencies": { | ||
"chai": "*", | ||
"mocha": "*", | ||
"chai": "*" | ||
"test-fixture": "^1.0.2" | ||
}, | ||
"dependencies": { | ||
"json-parser": "^0.2.0" | ||
} | ||
} |
# comment-json [![NPM version](https://badge.fury.io/js/comment-json.svg)](http://badge.fury.io/js/comment-json) [![Build Status](https://travis-ci.org/kaelzhang/node-comment-json.svg?branch=master)](https://travis-ci.org/kaelzhang/node-comment-json) [![Dependency Status](https://gemnasium.com/kaelzhang/node-comment-json.svg)](https://gemnasium.com/kaelzhang/node-comment-json) | ||
Parse and stringify JSON file with documments | ||
Parse and stringify JSON file with domments. | ||
@@ -23,18 +23,15 @@ ## Install | ||
```js | ||
var JSON = require('comment-json'); | ||
var obj = JSON.parse(fs.readFileSync('package.json').toString()); | ||
var json = require('comment-json'); | ||
var obj = json.parse(fs.readFileSync('package.json').toString()); | ||
console.log(obj); | ||
// -> | ||
// { | ||
// "// name": { | ||
// pos: "top", | ||
// body: " package name" | ||
// }, | ||
// "// name": "// package name", | ||
// name: "comment-json" | ||
// } | ||
JSON.stringify(obj, null, 2); // Will be the same as package.json | ||
json.stringify(obj, null, 2); // Will be the same as package.json | ||
``` | ||
### JSON.parse(string, [reviver]) | ||
### json.parse(string, [reviver]) | ||
@@ -44,3 +41,3 @@ The arguments are the same as the vanilla [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). | ||
### JSON.stringify(object, [replacer], [space]) | ||
### json.stringify(object, [replacer], [space]) | ||
@@ -52,8 +49,20 @@ The arguments are the same as the vanilla [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). | ||
### JSON.strip(string) | ||
### json.strip(string) | ||
Strips comments from `string`. | ||
### json.clean(object) | ||
Clean comment properties. | ||
```js | ||
var object = { | ||
"// name": "// package name", | ||
name: "comment-json" | ||
}; | ||
json.clean(object); // {name: "comment-json"} | ||
``` | ||
## License | ||
MIT |
'use strict'; | ||
var expect = require('chai').expect; | ||
var comment_json = require('../'); | ||
var json = require('../'); | ||
var fixture = require('test-fixture'); | ||
var fs = require('fs'); | ||
function each (subjects, replacers, spaces, iterator) { | ||
subjects.forEach(function (subject) { | ||
replacers.forEach(function (replacer) { | ||
spaces.forEach(function (space) { | ||
var desc = [subject, replacer, space].map(function (s) { | ||
return JSON.stringify(s); | ||
}).join(', '); | ||
iterator(subject, replacer, space, desc); | ||
}); | ||
}); | ||
}); | ||
} | ||
var subjects = [ | ||
'abc', | ||
1, | ||
true, | ||
false, | ||
null, | ||
undefined, | ||
[], | ||
{}, | ||
{a: 1, b: null}, | ||
['abc', 1, {a: 1, b: undefined}], | ||
[undefined, 1, 'abc'], | ||
{ | ||
a: undefined, | ||
b: false, | ||
c: [1, '1'] | ||
} | ||
]; | ||
var replacers = [ | ||
null, | ||
function (key, value) { | ||
if (typeof value === 'string') { | ||
return undefined; | ||
} | ||
return value; | ||
} | ||
]; | ||
var spaces = [ | ||
1, | ||
2, | ||
' ', | ||
'1' | ||
]; | ||
describe("vanilla usage of `json.stringify()`", function(){ | ||
each(subjects, replacers, spaces, function (subject, replacer, space, desc) { | ||
it('stringify: ' + desc, function(){ | ||
expect(json.stringify(subject, replacer, space)) | ||
.to | ||
.equal(JSON.stringify(subject, replacer, space)); | ||
}); | ||
}); | ||
}); | ||
describe("enhanced json.stringify()", function(){ | ||
var f = fixture(); | ||
function run (name, replacer, space, desc) { | ||
var file = f.resolve(name + '.js'); | ||
var e = [name, replacer, space].map(function (s) { | ||
return s === null | ||
? 'null' | ||
: s === undefined | ||
? 'undefined' | ||
: s; | ||
}).join('-') + '.json'; | ||
e = f.resolve(e); | ||
it(desc, function(){ | ||
expect(json.stringify(require(file), replacer, space)).to.equal(fs.readFileSync(e).toString()); | ||
}); | ||
} | ||
each([ | ||
'single-top', | ||
'single-right', | ||
'duplex', | ||
'deep' | ||
], | ||
[null], | ||
[2, 3, null], run); | ||
}); | ||
function every (subject, checker) { | ||
if (Object(subject) !== subject) { | ||
return checker(subject); | ||
} | ||
if (Array.isArray(subject)) { | ||
return subject.every(function (v) { | ||
return every(v, checker); | ||
}); | ||
} | ||
var key; | ||
for (key in subject) { | ||
if (!every(subject[key], checker)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
describe("vanilla json.parse()", function(){ | ||
each(subjects, replacers, spaces, function (subject, replacer, space, desc) { | ||
if (typeof space !== 'number' && !(typeof space == 'string' && /^\s*$/.test(space))) { | ||
return; | ||
} | ||
if (!every(subject, function (v) { | ||
return v !== undefined; | ||
})) { | ||
return; | ||
} | ||
if (typeof replacer === 'function') { | ||
return; | ||
} | ||
it('parse: ' + desc, function(){ | ||
var str = JSON.stringify(subject, replacer, space); | ||
expect(json.parse(str)).to.deep.equal(subject); | ||
}); | ||
}); | ||
}); |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
16469
25
443
66
1
3
2
+ Addedjson-parser@^0.2.0
+ Addedesprima@1.2.5(transitive)
+ Addedjson-parser@0.2.4(transitive)