.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / remark-stringify / lib / set-options.js
1 'use strict';
2
3 var xtend = require('xtend');
4 var encode = require('stringify-entities');
5 var defaults = require('./defaults');
6 var escapeFactory = require('./escape');
7 var returner = require('./util/returner');
8
9 module.exports = setOptions;
10
11 /* Map of applicable enum's. */
12 var maps = {
13   entities: {true: true, false: true, numbers: true, escape: true},
14   bullet: {'*': true, '-': true, '+': true},
15   rule: {'-': true, _: true, '*': true},
16   listItemIndent: {tab: true, mixed: true, 1: true},
17   emphasis: {_: true, '*': true},
18   strong: {_: true, '*': true},
19   fence: {'`': true, '~': true}
20 };
21
22 /* Expose `validate`. */
23 var validate = {
24   boolean: validateBoolean,
25   string: validateString,
26   number: validateNumber,
27   function: validateFunction
28 };
29
30 /* Set options.  Does not overwrite previously set
31  * options. */
32 function setOptions(options) {
33   var self = this;
34   var current = self.options;
35   var ruleRepetition;
36   var key;
37
38   if (options == null) {
39     options = {};
40   } else if (typeof options === 'object') {
41     options = xtend(options);
42   } else {
43     throw new Error('Invalid value `' + options + '` for setting `options`');
44   }
45
46   for (key in defaults) {
47     validate[typeof defaults[key]](options, key, current[key], maps[key]);
48   }
49
50   ruleRepetition = options.ruleRepetition;
51
52   if (ruleRepetition && ruleRepetition < 3) {
53     raise(ruleRepetition, 'options.ruleRepetition');
54   }
55
56   self.encode = encodeFactory(String(options.entities));
57   self.escape = escapeFactory(options);
58
59   self.options = options;
60
61   return self;
62 }
63
64 /* Throw an exception with in its `message` `value`
65  * and `name`. */
66 function raise(value, name) {
67   throw new Error('Invalid value `' + value + '` for setting `' + name + '`');
68 }
69
70 /* Validate a value to be boolean. Defaults to `def`.
71  * Raises an exception with `context[name]` when not
72  * a boolean. */
73 function validateBoolean(context, name, def) {
74   var value = context[name];
75
76   if (value == null) {
77     value = def;
78   }
79
80   if (typeof value !== 'boolean') {
81     raise(value, 'options.' + name);
82   }
83
84   context[name] = value;
85 }
86
87 /* Validate a value to be boolean. Defaults to `def`.
88  * Raises an exception with `context[name]` when not
89  * a boolean. */
90 function validateNumber(context, name, def) {
91   var value = context[name];
92
93   if (value == null) {
94     value = def;
95   }
96
97   if (isNaN(value)) {
98     raise(value, 'options.' + name);
99   }
100
101   context[name] = value;
102 }
103
104 /* Validate a value to be in `map`. Defaults to `def`.
105  * Raises an exception with `context[name]` when not
106  * in `map`. */
107 function validateString(context, name, def, map) {
108   var value = context[name];
109
110   if (value == null) {
111     value = def;
112   }
113
114   value = String(value);
115
116   if (!(value in map)) {
117     raise(value, 'options.' + name);
118   }
119
120   context[name] = value;
121 }
122
123 /* Validate a value to be function. Defaults to `def`.
124  * Raises an exception with `context[name]` when not
125  * a function. */
126 function validateFunction(context, name, def) {
127   var value = context[name];
128
129   if (value == null) {
130     value = def;
131   }
132
133   if (typeof value !== 'function') {
134     raise(value, 'options.' + name);
135   }
136
137   context[name] = value;
138 }
139
140 /* Factory to encode HTML entities.
141  * Creates a no-operation function when `type` is
142  * `'false'`, a function which encodes using named
143  * references when `type` is `'true'`, and a function
144  * which encodes using numbered references when `type` is
145  * `'numbers'`. */
146 function encodeFactory(type) {
147   var options = {};
148
149   if (type === 'false') {
150     return returner;
151   }
152
153   if (type === 'true') {
154     options.useNamedReferences = true;
155   }
156
157   if (type === 'escape') {
158     options.escapeOnly = true;
159     options.useNamedReferences = true;
160   }
161
162   return wrapped;
163
164   /* Encode HTML entities using the bound options. */
165   function wrapped(value) {
166     return encode(value, options);
167   }
168 }