edit-json-file
Advanced tools
Comparing version 1.0.1 to 1.0.2
224
lib/index.js
"use strict"; | ||
const findValue = require("find-value") | ||
, setValue = require("set-value") | ||
, rJson = require("r-json") | ||
, fs = require("fs") | ||
; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
class JsonEditor { | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var findValue = require("find-value"), | ||
setValue = require("set-value"), | ||
rJson = require("r-json"), | ||
fs = require("fs"), | ||
iterateObject = require("iterate-object"); | ||
var JsonEditor = function () { | ||
/** | ||
@@ -25,3 +31,5 @@ * JsonEditor | ||
*/ | ||
constructor (path, options) { | ||
function JsonEditor(path, options) { | ||
_classCallCheck(this, JsonEditor); | ||
this.options = options = options || {}; | ||
@@ -44,100 +52,124 @@ options.stringify_width = options.stringify_width || 2; | ||
*/ | ||
set (path, value) { | ||
if (typeof path === "object") { | ||
iterateObject(path, (val, n) => { | ||
setValue(this.data, n, val) | ||
}); | ||
} else { | ||
setValue(this.data, path, value); | ||
_createClass(JsonEditor, [{ | ||
key: "set", | ||
value: function set(path, value) { | ||
var _this = this; | ||
if ((typeof path === "undefined" ? "undefined" : _typeof(path)) === "object") { | ||
iterateObject(path, function (val, n) { | ||
setValue(_this.data, n, val); | ||
}); | ||
} else { | ||
setValue(this.data, path, value); | ||
} | ||
if (this.options.autosave) { | ||
this.save(); | ||
} | ||
return this; | ||
} | ||
if (this.options.autosave) { | ||
this.save(); | ||
/** | ||
* get | ||
* Get a value in a specific path. | ||
* | ||
* @name get | ||
* @function | ||
* @param {String} path | ||
* @returns {Value} The object path value. | ||
*/ | ||
}, { | ||
key: "get", | ||
value: function get(path) { | ||
if (path) { | ||
return findValue(this.data, path); | ||
} | ||
return this.toObject(); | ||
} | ||
return this; | ||
} | ||
/** | ||
* get | ||
* Get a value in a specific path. | ||
* | ||
* @name get | ||
* @function | ||
* @param {String} path | ||
* @returns {Value} The object path value. | ||
*/ | ||
get (path) { | ||
if (path) { | ||
return findValue(this.data, path) | ||
/** | ||
* read | ||
* Read the JSON file. | ||
* | ||
* @name read | ||
* @function | ||
* @param {Function} cb An optional callback function which will turn the function into an asynchronous one. | ||
* @returns {Object} The object parsed as object or an empty object by default. | ||
*/ | ||
}, { | ||
key: "read", | ||
value: function read(cb) { | ||
if (!cb) { | ||
try { | ||
return rJson(this.path); | ||
} catch (e) { | ||
return {}; | ||
} | ||
} | ||
rJson(this.path, function (err, data) { | ||
data = err ? {} : data; | ||
cb(null, data); | ||
}); | ||
} | ||
return this.toObject(); | ||
} | ||
/** | ||
* read | ||
* Read the JSON file. | ||
* | ||
* @name read | ||
* @function | ||
* @param {Function} cb An optional callback function which will turn the function into an asynchronous one. | ||
* @returns {Object} The object parsed as object or an empty object by default. | ||
*/ | ||
read (cb) { | ||
if (!cb) { | ||
try { | ||
return rJson(this.path); | ||
} catch (e) { | ||
return {}; | ||
/** | ||
* write | ||
* Write the JSON file. | ||
* | ||
* @name read | ||
* @function | ||
* @param {String} The file content. | ||
* @param {Function} cb An optional callback function which will turn the function into an asynchronous one. | ||
* @returns {JsonEditor} The `JsonEditor` instance. | ||
*/ | ||
}, { | ||
key: "write", | ||
value: function write(content, cb) { | ||
if (cb) { | ||
fs.writeFile(this.path, content, cb); | ||
} else { | ||
fs.writeFileSync(this.path, content); | ||
} | ||
return this; | ||
} | ||
rJson(this.path, function (err, data) { | ||
data = err ? {} : data; | ||
cb(null, data); | ||
}); | ||
} | ||
/** | ||
* write | ||
* Write the JSON file. | ||
* | ||
* @name read | ||
* @function | ||
* @param {String} The file content. | ||
* @param {Function} cb An optional callback function which will turn the function into an asynchronous one. | ||
* @returns {JsonEditor} The `JsonEditor` instance. | ||
*/ | ||
write (content, cb) { | ||
if (cb) { | ||
fs.writeFile(this.path, content, cb); | ||
} else { | ||
fs.writeFileSync(this.path, content); | ||
/** | ||
* save | ||
* Save the file back to disk. | ||
* | ||
* @name save | ||
* @function | ||
* @param {Function} cb An optional callback function which will turn the function into an asynchronous one. | ||
* @returns {JsonEditor} The `JsonEditor` instance. | ||
*/ | ||
}, { | ||
key: "save", | ||
value: function save(cb) { | ||
this.write(JSON.stringify(this.data, this.options.stringify_fn, this.options.stringify_width), cb); | ||
return this; | ||
} | ||
return this; | ||
} | ||
/** | ||
* save | ||
* Save the file back to disk. | ||
* | ||
* @name save | ||
* @function | ||
* @param {Function} cb An optional callback function which will turn the function into an asynchronous one. | ||
* @returns {JsonEditor} The `JsonEditor` instance. | ||
*/ | ||
save (cb) { | ||
this.write(JSON.stringify(this.data, this.options.stringify_fn, this.options.stringify_width), cb) | ||
return this; | ||
} | ||
/** | ||
* toObject | ||
* | ||
* @name toObject | ||
* @function | ||
* @returns {Object} The data object. | ||
*/ | ||
/** | ||
* toObject | ||
* | ||
* @name toObject | ||
* @function | ||
* @returns {Object} The data object. | ||
*/ | ||
toObject () { | ||
return this.data; | ||
} | ||
} | ||
}, { | ||
key: "toObject", | ||
value: function toObject() { | ||
return this.data; | ||
} | ||
}]); | ||
return JsonEditor; | ||
}(); | ||
/** | ||
@@ -153,4 +185,6 @@ * editJsonFile | ||
*/ | ||
module.exports = function editJsonFile (path, options) { | ||
module.exports = function editJsonFile(path, options) { | ||
return new JsonEditor(path, options); | ||
}; | ||
}; |
@@ -13,3 +13,3 @@ { | ||
"license": "MIT", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"main": "lib/index.js", | ||
@@ -42,2 +42,3 @@ "scripts": { | ||
"find-value": "^1.0.3", | ||
"iterate-object": "^1.3.2", | ||
"r-json": "^1.2.5", | ||
@@ -44,0 +45,0 @@ "set-value": "^0.4.0", |
Sorry, the diff of this file is not supported yet
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
12383
162
5
+ Addediterate-object@^1.3.2
+ Addediterate-object@1.3.4(transitive)