From b4b92a71e2582312776c1a83d889a78d3c6e3ccf Mon Sep 17 00:00:00 2001 From: Bill Christo Date: Fri, 14 Oct 2016 12:26:52 -0400 Subject: [PATCH] bug fix --- README.md | 2 ++ app.js | 23 +++++++++++++---------- package.json | 5 +++-- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5e8ee46..47bcabf 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # [filejson](https://github.com/bchr02/filejson) Use a JSON encoded file to automatically save a JavaScript value to disk whenever that value changes. A value can be a Javascript: string, number, boolean, null, object, or an array. The value can be structured in an array or an object to allow for more complex data stores. These structures can also be nested. As a result, you can use this module as a simple document store for storing semi structured data. +[![NPM](https://nodei.co/npm/filejson.png?downloads=true&stars=true)](https://nodei.co/npm/filejson/) + ## Requirements [ECMAScript 6 Reflect and Proxy objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) support, which is found natively in Node.js >= 6. If you are using a version of Node.js < 6, use a polyfill, such as [harmony-reflect](https://github.com/tvcutsem/harmony-reflect). Proxy support is key for this module working so eloquently. Other non-Proxy based modules require function calls each time you wish to save an object. Unlike those, [filejson](https://github.com/bchr02/filejson) is as easy as ```file.contents = "my new value"``` or ```file.contents = {"msg": "Hello World"}``` and the changes are automatically saved to disk. diff --git a/app.js b/app.js index e3a0245..d04c456 100644 --- a/app.js +++ b/app.js @@ -30,14 +30,14 @@ function Filejson(cfg) { set: function(target, key, value, receiver) { var check = function(value, tree) { var t = typeof value; - if(!(t === 'string' || t === 'number' || t === 'object' || t === 'boolean' || t === 'undefined')) { + if(!(t === "string" || t === "number" || t === "object" || t === "boolean" || t === "undefined")) { throw new Error("NON-JSON COMPATIBLE TYPE FOUND. " + t + " found within: " + tree); } }; var loopAll = function(obj, tree) { for(var key in obj){ tree += "." + key; - if(typeof obj[key] !== 'object'){ + if(typeof obj[key] !== "object"){ check(obj[key], tree); } else { @@ -46,11 +46,7 @@ function Filejson(cfg) { } }; - if(!self.cfg.speed) { - loopAll(self.contents, "file.contents"); - } - - if(!self.cfg.filename) { + if( !self.cfg.filename ) { throw new Error("You must specify a filename"); } @@ -58,7 +54,14 @@ function Filejson(cfg) { value = new Proxy(value, this); } - if(!self.paused) { + // The default behavior to store the value + Reflect.set(target, key, value, receiver); + + if( !self.cfg.speed ) { + loopAll(self.contents, "file.contents"); + } + + if( !self.paused ) { self.save(function(error) { if(error) { console.error(error); @@ -67,8 +70,8 @@ function Filejson(cfg) { }); } - // The default behavior to store the value - return Reflect.set(target, key, value, receiver); + // A Proxy must return true + return true; } }; diff --git a/package.json b/package.json index 9baede9..cee72fe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "filejson", - "version": "1.0.7", + "version": "1.0.8", "description": "Use a JSON encoded file to automatically save a JavaScript value to disk whenever that value changes.", "main": "app.js", "scripts": { @@ -9,7 +9,7 @@ "author": "Bill Christo", "license": "MIT", "devDependencies": { - "eslint": "^3.6.0" + "eslint": "^3.7.1" }, "dependencies": {}, "bugs": { @@ -40,6 +40,7 @@ "Proxy", "Proxies", "auto", + "sync", "autosave", "auto-save", "automatically"