Skip to content

Commit

Permalink
Added nconf.any method (#278)
Browse files Browse the repository at this point in the history
* Added nconf.any method

Fixes #126
Implemented on the Provider class
Takes an array of keys, or a variable argument list
Supports both callback and non-callback invocations

* Use an explicit search base in file store test

Fixes #224
Test file was created under process.env.HOME, but test was searching in
the current working directory. If the cwd was not on the same drive as
the home directory, the test would fail.

* Added some API documentation to README for 'any'

* Tweak `.any` documentation
  • Loading branch information
mhamann committed Oct 21, 2017
1 parent ca10d0e commit 35088a3
Show file tree
Hide file tree
Showing 6 changed files with 1,474 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -129,6 +129,24 @@ Adds a new store with the specified `name` and `options`. If `options.type` is n
nconf.add('global', { type: 'file', file: '/path/to/globalconf.json' });
```
### nconf.any(names, callback)
Given a set of key names, gets the value of the first key found to be truthy. The key names can be given as separate arguments
or as an array. If the last argument is a function, it will be called with the result; otherwise, the value is returned.
``` js
//
// Get one of 'NODEJS_PORT' and 'PORT' as a return value
//
var port = nconf.any('NODEJS_PORT', 'PORT');

//
// Get one of 'NODEJS_IP' and 'IPADDRESS' using a callback
//
nconf.any(['NODEJS_IP', 'IPADDRESS'], function(err, value) {
console.log('Connect to IP address ' + value);
});
```
### nconf.use(name, options)
Similar to `nconf.add`, except that it can replace an existing store if new options are provided
Expand Down
57 changes: 57 additions & 0 deletions lib/nconf/provider.js
Expand Up @@ -275,6 +275,63 @@ Provider.prototype.get = function (key, callback) {
});
};


//
// ### function any (keys, callback)
// #### @keys {array|string...} Array of keys to query, or a variable list of strings
// #### @callback {function} **Optional** Continuation to respond to when complete.
// Retrieves the first truthy value (if any) for the specified list of keys.
//
Provider.prototype.any = function (keys, callback) {

if (!Array.isArray(keys)) {
keys = Array.prototype.slice.call(arguments);
if (keys.length > 0 && typeof keys[keys.length - 1] === 'function') {
callback = keys.pop();
} else {
callback = null;
}
}

//
// If there is no callback, use the short-circuited "get"
// on each key in turn.
//
if (!callback) {
var val;
for (var i = 0; i < keys.length; ++i) {
val = this._execute('get', 1, keys[i], callback);
if (val) {
return val;
}
}
return null;
}

var keyIndex = 0,
result,
self = this;

async.whilst(function() {
return !result && keyIndex < keys.length;
}, function(next) {
var key = keys[keyIndex];
keyIndex++;

self.get(key, function(err, v) {
if (err) {
next(err);
} else {
result = v;
next();
}
});
}, function(err) {
return err ? callback(err) : callback(null, result);
});
};


//
// ### function set (key, value, callback)
// #### @key {string} Key to set in this instance
Expand Down

0 comments on commit 35088a3

Please sign in to comment.