-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (60 loc) · 2.07 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
"use strict";
var global, exports;
var Minimatch = require("minimatch").Minimatch
function isEnumerable(value) {
var type = typeof value;
return !!value && (type == 'object' || type == 'function') && (type != 'string');
}
function anyMatch(path, matchArray) {
if (!matchArray) return false;
return matchArray.some(function(m){return m.match(path);});
/*return matchArray.some(function(m){
var r = m.match(path);
console.log(m.pattern +" = "+path +" -> "+r);
return r;
});*/
}
// for each path,
// if it matches a not-patten, skip it
// if it is not enumerable (array or object)
// - if it matches a pattern, include it
// - else skip
// on leaving an enumerable, if it is empty and not an array, remove it
function filterREC(keeps, rejects, input) {
var descend = function descendREC(o, p) {
if (!isEnumerable(o)) {
if (anyMatch(p,keeps)) {
return o;
} else {
return undefined;
}
}
var result = Array.isArray(o) ? ([]) : ({});
var added = false;
Object.keys(o).forEach(function(k){
var npath = (p)?(p+"/"+k):(k);
if (!anyMatch(npath, rejects)) {
var children = descendREC(o[k], npath);
if (children !== undefined) {
added = true;
result[k] = children;
}
}
});
return (added) ? (result) : (undefined);
}
return descend(input);
}
(function (provides) {
provides.filter = function filter(patterns, input){
var keeps = [];
var rejects = [];
patterns.forEach(function (p) {
if (p.length < 1) return;
if (p[0] === '!') {rejects.push(new Minimatch(p.substr(1)));}
else {keeps.push(new Minimatch(p));}
});
return filterREC(keeps, rejects, input);
};
/* istanbul ignore next */ // `this` branch doesn't get followed
})(global || exports || this);