config: add filter method.
This commit is contained in:
parent
5947647ebc
commit
8040600326
|
@ -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));
|
||||
|
|
Loading…
Reference in New Issue