🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →

json-file-plus

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-file-plus - npm Package Compare versions

Comparing version

to
0.2.0

@@ -8,3 +8,3 @@ var fs = require('fs');

var JSONFile = function (raw) {
var JSONFile = function (filename, raw) {
var hasTrailingNewline = (/\n\n$/).test(raw),

@@ -18,2 +18,3 @@ indentMatch = raw.match(/^[ \t]+/m),

};
this.filename = filename;
this.data = JSON.parse(raw);

@@ -40,7 +41,7 @@ };

};
JSONFile.prototype.save = function (filename, callback) {
JSONFile.prototype.save = function (callback) {
var endingNewlines = this.format.trailing ? "\n\n" : "\n";
var indent = this.format.indent || 2;
var json = new Buffer(JSON.stringify(this.data, null, indent) + endingNewlines);
fs.writeFile(filename, json, callback);
fs.writeFile(this.filename, json, callback);
};

@@ -57,3 +58,3 @@

raw = rawBuf.toString('utf8');
try { file = new JSONFile(raw); }
try { file = new JSONFile(filename, raw); }
catch (e) { err = e; }

@@ -60,0 +61,0 @@ }

{
"name": "json-file-plus",
"version": "0.1.1",
"version": "0.2.0",
"author": "Jordan Harband",

@@ -5,0 +5,0 @@ "description": "Read from and write to a JSON file, minimizing diffs and preserving formatting.",

@@ -10,3 +10,5 @@ #json-file-plus <sup>[![Version Badge][2]][1]</sup>

var jsonFile = require('json-file-plus');
jsonFile.read('package.json', function (err, file) {
var path = require('path'); // in node-core
var filename = path.join(process.cwd(), 'package.json');
jsonFile.read(filename, function (err, file) {
if (err) { return doSomethingWithError(err); }

@@ -32,5 +34,8 @@

/* change the filename if desired */
file.filename = path.join(process.cwd(), 'new-package.json');
/* Save the file, preserving formatting. */
/* Callback will be passed to fs.writeFile */
file.save('new-package.json', fsWriteFileCallback);
file.save(fsWriteFileCallback);
});

@@ -37,0 +42,0 @@ ```

@@ -157,1 +157,28 @@ var test = require('tape');

test('remembers filename', function (t) {
t.plan(1);
jsonFile(testFilename, function (err, file) {
t.equal(file.filename, testFilename, 'filename equals ' + testFilename);
t.end();
});
});
test('saves properly', function (t) {
t.plan(4);
jsonFile(testFilename, function (err, file) {
t.equal(file.filename, testFilename, 'filename equals ' + testFilename);
file.set({ foo: !testContents.foo });
file.save(function (err) {
t.notOk(err, 'no error');
jsonFile(testFilename, function (err, file2) {
t.equal(file2.get('foo'), !testContents.foo, 'value was properly saved');
file2.set({ foo: testContents.foo }); // restore original value
file2.save(function (err) {
t.notOk(err, 'no error');
t.end();
});
});
});
});
});

@@ -6,3 +6,7 @@ {

"false": false,
"arr": [1, 2, 3],
"arr": [
1,
2,
3
],
"obj": {

@@ -9,0 +13,0 @@ "nested": {}