-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
41 lines (32 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';
const figgyPudding = require('figgy-pudding');
module.exports = butterscotchPudding;
function butterscotchPudding (options) {
const spec = Object.keys(options).reduce((obj, key) => {
const cfg = options[key];
obj[key] = {};
if ('default' in cfg) {
// propagate explicit defaults
obj[key].default = cfg.default;
} else if (cfg.defaultDescription && cfg.type) {
// default descriptions _without_ type must _remain_ undefined
if (cfg.type === 'boolean' && /^(true|false)$/.test(cfg.defaultDescription)) {
obj[key].default = JSON.parse(cfg.defaultDescription);
} else if (cfg.type === 'number' && !isNaN(cfg.defaultDescription)) {
obj[key].default = Number(cfg.defaultDescription);
}
} else if (cfg.type === 'array') {
// yargs does this, but why not
obj[key].default = [];
}
if (key.indexOf('-') > -1) {
// back-compat for durable camelOptions
obj[camelize(key)] = key;
}
return obj;
}, {});
return figgyPudding(spec);
}
function camelize (str) {
return str.replace(/-(\w)/g, (m, p1) => p1.toUpperCase());
}