-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
58 lines (47 loc) · 1.54 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const { isPlainObject } = require('is-plain-object');
const minimatch = require('minimatch');
const JSONPATH_SEP = '/';
const isTraversable = (value) => Array.isArray(value) || isPlainObject(value);
const createMatcher = (test) => {
if (typeof test === 'function') {
return test;
}
if (test && test.length) {
const [pattern, opts] = !Array.isArray(test) ? [test] : test;
test = ([path]) => minimatch(path, pattern,opts);
}
return test && test.test ? ([path]) => test.test(path) : test;
};
const formatJsonPath = (...args) => (JSONPATH_SEP + args.filter(Boolean).join(JSONPATH_SEP)).replace(new RegExp(`[${JSONPATH_SEP}]+`), JSONPATH_SEP);
const parseJsonPath = (path) => path.split(new RegExp(`[${JSONPATH_SEP}]+`)).filter(Boolean);
/**
* Wraps a function iteratior to become an iterable
* @param {Function} next
* @returns {Iterable}
*/
const wrapIterator = (next) => ({
next,
[Symbol.iterator]: function() { return this; },
});
const entries = (nested, prefix) => {
const target = [];
const entries = nested && typeof nested === 'object' ? Object.entries(nested) : [];
let i, len;
for(len = entries.length, i = 0; i < len; i++) {
const path = formatJsonPath(prefix, entries[i][0]);
const value = entries[i][1];
const entry = [path, value];
target.push(entry);
}
return target;
};
const parseOptions = (opts) => typeof opts !== 'object' ? ({ test: opts }) : { ...opts };
module.exports = {
isTraversable,
createMatcher,
wrapIterator,
formatJsonPath,
entries,
parseOptions,
parseJsonPath,
};