Skip to content

Commit

Permalink
Merge pull request #2 from de-luca/feature/deep-get-set-has
Browse files Browse the repository at this point in the history
Deep access support
  • Loading branch information
de-luca committed Apr 19, 2016
2 parents 63a5229 + 7bed4d0 commit ce9601a
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
before_script:
- export DISPLAY=:99.0; sh -e /etc/init.d/xvfb start
notifications:
email: false
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ console.log(config.get('foo')); // shows 'bar'

## API

*All `key` can be a classic key (eg: `foo`) or a multiple level key with levels separated by `.` (eg: `foo.bar`)*

### `config.file()`
Returns the name of the file the config is stored in.

Expand All @@ -29,8 +31,9 @@ Set a key with the specified value. If the key is already in use its value will
### `config.get(key)`
Returns the value associated with the key. If the key is not defined `undefined` is returned.

### `config.keys()`
Returns an array containing all keys in the config file.
### `config.keys([key])`
If `key` is provided, returns an array containing all keys if the `key` object.
If `key` is omitted, returns an array containing all keys in the config file.

### `config.all()`
Returns an object with all the data currently saved.
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-json-config",
"version": "1.0.1",
"version": "1.1.0",
"description": "Simply set and get configuration from a json file for your Electron app",
"main": "src/index.js",
"keywords": [
Expand All @@ -21,19 +21,19 @@
"url": "de-luca.io"
},
"repository": {
"type" : "git",
"url" : "https://github.com/de-luca/electron-json-config.git"
"type": "git",
"url": "https://github.com/de-luca/electron-json-config.git"
},
"scripts": {
"test": "electron-mocha --recursive test -R progress && electron-mocha --renderer --recursive test -R progress"
},
"license": "BSD-2-Clause",
"dependencies": {
"exists-file": "^1.0.0"
"exists-file": "^1.0.1"
},
"devDependencies": {
"electron-mocha": "^0.8.0",
"electron-prebuilt": "^0.35.6",
"electron-mocha": "^1.2.0",
"electron-prebuilt": "^0.37.6",
"mochainon": "^1.0.0"
},
"files": [
Expand Down
49 changes: 43 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,70 @@ const sync = function() {
fs.writeFileSync(file, JSON.stringify(config));
};

const search = function(object, key) {
let path = key.split('.');
for(let i = 0; i < path.length; i++) {
if(object[path[i]] === undefined) {
return undefined;
}
object = object[path[i]];
}
return object;
};

const set = function(object, key) {
let path = key.split('.');
for(var i = 0; i < path.length - 1; ++i) {
if(!object[path[i]]) {
object[path[i]] = {};
}
object = object[path[i]];
}
return function(object, attribute) {
return function(value) { object[attribute] = value; };
} (object, path[i]);
};

const remove = function(object, key) {
let path = key.split('.');
for(var i = 0; i < path.length - 1; ++i) {
if(!object[path[i]]) {
object[path[i]] = {};
}
object = object[path[i]];
}
return function(object, attribute) {
return function() { delete object[attribute]; };
} (object, path[i]);
};

exports.file = function() {
return file;
};

exports.has = function(key) {
return config.hasOwnProperty(key);
return search(config, key) !== undefined;
};

exports.set = function(key, value) {
config[key] = value;
set(config, key)(value);
sync();
};

exports.get = function(key) {
return config[key];
return search(config, key);
};

exports.keys = function() {
return Object.keys(config);
exports.keys = function(key) {
return Object.keys((key) ? search(config, key) : config);
};

exports.all = function() {
return config;
};

exports.delete = function(key) {
delete config[key];
remove(config, key)();
sync();
};

Expand Down
109 changes: 108 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ it('.set() and .get() a null', function(done) {
done();
});

it('deep .set() and deep .get() a null', function(done) {
config.set('foo.bar', null);
var res = config.get('foo.bar');
m.chai.expect(res).to.be.a('null');
m.chai.expect(res).to.equals(null);
done();
});

it('.set() and .get() an undefined', function(done) {
config.set('foo', undefined);
var res = config.get('foo');
Expand All @@ -29,6 +37,14 @@ it('.set() and .get() an undefined', function(done) {
done();
});

it('deep .set() and deep .get() an undefined', function(done) {
config.set('foo.bar', undefined);
var res = config.get('foo.bar');
m.chai.expect(res).to.be.an('undefined');
m.chai.expect(res).to.equals(undefined);
done();
});

it('.set() and .get() a boolean', function(done) {
config.set('foo', true);
var res = config.get('foo');
Expand All @@ -37,6 +53,14 @@ it('.set() and .get() a boolean', function(done) {
done();
});

it('deep .set() and deep .get() a boolean', function(done) {
config.set('foo.bar', true);
var res = config.get('foo.bar');
m.chai.expect(res).to.be.a('boolean');
m.chai.expect(res).to.equals(true);
done();
});

it('.set() and .get() a string', function(done) {
config.set('foo', 'bar');
var res = config.get('foo');
Expand All @@ -45,6 +69,14 @@ it('.set() and .get() a string', function(done) {
done();
});

it('deep .set() and deep .get() a string', function(done) {
config.set('foo.bar', 'baz');
var res = config.get('foo.bar');
m.chai.expect(res).to.be.a('string');
m.chai.expect(res).to.equals('baz');
done();
});

it('.set() and .get() a number', function(done) {
config.set('foo', 1);
var res = config.get('foo');
Expand All @@ -53,6 +85,14 @@ it('.set() and .get() a number', function(done) {
done();
});

it('deep .set() and deep .get() a number', function(done) {
config.set('foo.bar', 1);
var res = config.get('foo.bar');
m.chai.expect(res).to.be.a('number');
m.chai.expect(res).to.equals(1);
done();
});

it('.set() and .get() an array', function(done) {
config.set('foo', ['bar', 'baz']);
var res = config.get('foo');
Expand All @@ -61,6 +101,14 @@ it('.set() and .get() an array', function(done) {
done();
});

it('deep .set() and deep .get() an array', function(done) {
config.set('foo.bar', ['bar', 'baz']);
var res = config.get('foo.bar');
m.chai.expect(res).to.be.an('array');
m.chai.expect(res).to.deep.equals(['bar', 'baz']);
done();
});

it('.set() and .get() an object', function(done) {
config.set('foo', {bar: true, baz: 42});
var res = config.get('foo');
Expand All @@ -69,6 +117,30 @@ it('.set() and .get() an object', function(done) {
done();
});

it('deep .set() and deep .get() an object', function(done) {
config.set('foo.bar', {bar: true, baz: 42});
var res = config.get('foo.bar');
m.chai.expect(res).to.be.an('object');
m.chai.expect(res).to.deep.equals({bar: true, baz: 42});
done();
});

it('deep .set() and deep .get() an object', function(done) {
//config.set('foo.bar', {bar: true, baz: 42});
var res = config.get('foo.bar.baz');
m.chai.expect(res).to.be.an('undefined');
m.chai.expect(res).to.deep.equals(undefined);
done();
});

it('very deep .set() and very deep .get() an object', function(done) {
config.set('foo.bar.baz.very.deep', {itsdeep: true});
var res = config.get('foo.bar.baz.very.deep');
m.chai.expect(res).to.be.an('object');
m.chai.expect(res).to.deep.equals({itsdeep: true});
done();
});

it('.has()', function(done) {
var res;
config.set('foo', 'bar');
Expand All @@ -81,7 +153,19 @@ it('.has()', function(done) {
done();
});

it('gets .keys()', function(done) {
it('deep .has()', function(done) {
var res;
config.set('foo.bar', 'baz');
res = config.has('foo.bar');
m.chai.expect(res).to.be.a('boolean');
m.chai.expect(res).to.equals(true);
res = config.has('foo.bar.baz');
m.chai.expect(res).to.be.a('boolean');
m.chai.expect(res).to.equals(false);
done();
});

it('gets .keys() from top level', function(done) {
config.set('foo', undefined);
config.set('bar', undefined);
config.set('baz', undefined);
Expand All @@ -92,6 +176,16 @@ it('gets .keys()', function(done) {
done();
});

it('gets .keys() from a sub level', function(done) {
config.set('foo.bar', undefined);
config.set('foo.baz', undefined);
var res = config.keys('foo');
m.chai.expect(res).to.be.an('array');
m.chai.expect(res).to.have.length(2);
m.chai.expect(res).to.deep.equals(['bar', 'baz']);
done();
});

it('gets .all()', function(done) {
config.set('foo', 1);
config.set('bar', 2);
Expand Down Expand Up @@ -119,6 +213,19 @@ it('.delete()', function(done) {
done();
});

it('deep .delete()', function(done) {
var res;
config.set('foo.bar', true);
res = config.get('foo.bar');
m.chai.expect(res).to.be.a('boolean');
m.chai.expect(res).to.equals(true);
config.delete('foo.bar');
res = config.get('foo.bar');
m.chai.expect(res).to.be.an('undefined');
m.chai.expect(res).to.equals(undefined);
done();
});

it('.purge()', function(done) {
var res;
config.set('foo', true);
Expand Down

0 comments on commit ce9601a

Please sign in to comment.