From 03e9bbb3bc8db998e8276d74f7640189e7d78ee3 Mon Sep 17 00:00:00 2001 From: leschekfm Date: Thu, 10 Mar 2016 09:22:44 +0100 Subject: [PATCH 1/6] require module only once --- test/test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/test.js b/test/test.js index 4f63351..f4ff2b2 100644 --- a/test/test.js +++ b/test/test.js @@ -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 }) @@ -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, @@ -43,7 +44,7 @@ fs.writeFileSync(jsonrc, [ '}' ].join('\n')); -var commentedJSON = require('../')(n, { +var commentedJSON = rc(n, { option: true }) From 18f19cfb45162a3e5190a13835722aab36b7fa5d Mon Sep 17 00:00:00 2001 From: leschekfm Date: Thu, 10 Mar 2016 09:27:57 +0100 Subject: [PATCH 2/6] add test for second param --- test/test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/test.js b/test/test.js index f4ff2b2..58d372d 100644 --- a/test/test.js +++ b/test/test.js @@ -58,3 +58,14 @@ 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 a string as second param throws error +var errorThrown = false; +try { + rc(n, 'asdfas'); +} +catch(err) { + errorThrown = true; +} + +assert(errorThrown, true) From 46400515487abdfa704761472bd1cd44b63be659 Mon Sep 17 00:00:00 2001 From: leschekfm Date: Thu, 10 Mar 2016 09:57:02 +0100 Subject: [PATCH 3/6] add type check --- index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6f8f113..7f46214 100755 --- a/index.js +++ b/index.js @@ -13,10 +13,17 @@ 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 = ( + + // 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') + } + + /*defaults = ( 'string' === typeof defaults ? cc.json(defaults) : defaults - ) || {} + ) || {}*/ parse = parse || cc.parse From ef743ddb7cc99d9d74744d71facb0259e1c90e21 Mon Sep 17 00:00:00 2001 From: leschekfm Date: Thu, 10 Mar 2016 09:57:28 +0100 Subject: [PATCH 4/6] add more types to tests --- test/test.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/test/test.js b/test/test.js index 58d372d..2f16312 100644 --- a/test/test.js +++ b/test/test.js @@ -59,13 +59,18 @@ assert.equal(commentedJSON.config, jsonrc) assert.equal(commentedJSON.configs.length, 1) assert.equal(commentedJSON.configs[0], jsonrc) -// check if passing a string as second param throws error -var errorThrown = false; -try { - rc(n, 'asdfas'); -} -catch(err) { - errorThrown = true; +// check if passing a somthing 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) } -assert(errorThrown, true) From 7014253030582a46aec77558f6957184c6d2ea68 Mon Sep 17 00:00:00 2001 From: leschekfm Date: Thu, 10 Mar 2016 09:59:31 +0100 Subject: [PATCH 5/6] remove unused utils.json() --- index.js | 5 ----- lib/utils.js | 5 ----- 2 files changed, 10 deletions(-) diff --git a/index.js b/index.js index 7f46214..c413ec1 100755 --- a/index.js +++ b/index.js @@ -19,11 +19,6 @@ module.exports = function (name, defaults, argv, parse) { if (typeof defaults !== 'object' || Array.isArray(defaults)) { throw new Error('defaults has to be an object') } - - /*defaults = ( - 'string' === typeof defaults - ? cc.json(defaults) : defaults - ) || {}*/ parse = parse || cc.parse diff --git a/lib/utils.js b/lib/utils.js index ae6dec0..d1530a2 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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 = {} From e1cb4b4a3cbe3cf6626ab58b6b2b98fd086c2779 Mon Sep 17 00:00:00 2001 From: leschekfm Date: Thu, 10 Mar 2016 10:05:31 +0100 Subject: [PATCH 6/6] fix typo --- test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index 2f16312..42b8c2d 100644 --- a/test/test.js +++ b/test/test.js @@ -59,7 +59,7 @@ assert.equal(commentedJSON.config, jsonrc) assert.equal(commentedJSON.configs.length, 1) assert.equal(commentedJSON.configs[0], jsonrc) -// check if passing a somthing other than an object as second param throws error +// 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++) {