.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / minimist-options / index.js
1 'use strict';
2
3 const isPlainObject = require('is-plain-obj');
4 const arrify = require('arrify');
5
6 const push = (obj, prop, value) => {
7         if (!obj[prop]) {
8                 obj[prop] = [];
9         }
10
11         obj[prop].push(value);
12 };
13
14 const insert = (obj, prop, key, value) => {
15         if (!obj[prop]) {
16                 obj[prop] = {};
17         }
18
19         obj[prop][key] = value;
20 };
21
22 const passthroughOptions = ['stopEarly', 'unknown', '--'];
23
24 module.exports = options => {
25         options = options || {};
26
27         const result = {};
28
29         passthroughOptions.forEach(key => {
30                 if (options[key]) {
31                         result[key] = options[key];
32                 }
33         });
34
35         Object.keys(options).forEach(key => {
36                 let value = options[key];
37
38                 if (key === 'arguments') {
39                         key = '_';
40                 }
41
42                 // If short form is used
43                 // convert it to long form
44                 // e.g. { 'name': 'string' }
45                 if (typeof value === 'string') {
46                         value = {type: value};
47                 }
48
49                 if (isPlainObject(value)) {
50                         const props = value;
51
52                         if (props.type) {
53                                 const type = props.type;
54
55                                 if (type === 'string') {
56                                         push(result, 'string', key);
57                                 }
58
59                                 if (type === 'boolean') {
60                                         push(result, 'boolean', key);
61                                 }
62                         }
63
64                         const aliases = arrify(props.alias);
65
66                         aliases.forEach(alias => {
67                                 insert(result, 'alias', alias, key);
68                         });
69
70                         if ({}.hasOwnProperty.call(props, 'default')) {
71                                 insert(result, 'default', key, props.default);
72                         }
73                 }
74         });
75
76         return result;
77 };