-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
102 lines (84 loc) · 2.09 KB
/
index.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*!
* glob-object <https:/jonschlinkert/glob-object>
*
* Copyright (c) 2015-2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var stringify = require('stringify-keys');
var unique = require('arr-unique');
var clone = require('clone-deep');
var unset = require('unset-value');
var get = require('get-value');
var set = require('set-value');
var mm = require('micromatch');
function globObject(patterns, obj, options) {
var cloned = clone(obj);
var keys = stringify(cloned, '/');
patterns = arrayify(patterns).map(toSlashes);
var negated = false;
for (var i = 0; i < patterns.length; i++) {
var pattern = patterns[i];
if (pattern.charAt(0) === '!') {
negated = true;
patterns.splice(1, i);
var m = mm(keys, pattern.slice(1), options);
var len = m.length;
var idx = -1;
while (++idx < len) {
unset(cloned, toDots(m[idx]));
}
}
}
// re-create keys if any negation patterns were passed
if (negated) {
keys = stringify(cloned, '/');
}
var matches = mm(keys, patterns, options);
return matches.reduce(function(acc, path) {
var key = toDots(path);
set(acc, key, get(cloned, key));
return acc;
}, {});
}
function reducePaths(paths) {
for (var i = 0; i < paths.length; i++) {
var key = paths[i];
var arr = removeElement(paths, key);
var diff = paths.length - arr.length;
if (diff > 0) {
paths = arr;
}
}
return unique(paths).sort();
}
function removeElement(paths, key) {
var arr = paths.slice();
var len = arr.length;
var idx = -1;
while (++idx < len) {
var fp = arr[idx];
if (fp.indexOf(key) === 0 && fp !== key) {
arr.splice(arr.indexOf(fp), 1);
len--;
}
}
return arr;
}
function toSlashes(key) {
return key.split('.').join('/');
}
function toDots(key) {
return key.split('/').join('.');
}
function arrayify(val) {
return val ? (Array.isArray(val) ? val : [val]) : [];
}
/**
* Expose `reducePaths` for unit tests
*/
globObject.reducePaths = reducePaths;
/**
* Expose `globObject`
*/
module.exports = globObject;