From a9ee0bfeed365980e0e5b72a4f6fede1be4cfd0c Mon Sep 17 00:00:00 2001 From: Bill Christo Date: Wed, 21 Sep 2016 12:26:12 -0400 Subject: [PATCH] bug fixes and added jsdocs --- app.js | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/app.js b/app.js index 83649a4..aa64860 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,8 @@ var fs = require("fs"); +/** + * @class + */ function Filejson(cfg) { "use stict"; @@ -49,7 +52,7 @@ function Filejson(cfg) { } }; - if(typeof cfg === undefined) { + if( typeof cfg === "undefined" ) { cfg = {}; } @@ -94,7 +97,17 @@ function Filejson(cfg) { }.bind(this), this.cfg.delay); }; - this.load = function(filename, object, callback) { + /** + * This is the starting point for using Filejson. It is within this function's callback that you will be able to use this module. + * @param {!string} filename - The filename where you would like to load/save changes to. The filename must exist. + * @param {*} [overwriteWith] - You can optionally overwrite the contents of filename. Most of the time this will not be needed. + * @param {!callback} callback - The callback that handles the response. + */ + this.load = function() { + var filename = arguments[0]; + var overwriteWith = (typeof arguments[2] === "undefined") ? undefined : arguments[1]; + var callback = (typeof arguments[2] === "undefined") ? arguments[1] : arguments[2]; + var updateContentsWithoutSaving = function(contents) { this.paused = true; this.contents = contents; @@ -103,9 +116,13 @@ function Filejson(cfg) { this.cfg.filename = filename; - if( callback === undefined ) { - callback = object; - + if(overwriteWith) { + updateContentsWithoutSaving(overwriteWith); + this.save(function(error) { + callback(error, this); + }.bind(this)); + } + else { fs.readFile(filename, "utf-8", function(error, contents) { if (error) { callback(error, this); @@ -129,16 +146,16 @@ function Filejson(cfg) { }.bind(this)); } - else { - updateContentsWithoutSaving(object); - this.save(function(error) { - callback(error, this); - }.bind(this)); - } }; return new Proxy(this, handler); + /** + * @callback callback + * @param {?string} error - callback error + * @param {!Object} Filejson instance - returns the instance + */ + } module.exports = Filejson;