Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add tests

  • Loading branch information...
commit 6f3fd0fea7270f16121d487b61feb66f41d34818 1 parent 9b039de
authored September 26, 2012

Showing 1 changed file with 25 additions and 15 deletions. Show diff stats Hide diff stats

  1. 40  pkgconfig.js
40  pkgconfig.js
@@ -90,33 +90,43 @@ function readFile(pathname) {
90 90
         } else if (isFile(pathname + '.json')) {
91 91
             return readJsonFile(pathname + '.json');
92 92
         } else {
93  
-            msg = ' (tried .js and .json)'; // note leading space
  93
+            msg = '(.js|.json)';
94 94
         }
95 95
     }
96  
-    throw new Error(strformat("File not found '{0}'{1}", pathname, msg));
  96
+    throw new Error(strformat("File not found '{0}{1}'", pathname, msg));
97 97
 }
98 98
 
99 99
 /**
100  
- * The function exported by this module.
  100
+ * Process the options parameter and return a valid options object.
101 101
  */
102  
-function pkgconfig(options) {
103  
-    if (typeof options === 'undefined') {
104  
-        options = {};
  102
+function processOptions(options) {
  103
+    var opt;
  104
+    if (typeof options === 'object') {
  105
+        opt = options;
105 106
     } else if (typeof options === 'string') {
106  
-        options = {config: options};
107  
-    } else if (typeof options !== 'object') {
  107
+        opt = {config: options};
  108
+    } else if (typeof options === 'undefined') {
  109
+        opt = {}; // use default values in options schema
  110
+    } else {
108 111
         throw new TypeError('options must be an object or a string');
109 112
     }
110  
-    options = jsvutil.validate(options, OPTIONS_SCHEMA);
  113
+    return jsvutil.validate(opt, OPTIONS_SCHEMA);
  114
+}
  115
+
  116
+/**
  117
+ * The function exported by this module.
  118
+ */
  119
+function pkgconfig(options) {
  120
+    var opt = processOptions(options);
111 121
     var pkginfo = findpkg(module.parent);
112  
-    if (typeof options.schema === 'string') {
113  
-        options.schema = readFile(pkginfo.resolve(options.schema));
  122
+    if (typeof opt.schema === 'string') {
  123
+        opt.schema = readFile(pkginfo.resolve(opt.schema));
114 124
     }
115  
-    if (typeof options.config === 'string') {
116  
-        options.config = readFile(pkginfo.resolve(options.config));
  125
+    if (typeof opt.config === 'string') {
  126
+        opt.config = readFile(pkginfo.resolve(opt.config));
117 127
     }
118  
-    jsvutil.check(options.schema);
119  
-    return jsvutil.validate(options.config, options.schema);
  128
+    jsvutil.check(opt.schema);
  129
+    return jsvutil.validate(opt.config, opt.schema);
120 130
 }
121 131
 
122 132
 module.exports = pkgconfig;

0 notes on commit 6f3fd0f

Please sign in to comment.
Something went wrong with that request. Please try again.