Total Complexity | 8 |
Complexity/F | 4 |
Lines of Code | 32 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | export default applyOptions; |
||
2 | |||
3 | function applyOptions(optionsBlueprint, options) { |
||
4 | var returnOptions = {}; |
||
5 | for (var name in optionsBlueprint) { |
||
6 | if (optionsBlueprint.hasOwnProperty(name)) { |
||
7 | if (typeof options[name] !== 'undefined') { |
||
8 | returnOptions[name] = convertOption(options[name], optionsBlueprint[name].type); |
||
9 | } |
||
10 | else { |
||
11 | returnOptions[name] = optionsBlueprint[name].default; |
||
12 | } |
||
13 | } |
||
14 | } |
||
15 | return returnOptions; |
||
16 | } |
||
17 | |||
18 | // Convert an option to a specified type |
||
19 | function convertOption(data, type) { |
||
20 | if (type === 'string') { |
||
21 | return '' + data; |
||
22 | } |
||
23 | else if (type === 'number') { |
||
24 | return parseInt(data, 10); |
||
25 | } |
||
26 | else if (type === 'boolean') { |
||
27 | return (typeof data === 'boolean' && data) || data === 'true' || data === 'True'; |
||
28 | } |
||
29 | else { |
||
30 | return data; |
||
31 | } |
||
32 | } |
||
33 |