Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bchr02 committed Oct 14, 2016
1 parent 82b242d commit b4b92a7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
23 changes: 13 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -46,19 +46,22 @@ 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");
}

if( value instanceof Object ) {
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);
Expand All @@ -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;
}
};

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -9,7 +9,7 @@
"author": "Bill Christo",
"license": "MIT",
"devDependencies": {
"eslint": "^3.6.0"
"eslint": "^3.7.1"
},
"dependencies": {},
"bugs": {
Expand Down Expand Up @@ -40,6 +40,7 @@
"Proxy",
"Proxies",
"auto",
"sync",
"autosave",
"auto-save",
"automatically"
Expand Down

0 comments on commit b4b92a7

Please sign in to comment.