简体   繁体   中英

node-config change delimiter from dot to colon

Im migrating my config from another library to node-config .

I have two questions:

  1. The old library uses config.get('a:b'); to get some value, but node-config use a single dot as a delimiter: config.get('a.b'); .

Is there is a way to configure it to use : to save my time and refactor my code?

  1. Is there is a way to set a runtime values. eg config.set('key', 'val'); ?

Done it by: 1. wrap node-config in a new js file 2. proxied the get , has and set methods methods

Something like that:

const config = require('config');

const inMemDict = {};

const toNewKey = key => {
    return key && key.split(':').join('.');
};

const { get: origGet, has: origHas } = config;

config.get = function (key, ...args) {
    key = toNewKey(key);

    if(typeof inMemDict[key] !== 'undefined') {
        return inMemDict[key];
    }

    return origGet.apply(config, [key, ...args]);
};

config.has = function (key, ...args) {
    key = toNewKey(key);

    if(typeof inMemDict[key] !== 'undefined') {
        return inMemDict[key];
    }

    return origHas.apply(config, [key, ...args]);
};

config.set = function (key, val) {
    if(!key) return;
    inMemDict[toNewKey(key)] = val;
};

module.exports = config;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM