Skip to content

Commit

Permalink
Merge pull request #12 from de-luca/feature/bulk-manipulation
Browse files Browse the repository at this point in the history
Add bulk manipulation functions
  • Loading branch information
de-luca committed Mar 19, 2017
2 parents 0018b0d + 1110229 commit cf76711
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-json-config",
"version": "1.4.0",
"version": "1.5.0",
"description": "Simply set and get configuration from a json file for your Electron app",
"main": "src/index.js",
"keywords": [
Expand Down
18 changes: 17 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const file = (electron.app || electron.remote.app).getPath('userData')+'/config.
if(!u.exists(file)) {
fs.writeFileSync(file, '{}');
}
var config = JSON.parse(fs.readFileSync(file));

let config = JSON.parse(fs.readFileSync(file));


exports.file = function() {
return file;
Expand All @@ -23,6 +25,13 @@ exports.set = function(key, value) {
u.sync(file, config);
};

exports.setBulk = function(items) {
for (let key in items) {
u.set(config, key)(items[key]);
}
u.sync(file, config);
};

exports.get = function(key, defaultValue) {
const value = u.search(config, key);
return value === undefined ? defaultValue : value;
Expand All @@ -41,6 +50,13 @@ exports.delete = function(key) {
u.sync(file, config);
};

exports.deleteBulk = function(keys) {
for (let key of keys) {
u.remove(config, key)();
}
u.sync(file, config);
};

exports.purge = function() {
config = {};
u.sync(file, config);
Expand Down
97 changes: 96 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
const m = require('mochainon');
const config = require('../src/index.js');

beforeEach(config.purge);

beforeEach(function() {
config.purge();
});


it('.set() and .get() a null', function(done) {
config.set('foo', null);
Expand Down Expand Up @@ -241,3 +245,94 @@ it('.purge()', function(done) {
m.chai.expect(res).to.deep.equals([]);
done();
});


it('.setBulk() multiple values in a single call and .get() them', function(done) {
config.setBulk({
a_boolean: true,
a_string: "foo bar",
an_int: 42,
an_array: ['foo', 'bar'],
"an.object": {
foo: 'bar',
theAnswer: 42,
}
});

let keys = config.keys();
m.chai.expect(keys).to.have.length(5);
m.chai.expect(keys).to.deep.equals([
'a_boolean',
'a_string',
'an_int',
'an_array',
'an'
]);

let deepSettedObject = config.get('an');
m.chai.expect(deepSettedObject).to.deep.equals({
object: {
foo: 'bar',
theAnswer: 42,
}
});

let all = config.all();
m.chai.expect(all).to.deep.equals({
a_boolean: true,
a_string: "foo bar",
an_int: 42,
an_array: ['foo', 'bar'],
an: {
object: {
foo: 'bar',
theAnswer: 42,
}
}
});

done();
});

it('.deleteBulk multiple values in a single call', function(done) {
config.setBulk({
a_boolean: true,
a_string: "foo bar",
an_int: 42,
an_array: ['foo', 'bar'],
"an.object": {
foo: 'bar',
theAnswer: 42,
}
});

config.deleteBulk([
'a_boolean',
'an_int',
'an.object.theAnswer',
]);

let keys = config.keys();
m.chai.expect(keys).to.have.length(3);
m.chai.expect(keys).to.deep.equals([
'a_string',
'an_array',
'an'
]);

let all = config.all();
m.chai.expect(all).to.deep.equals({
a_string: "foo bar",
an_array: ['foo', 'bar'],
an: {
object: {
foo: 'bar'
}
}
});

let foo = config.get('an.object.foo');
m.chai.expect(foo).to.equals('bar');

done();
});

0 comments on commit cf76711

Please sign in to comment.