common.js | |
---|---|
/*
* utils.js: Utility functions for the nconf module.
*
* (C) 2011, Charlie Robbins
*
*/
var fs = require('fs'),
async = require('async'),
formats = require('./formats'),
Memory = require('./stores/Memory').Memory;
var common = exports; | |
function path (key)@key {string} The ':' delimited key to splitReturns a fully-qualified path to a nested nconf key. | common.path = function (key) {
return key.split(':');
}; |
function key (arguments)Returns a | common.key = function () {
return Array.prototype.slice.call(arguments).join(':');
}; |
function loadFiles (files, callback)@files {Object|Array} List of files (or settings object) to load.@callback {function} Continuation to respond to when complete.Loads all the data in the specified | common.loadFiles = function (files, callback) {
if (!files) {
return callback(null, {});
}
var options = Array.isArray(files) ? { files: files } : files; |
Set the default JSON format if not already specified | options.format = options.format || formats.json;
function parseFile (file, next) {
fs.readFile(file, function (err, data) {
return !err
? next(null, options.format.parse(data.toString()))
: next(err);
});
}
async.map(files, parseFile, function (err, objs) {
return err ? callback(err) : callback(null, common.merge(objs));
});
}; |
function loadFilesSync (files)@files {Object|Array} List of files (or settings object) to load.Loads all the data in the specified | common.loadFilesSync = function (files) {
if (!files) {
return;
} |
Set the default JSON format if not already specified | var options = Array.isArray(files) ? { files: files } : files;
options.format = options.format || formats.json;
return common.merge(files.map(function (file) {
return options.format.parse(fs.readFileSync(file, 'utf8'));
}));
}; |
function merge (objs)@objs {Array} Array of object literals to mergeMerges the specified | common.merge = function (objs) {
var store = new Memory();
objs.forEach(function (obj) {
Object.keys(obj).forEach(function (key) {
store.merge(key, obj[key]);
});
});
return store.store;
}; |
function capitalize (str)@str {string} String to capitalizeCapitalizes the specified | common.capitalize = function (str) {
return str && str[0].toUpperCase() + str.slice(1);
};
|