json-file-plus
Advanced tools
Comparing version
@@ -120,4 +120,14 @@ { | ||
"disallowArrowFunctions": true | ||
"disallowArrowFunctions": true, | ||
"disallowMultiLineTernary": true, | ||
"validateOrderInObjectKeys": "asc-insensitive", | ||
"disallowIdenticalDestructuringNames": true, | ||
"disallowNestedTernaries": { "maxLevel": 1 }, | ||
"requireSpaceAfterComma": true | ||
} | ||
@@ -0,1 +1,9 @@ | ||
[3.1.0](https://github.com/ljharb/node-json-file/releases/tag/v3.1.0) / 2015-10-17 | ||
================== | ||
* [New] Create `JSONData` and expose it, to separate "file" behavior from "json" behavior | ||
* [Deps] update `is`, `promiseback` | ||
* [Dev Deps] update `tape`, `jscs`, `eslint`, `@ljharb/eslint-config`, `nsp`, `object-keys` | ||
* [Tests] up to `io.js` `v3.3`, `node` `v4.2` | ||
* [Tests] Use `evalmd` to verify the README | ||
[3.0.1](https://github.com/ljharb/node-json-file/releases/tag/v3.0.1) / 2015-08-18 | ||
@@ -2,0 +10,0 @@ ================== |
31
index.js
@@ -8,3 +8,3 @@ 'use strict'; | ||
var JSONFile = function JSONFile(filename, raw) { | ||
var JSONData = function JSONData(raw) { | ||
var hasTrailingNewline = (/\n\n$/).test(raw); | ||
@@ -18,6 +18,8 @@ var indentMatch = String(raw).match(/^[ \t]+/m); | ||
}; | ||
this.filename = filename; | ||
this.data = JSON.parse(raw); | ||
if (raw) { | ||
this.data = JSON.parse(raw); | ||
} | ||
}; | ||
JSONFile.prototype.get = function (key, callback) { | ||
JSONData.prototype.get = function (key, callback) { | ||
var data = extend({}, this.data); | ||
@@ -36,12 +38,21 @@ if (is.fn(key)) { | ||
}; | ||
JSONFile.prototype.set = function (obj) { | ||
JSONData.prototype.set = function (obj) { | ||
if (!is.hash(obj)) { throw new TypeError('object must be a plain object'); } | ||
extend(true, this.data, obj); | ||
}; | ||
JSONFile.prototype.save = function (callback) { | ||
JSONData.prototype.stringify = function stringify() { | ||
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); | ||
return new Buffer(JSON.stringify(this.data, null, indent) + endingNewlines); | ||
}; | ||
var JSONFile = function JSONFile(filename, raw) { | ||
JSONData.call(this, raw); | ||
this.filename = filename; | ||
}; | ||
JSONFile.prototype = new JSONData(); | ||
JSONFile.prototype.save = function (callback) { | ||
var deferred = promiseback(callback); | ||
fs.writeFile(this.filename, json, function (err, result) { | ||
fs.writeFile(this.filename, this.stringify(), function (err, result) { | ||
if (err) { | ||
@@ -56,3 +67,3 @@ deferred.reject(err); | ||
var readJSON = function (filename) { | ||
var readJSON = function readJSON(filename) { | ||
var callback; | ||
@@ -83,4 +94,4 @@ if (arguments.length > 1) { | ||
readJSON.JSONFile = JSONFile; | ||
readJSON.JSONData = JSONData; | ||
module.exports = readJSON; | ||
{ | ||
"name": "json-file-plus", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"author": "Jordan Harband", | ||
@@ -9,3 +9,3 @@ "description": "Read from and write to a JSON file, minimizing diffs and preserving formatting.", | ||
"scripts": { | ||
"test": "npm run lint && node test/test.js && npm run security", | ||
"test": "npm run lint && evalmd README.md && node test/test.js && npm run security", | ||
"coverage": "covert test/test.js", | ||
@@ -32,15 +32,16 @@ "coverage-quiet": "covert test/test.js --quiet", | ||
"dependencies": { | ||
"is": "^3.0.1", | ||
"is": "^3.1.0", | ||
"node.extend": "^1.1.5", | ||
"promiseback": "^2.0.1" | ||
"promiseback": "^2.0.2" | ||
}, | ||
"devDependencies": { | ||
"tape": "^4.2.0", | ||
"tape": "^4.2.1", | ||
"foreach": "^2.0.5", | ||
"object-keys": "^1.0.7", | ||
"object-keys": "^1.0.8", | ||
"covert": "^1.1.0", | ||
"jscs": "^2.1.0", | ||
"eslint": "^1.2.0", | ||
"@ljharb/eslint-config": "^1.0.5", | ||
"nsp": "^1.0.3" | ||
"jscs": "^2.3.4", | ||
"eslint": "^1.7.1", | ||
"@ljharb/eslint-config": "^1.4.1", | ||
"nsp": "^1.1.0", | ||
"evalmd": "^0.0.16" | ||
}, | ||
@@ -47,0 +48,0 @@ "engines": { |
@@ -18,2 +18,4 @@ #json-file-plus <sup>[![Version Badge][npm-version-svg]][npm-url]</sup> | ||
var filename = path.join(process.cwd(), 'package.json'); | ||
var callback = function (err, result) { /* your code here */ }; | ||
/* Note: jsonFile also returns a Promise, if you prefer that to a Node-style callback ("errorback"). */ | ||
@@ -47,3 +49,3 @@ jsonFile(filename, function (err, file) { | ||
/* Returns a Promise. */ | ||
file.save(fsWriteFileCallback).then(function () { | ||
file.save(callback).then(function () { | ||
console.log('success!'); | ||
@@ -50,0 +52,0 @@ }).catch(function (err) { |
@@ -12,10 +12,10 @@ 'use strict'; | ||
var testContents = { | ||
arr: [1, 2, 3], | ||
'false': false, | ||
foo: 'bar', | ||
'null': null, | ||
'true': true, | ||
'false': false, | ||
arr: [1, 2, 3], | ||
obj: { | ||
nested: {} | ||
} | ||
}, | ||
'true': true | ||
}; | ||
@@ -39,6 +39,7 @@ | ||
test('returns a file', function (t) { | ||
t.plan(2); | ||
t.plan(3); | ||
jsonFile(testFilename, function (err, file) { | ||
t.error(err, 'no error'); | ||
t.ok(file instanceof jsonFile.JSONFile, 'file is instance of JSONFile'); | ||
t.ok(file instanceof jsonFile.JSONData, 'file is instance of JSONData'); | ||
t.end(); | ||
@@ -205,4 +206,4 @@ }); | ||
var expectedError = { | ||
code: 'ENOENT', | ||
errno: err.errno, | ||
code: 'ENOENT', | ||
path: filename | ||
@@ -209,0 +210,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
26631
10.05%13
8.33%413
3.77%73
2.82%9
12.5%Updated
Updated