Skip to content

Commit

Permalink
add config.getInt(filename, default_value) (#44)
Browse files Browse the repository at this point in the history
* remove ; from function declarations
* add config.getInt(filename, default_value)
  • Loading branch information
msimerson committed Nov 5, 2018
1 parent f501adc commit c3aab47
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
engines:
engines:
eslint:
enabled: true
channel: "eslint-3"
Expand Down
5 changes: 5 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

## 1.0.16 - 2018-11-02

- remove trailing ; from function declarations
- add config.getInt(filename, default_value)

## 1.0.15 - 2017-09-21

- additional test for 'missing json loads yaml'
Expand Down
15 changes: 13 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,22 @@ Config.prototype.get = function (name, type, cb, options) {
}

return results;
};
}

Config.prototype.getInt = function (filename, default_value) {

if (!filename) return NaN;

const full_path = path.resolve(this.root_path, filename);
const r = parseInt(cfreader.read_config(full_path, 'value', null, null), 10);

if (!isNaN(r)) return r;
return parseInt(default_value, 10);
}

Config.prototype.getDir = function (name, opts, done) {
cfreader.read_dir(path.resolve(this.root_path, name), opts, done);
};
}

function merge_config (defaults, overrides, type) {
switch (type) {
Expand Down
18 changes: 9 additions & 9 deletions configfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ cfreader.on_watch_event = function (name, type, options, cb) {
}
}
};
};
}

cfreader.watch_dir = function () {
// NOTE: Has OS platform limitations:
Expand Down Expand Up @@ -137,7 +137,7 @@ cfreader.watch_dir = function () {
console.error(`Error watching directory ${cp}(${e})`);
}
return;
};
}

cfreader.watch_file = function (name, type, cb, options) {
// This works on all OS's, but watch_dir() above is preferred for Linux and
Expand All @@ -161,7 +161,7 @@ cfreader.watch_file = function (name, type, cb, options) {
}
}
return;
};
}

cfreader.get_cache_key = function (name, options) {

Expand Down Expand Up @@ -221,7 +221,7 @@ cfreader.read_config = function (name, type, cb, options) {
}

return result;
};
}

function isDirectory (filepath) {
return new Promise(function (resolve, reject) {
Expand Down Expand Up @@ -287,7 +287,7 @@ cfreader.read_dir = function (name, opts, done) {
})

if (opts.watchCb) fsWatchDir(name);
};
}

cfreader.ensure_enoent_timer = function () {
if (cfreader._enoent_timer) return;
Expand All @@ -314,7 +314,7 @@ cfreader.ensure_enoent_timer = function () {
}
}, 60 * 1000);
cfreader._enoent_timer.unref(); // This shouldn't block exit
};
}

cfreader.get_filetype_reader = function (type) {
switch (type) {
Expand All @@ -325,7 +325,7 @@ cfreader.get_filetype_reader = function (type) {
return require('./readers/flat');
}
return require(`./readers/${type}`);
};
}

cfreader.load_config = function (name, type, options) {
let result;
Expand Down Expand Up @@ -378,7 +378,7 @@ cfreader.load_config = function (name, type, options) {
return cfrType.empty(options, type);
}
return result;
};
}

cfreader.process_file_overrides = function (name, options, result) {
// We might be re-loading this file:
Expand All @@ -404,4 +404,4 @@ cfreader.process_file_overrides = function (name, options, result) {
console.log(`Overriding file ${fn} with config from ${name}`);
cfreader._config_cache[path.join(cp, fn)] = result[keys[j]];
}
};
}
6 changes: 3 additions & 3 deletions readers/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require('fs');

exports.load = function (name) {
return fs.readFileSync(name);
};
}

exports.loadPromise = function (name) {
return new Promise(function (resolve, reject) {
Expand All @@ -13,8 +13,8 @@ exports.loadPromise = function (name) {
resolve({ path: name, data: content });
});
});
};
}

exports.empty = function () {
return null;
};
}
4 changes: 2 additions & 2 deletions readers/flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ exports.load = function (name, type, options, regex) {
}

return result;
};
}

exports.empty = function (options, type) {
if (type) {
if (type === 'flat') { return null; }
if (type === 'value') { return null; }
}
return [];
};
}

function in_array (item, array) {
if (!array) return false;
Expand Down
4 changes: 2 additions & 2 deletions readers/hjson.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const hjson = require('hjson');

exports.load = function (name) {
return hjson.parse(fs.readFileSync(name, "utf8"));
};
}

exports.empty = function () {
return {};
};
}
8 changes: 4 additions & 4 deletions readers/ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ exports.load = function (name, options, regex) {
});

return result;
};
}

exports.empty = function (options) {
this.bool_matches = [];
return this.init_booleans(options, { main: {} });
};
}

exports.init_booleans = function (options, result) {
if (!options) return result;
Expand Down Expand Up @@ -119,9 +119,9 @@ exports.init_booleans = function (options, result) {
}

return result;
};
}

exports.logger = function (msg, level) {
// if (!level) level = 'logwarn';
console.log(msg);
};
}
4 changes: 2 additions & 2 deletions readers/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const fs = require('fs');

exports.load = function (name) {
return JSON.parse(fs.readFileSync(name));
};
}

exports.empty = function () {
return {};
};
}
4 changes: 2 additions & 2 deletions readers/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const yaml = require('js-yaml');

exports.load = function (name) {
return yaml.safeLoad(fs.readFileSync(name, 'utf8'));
};
}

exports.empty = function () {
return {};
};
}
53 changes: 46 additions & 7 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ exports.config = {
test.equal(c.overrides_path, path.join('bar','config'));
test.done();
},
};
}

exports.config_path = {
'config_path process.env.HARAKA': function (test) {
Expand All @@ -64,7 +64,7 @@ exports.config_path = {
process.env.NODE_ENV = 'test';
test.done();
},
};
}

exports.arrange_args = {
'setUp' : setUp,
Expand Down Expand Up @@ -180,7 +180,7 @@ exports.arrange_args = {
['test.ini', 'data', cb, opts]);
test.done();
},
};
}

const hjsonRes = {
matt: 'waz here and also made comments',
Expand All @@ -189,13 +189,13 @@ const hjsonRes = {
'has a property one': 'with a value A',
'has a property two': 'with a value B'
}
};
}

const jsonRes = {
matt: 'waz here',
array: [ 'has an element' ],
objecty: { 'has a property': 'with a value' }
};
}

const yamlRes = {
main: {
Expand All @@ -219,7 +219,7 @@ const yamlRes = {
objecty: {
'has a property': 'with a value'
}
};
}

function _test_get (test, name, type, callback, options, expected) {
test.expect(1);
Expand All @@ -229,6 +229,17 @@ function _test_get (test, name, type, callback, options, expected) {
test.done();
}

function _test_int (test, name, default_value, expected) {
test.expect(2);
const config = require('../config');
const result = config.getInt(name, default_value);
if (result) {
test.equal(typeof result, 'number');
}
test.deepEqual(result, expected);
test.done();
}

exports.get = {
'setUp' : setUp,
// config.get('name');
Expand Down Expand Up @@ -356,7 +367,7 @@ exports.get = {
test.ok(Buffer.isBuffer(res));
test.done();
},
};
}

exports.merged = {
'setUp' : setUp,
Expand Down Expand Up @@ -392,6 +403,34 @@ exports.merged = {
},
}

exports.getInt = {
'setUp' : setUp,
// config.get('name');
'empty filename is NaN' : function (test) {
test.expect(2);
const result = this.config.getInt();
test.equal(typeof result, 'number');
test.ok(isNaN(result));
test.done();
},
'empty/missing file contents is NaN' : function (test) {
test.expect(2);
const result = this.config.getInt('test-non-exist');
test.equal(typeof result, 'number');
test.ok(isNaN(result));
test.done();
},
'non-existing file returns default' : function (test) {
_test_int(test, 'test-non-exist', 5, 5);
},
'test.int equals 6' : function (test) {
_test_int(test, 'test.int', undefined, 6);
},
'test.int equals 6 (with default 7)' : function (test) {
_test_int(test, 'test.int', 7, 6);
},
}

const tmpFile = path.resolve('test', 'config', 'dir', '4.ext');

function cleanup (done) {
Expand Down
1 change: 1 addition & 0 deletions test/config/test.int
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6
6 changes: 3 additions & 3 deletions test/readers/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

const fs = require('fs');

const _set_up = function (done) {
function _set_up (done) {
this.bin = require('../../readers/binary');
done();
};
}

exports.load = {
setUp : _set_up,
Expand All @@ -28,4 +28,4 @@ exports.load = {
test.deepEqual(result, fs.readFileSync(testBin));
test.done();
},
};
}
6 changes: 3 additions & 3 deletions test/readers/flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

const regex = require('../../configfile').regex;

const _set_up = function (done) {
function _set_up (done) {
this.flat = require('../../readers/flat');
done();
};
}

exports.load = {
setUp: _set_up,
Expand Down Expand Up @@ -40,4 +40,4 @@ exports.load = {
test.ok(result);
test.done();
},
};
}
6 changes: 3 additions & 3 deletions test/readers/hjson.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const _set_up = function (done) {
function _set_up (done) {
this.hjson = require('../../readers/hjson');
done();
};
}

exports.load = {
setUp : _set_up,
Expand All @@ -27,4 +27,4 @@ exports.load = {
test.ok(result.object['has a property two']);
test.done();
},
};
}
Loading

0 comments on commit c3aab47

Please sign in to comment.