Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check type of defaults param #77

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ module.exports = function (name, defaults, argv, parse) {
throw new Error('rc(name): name *must* be string')
if(!argv)
argv = require('minimist')(process.argv.slice(2))
defaults = (
'string' === typeof defaults
? cc.json(defaults) : defaults
) || {}

// make sure defaults has the right type
defaults = defaults || {};
if (typeof defaults !== 'object' || Array.isArray(defaults)) {
throw new Error('defaults has to be an object')
}

parse = parse || cc.parse

Expand Down
5 changes: 0 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ var file = exports.file = function () {
}
}

var json = exports.json = function () {
var content = file.apply(null, arguments)
return content ? parse(content) : null
}

var env = exports.env = function (prefix, env) {
env = env || process.env
var obj = {}
Expand Down
23 changes: 20 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var rc = require('../');

var n = 'rc'+Math.random()
var assert = require('assert')

process.env[n+'_envOption'] = 42

var config = require('../')(n, {
var config = rc(n, {
option: true
})

Expand All @@ -13,7 +14,7 @@ console.log(config)
assert.equal(config.option, true)
assert.equal(config.envOption, 42)

var customArgv = require('../')(n, {
var customArgv = rc(n, {
option: true
}, { // nopt-like argv
option: false,
Expand Down Expand Up @@ -43,7 +44,7 @@ fs.writeFileSync(jsonrc, [
'}'
].join('\n'));

var commentedJSON = require('../')(n, {
var commentedJSON = rc(n, {
option: true
})

Expand All @@ -57,3 +58,19 @@ assert.equal(commentedJSON.envOption, 42)
assert.equal(commentedJSON.config, jsonrc)
assert.equal(commentedJSON.configs.length, 1)
assert.equal(commentedJSON.configs[0], jsonrc)

// check if passing something other than an object as second param throws error
errorWorthyTypes = ['asdf', 1, [], function() { }]

for (var i = 0; i < errorWorthyTypes.length; i++) {
var errorThrown = false;
try {
rc(n, errorWorthyTypes[i]);
}
catch (err) {
errorThrown = true;
}

assert(errorThrown, true)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't that be assert.equal(errorThrown, true) or just assert(errorThrown)?

}