.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / node_modules / map-obj / index.js
1 'use strict';
2
3 // customized for this use-case
4 const isObject = x =>
5         typeof x === 'object' &&
6         x !== null &&
7         !(x instanceof RegExp) &&
8         !(x instanceof Error) &&
9         !(x instanceof Date);
10
11 module.exports = function mapObj(obj, fn, opts, seen) {
12         opts = Object.assign({
13                 deep: false,
14                 target: {}
15         }, opts);
16
17         seen = seen || new WeakMap();
18
19         if (seen.has(obj)) {
20                 return seen.get(obj);
21         }
22
23         seen.set(obj, opts.target);
24
25         const target = opts.target;
26         delete opts.target;
27
28         for (const key of Object.keys(obj)) {
29                 const val = obj[key];
30                 const res = fn(key, val, obj);
31                 let newVal = res[1];
32
33                 if (opts.deep && isObject(newVal)) {
34                         if (Array.isArray(newVal)) {
35                                 newVal = newVal.map(x => isObject(x) ? mapObj(x, fn, opts, seen) : x);
36                         } else {
37                                 newVal = mapObj(newVal, fn, opts, seen);
38                         }
39                 }
40
41                 target[res[0]] = newVal;
42         }
43
44         return target;
45 };