Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

comment-json

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

comment-json - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

lib/parse.js

2

index.js
'use strict';
exports.stringify = require('./lib/stringify');
exports.parse = require('./lib/parse');

10

package.json
{
"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);
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc