-
Notifications
You must be signed in to change notification settings - Fork 0
/
pick.js
203 lines (164 loc) · 5.47 KB
/
pick.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
function pick(path, matchObject, defaultValue) {
var matcher = compileMatcher(matchObject);
var selector = compileSelector(path, matcher);
function applyMatcher(selection) {
if (!selection.matched) {
selection.matched = matcher(selection.value, selection.parent);
}
return selection.matched;
}
return function(data, parent) {
var wasWrapped = (data instanceof Selection);
data = (data instanceof Selection) ? data : createSelection(data, parent);
var selection = selector(data);
if (typeof selection !== 'undefined' && applyMatcher(selection)) {
return wasWrapped ? selection : selection.value;
}
return defaultValue;
};
}
function compileMatcher(matchObject) {
if (typeof matchObject === 'undefined') {
return function(candidate) {
return typeof candidate !== 'undefined';
};
}
if (typeof matchObject === 'function') {
return matchObject;
}
var matchers = [];
for (var path in matchObject) {
if (matchObject.hasOwnProperty(path)) {
var expected = matchObject[path];
var selector = pick(path);
matchers.push({selector : selector, expected : expected });
}
}
return function(candidate, candidateParent) {
for(var idx = 0; idx < matchers.length; idx++) {
var test = matchers[idx];
var selection = test.selector(candidate, candidateParent);
if (selection !== test.expected) {
return false;
}
}
return true;
};
}
function compileSelector(path, matcher) {
var selectors = selectorsForPath(path, matcher);
return function(currentSelection) {
if (typeof currentSelection === 'undefined' || currentSelection.value === null) {
return undefined;
}
for (var idx = 0; idx < selectors.length; idx++) {
var selector = selectors[idx];
currentSelection = selector(currentSelection);
if (typeof currentSelection === 'undefined') {
break;
}
}
return currentSelection;
}
}
function selectorsForPath(path, matcher) {
var segments = toSegments(path);
var selectors = [];
function nonEmpty(selector) {
return selector.length > 0;
}
function toSegments(path) {
if (typeof path === 'string') {
return path.split("/").filter(nonEmpty);
} else { // already segmented
return path.slice(0);
}
}
while (segments.length > 0) {
var segment = segments.shift();
var field = parseField(segment);
var index = parseIndex(segment);
if (typeof field === 'string') {
if (field === '..') {
selectors.push(selectParent);
} else {
selectors.push(selectField(field));
}
}
if (typeof index === 'string') {
if (index === '*') {
selectors.push(selectAny(segments, matcher));
segments = [];
} else if (index === '') {
selectors.push(selectAll(segments, matcher));
segments = [];
} else {
selectors.push(selectField(parseInt(index)));
}
}
}
return selectors;
}
function parseField(segment) {
var indexStart = segment.lastIndexOf('[');
var length = (indexStart !== -1) ? indexStart : segment.length;
var field = segment.substring(0, length);
return field.length > 0 ? field : undefined;
}
function parseIndex(segment) {
var openingBracket = segment.lastIndexOf('[');
var closingBracket = segment.lastIndexOf(']');
if (openingBracket !== -1 && closingBracket !== -1) {
return segment.substring(openingBracket+1, closingBracket);
} else {
return undefined;
}
}
function selectParent(wrappedObject) {
return wrappedObject.parent;
}
function selectField(fieldName) {
return function fieldSelector(wrappedObject) {
var object = wrappedObject.value;
return createSelection(object[fieldName], wrappedObject);
};
}
function selectAny(remainingSegments, matcher) {
return function anySelector(currentSelection) {
var array = currentSelection.value;
for (var idx = 0; idx < array.length; idx++) {
var nextRoot = createSelection(array[idx], currentSelection);
var selection = pick(remainingSegments, matcher)(nextRoot);
if (typeof selection !== 'undefined') {
return selection;
}
}
return undefined;
}
}
function selectAll(path, matcher) {
return function allSelector(currentSelection) {
var result = [];
var array = currentSelection.value;
for (var idx = 0; idx < array.length; idx++) {
var nextValue = pick(path, matcher)(array[idx]);
if (typeof nextValue !== 'undefined') {
result.push(nextValue);
}
}
return createSelection(result, currentSelection, true);
};
}
function Selection(value, parent, matched) {
this.value = value;
this.parent = parent;
this.matched = matched;
}
function createSelection(value, parent, matched) {
if (typeof value === 'undefined') {
return value;
} else {
return new Selection(value, parent, matched);
}
}
module.exports = pick;