Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / debug / src / common.js
1
2 /**
3  * This is the common logic for both the Node.js and web browser
4  * implementations of `debug()`.
5  */
6
7 function setup(env) {
8         createDebug.debug = createDebug;
9         createDebug.default = createDebug;
10         createDebug.coerce = coerce;
11         createDebug.disable = disable;
12         createDebug.enable = enable;
13         createDebug.enabled = enabled;
14         createDebug.humanize = require('ms');
15         createDebug.destroy = destroy;
16
17         Object.keys(env).forEach(key => {
18                 createDebug[key] = env[key];
19         });
20
21         /**
22         * The currently active debug mode names, and names to skip.
23         */
24
25         createDebug.names = [];
26         createDebug.skips = [];
27
28         /**
29         * Map of special "%n" handling functions, for the debug "format" argument.
30         *
31         * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
32         */
33         createDebug.formatters = {};
34
35         /**
36         * Selects a color for a debug namespace
37         * @param {String} namespace The namespace string for the for the debug instance to be colored
38         * @return {Number|String} An ANSI color code for the given namespace
39         * @api private
40         */
41         function selectColor(namespace) {
42                 let hash = 0;
43
44                 for (let i = 0; i < namespace.length; i++) {
45                         hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
46                         hash |= 0; // Convert to 32bit integer
47                 }
48
49                 return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
50         }
51         createDebug.selectColor = selectColor;
52
53         /**
54         * Create a debugger with the given `namespace`.
55         *
56         * @param {String} namespace
57         * @return {Function}
58         * @api public
59         */
60         function createDebug(namespace) {
61                 let prevTime;
62                 let enableOverride = null;
63
64                 function debug(...args) {
65                         // Disabled?
66                         if (!debug.enabled) {
67                                 return;
68                         }
69
70                         const self = debug;
71
72                         // Set `diff` timestamp
73                         const curr = Number(new Date());
74                         const ms = curr - (prevTime || curr);
75                         self.diff = ms;
76                         self.prev = prevTime;
77                         self.curr = curr;
78                         prevTime = curr;
79
80                         args[0] = createDebug.coerce(args[0]);
81
82                         if (typeof args[0] !== 'string') {
83                                 // Anything else let's inspect with %O
84                                 args.unshift('%O');
85                         }
86
87                         // Apply any `formatters` transformations
88                         let index = 0;
89                         args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, format) => {
90                                 // If we encounter an escaped % then don't increase the array index
91                                 if (match === '%%') {
92                                         return '%';
93                                 }
94                                 index++;
95                                 const formatter = createDebug.formatters[format];
96                                 if (typeof formatter === 'function') {
97                                         const val = args[index];
98                                         match = formatter.call(self, val);
99
100                                         // Now we need to remove `args[index]` since it's inlined in the `format`
101                                         args.splice(index, 1);
102                                         index--;
103                                 }
104                                 return match;
105                         });
106
107                         // Apply env-specific formatting (colors, etc.)
108                         createDebug.formatArgs.call(self, args);
109
110                         const logFn = self.log || createDebug.log;
111                         logFn.apply(self, args);
112                 }
113
114                 debug.namespace = namespace;
115                 debug.useColors = createDebug.useColors();
116                 debug.color = createDebug.selectColor(namespace);
117                 debug.extend = extend;
118                 debug.destroy = createDebug.destroy; // XXX Temporary. Will be removed in the next major release.
119
120                 Object.defineProperty(debug, 'enabled', {
121                         enumerable: true,
122                         configurable: false,
123                         get: () => enableOverride === null ? createDebug.enabled(namespace) : enableOverride,
124                         set: v => {
125                                 enableOverride = v;
126                         }
127                 });
128
129                 // Env-specific initialization logic for debug instances
130                 if (typeof createDebug.init === 'function') {
131                         createDebug.init(debug);
132                 }
133
134                 return debug;
135         }
136
137         function extend(namespace, delimiter) {
138                 const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
139                 newDebug.log = this.log;
140                 return newDebug;
141         }
142
143         /**
144         * Enables a debug mode by namespaces. This can include modes
145         * separated by a colon and wildcards.
146         *
147         * @param {String} namespaces
148         * @api public
149         */
150         function enable(namespaces) {
151                 createDebug.save(namespaces);
152
153                 createDebug.names = [];
154                 createDebug.skips = [];
155
156                 let i;
157                 const split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
158                 const len = split.length;
159
160                 for (i = 0; i < len; i++) {
161                         if (!split[i]) {
162                                 // ignore empty strings
163                                 continue;
164                         }
165
166                         namespaces = split[i].replace(/\*/g, '.*?');
167
168                         if (namespaces[0] === '-') {
169                                 createDebug.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
170                         } else {
171                                 createDebug.names.push(new RegExp('^' + namespaces + '$'));
172                         }
173                 }
174         }
175
176         /**
177         * Disable debug output.
178         *
179         * @return {String} namespaces
180         * @api public
181         */
182         function disable() {
183                 const namespaces = [
184                         ...createDebug.names.map(toNamespace),
185                         ...createDebug.skips.map(toNamespace).map(namespace => '-' + namespace)
186                 ].join(',');
187                 createDebug.enable('');
188                 return namespaces;
189         }
190
191         /**
192         * Returns true if the given mode name is enabled, false otherwise.
193         *
194         * @param {String} name
195         * @return {Boolean}
196         * @api public
197         */
198         function enabled(name) {
199                 if (name[name.length - 1] === '*') {
200                         return true;
201                 }
202
203                 let i;
204                 let len;
205
206                 for (i = 0, len = createDebug.skips.length; i < len; i++) {
207                         if (createDebug.skips[i].test(name)) {
208                                 return false;
209                         }
210                 }
211
212                 for (i = 0, len = createDebug.names.length; i < len; i++) {
213                         if (createDebug.names[i].test(name)) {
214                                 return true;
215                         }
216                 }
217
218                 return false;
219         }
220
221         /**
222         * Convert regexp to namespace
223         *
224         * @param {RegExp} regxep
225         * @return {String} namespace
226         * @api private
227         */
228         function toNamespace(regexp) {
229                 return regexp.toString()
230                         .substring(2, regexp.toString().length - 2)
231                         .replace(/\.\*\?$/, '*');
232         }
233
234         /**
235         * Coerce `val`.
236         *
237         * @param {Mixed} val
238         * @return {Mixed}
239         * @api private
240         */
241         function coerce(val) {
242                 if (val instanceof Error) {
243                         return val.stack || val.message;
244                 }
245                 return val;
246         }
247
248         /**
249         * XXX DO NOT USE. This is a temporary stub function.
250         * XXX It WILL be removed in the next major release.
251         */
252         function destroy() {
253                 console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
254         }
255
256         createDebug.enable(createDebug.load());
257
258         return createDebug;
259 }
260
261 module.exports = setup;