config: add filter method.

This commit is contained in:
Christopher Jeffrey 2017-12-12 12:06:56 -08:00
parent 5947647ebc
commit 8040600326
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
1 changed files with 34 additions and 0 deletions

View File

@ -145,6 +145,31 @@ class Config {
this.prefix = this.getPrefix();
}
/**
* Create a child config. Filter by plugin name.
* @param {String} name
* @returns {Config}
*/
filter(name) {
assert(typeof name === 'string');
const child = new Config(this.module);
child.prefix = this.prefix;
child.suffix = this.suffix;
child.fallback = this.fallback;
child.argv = this.argv;
child.pass = this.pass;
_filter(name, this.env, child.env);
_filter(name, this.args, child.args);
_filter(name, this.query, child.query);
_filter(name, this.hash, child.hash);
return child;
}
/**
* Set default option.
* @param {String} key
@ -1106,6 +1131,15 @@ function isUpperKey(key) {
return !/[a-z]/.test(key);
}
function _filter(name, a, b) {
for (const key of Object.keys(a)) {
if (key.length > name.length && key.indexOf(name) === 0) {
const sub = key.substring(name.length);
b[sub] = a[key];
}
}
}
function fromFloat(num, exp) {
assert(typeof num === 'number' && isFinite(num));
assert(Number.isSafeInteger(exp));