-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonty.js
200 lines (164 loc) · 5.5 KB
/
monty.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
/**
*
* User: Leon Stankowski
* Email: [email protected]
* Date: 10/14/12
* Time: 12:22 AM
*
*/
'use strict';
function getSubItem(item, path) {
var pathElements = path.split('.');
var pathElementsLength = pathElements.length;
for (var i = 0; i < pathElementsLength; ++i) {
var pathElement = pathElements[i];
if (typeof item !== 'object') return undefined;
item = item[pathElement];
}
return item;
}
var CriteriaTypeMismatch = new Error('criteria type mismatch');
var OrRequiresNonEmptyArray = new Error('$or requires nonempty array');
var testOperators = {};
testOperators.$gt = function(item, criteria) {
if (criteria instanceof Object) return CriteriaTypeMismatch;
return item > criteria;
}
testOperators.$gte = function(item, criteria) {
if (criteria instanceof Object) return CriteriaTypeMismatch;
return item >= criteria;
}
testOperators.$lt = function(item, criteria) {
if (criteria instanceof Object) return CriteriaTypeMismatch;
return item < criteria;
}
testOperators.$lte = function(item, criteria) {
if (criteria instanceof Object) return CriteriaTypeMismatch;
return item <= criteria;
}
testOperators.$ne = function(item, criteria) {
if (criteria instanceof Object) return CriteriaTypeMismatch;
return item !== criteria;
}
testOperators.$in = function(item, criteria) {
if (criteria instanceof Array) {
var criteriaLength = criteria.length;
for (var i = 0; i < criteriaLength; ++i) {
var subCriteria = criteria[i];
var disposition = testExpression(item, subCriteria);
if (disposition === true) return disposition;
}
return false;
}
return CriteriaTypeMismatch;
}
testOperators.$all = function(item, criteria) {
if (criteria instanceof Array) {
var criteriaLength = criteria.length;
for (var i = 0; i < criteriaLength; ++i) {
var subCriteria = criteria[i];
var disposition = testExpression(item, subCriteria);
if (disposition !== true) return disposition;
}
return true;
}
return CriteriaTypeMismatch;
}
testOperators.$exists = function(item, criteria) {
var typeofItem = typeof item;
if (typeofItem === 'undefined' || typeofItem === 'null') return false;
return true;
}
testOperators.$nin = function(item, criteria) {
var disposition = testOperators.$in(item, criteria);
if (disposition === true) {
disposition = false;
} else if (disposition === false) {
disposition = true;
}
return disposition;
}
testOperators.$or = function(item, criteria) {
if (criteria instanceof Array) {
var disposition = OrRequiresNonEmptyArray;
var criteriaLength = criteria.length;
for (var i = 0; i < criteriaLength; ++i) {
var subCriteria = criteria[i];
var disposition = testCriteria(item, subCriteria);
if (disposition === true) return disposition;
}
return disposition;
}
return CriteriaTypeMismatch;
}
// $in, $nin, $or WHERE item is an array
// $nor, $and, $size, $mod, $type
// $elemMatch
function testExpression(item, criteria) {
var disposition = false;
/*
console.log('');
console.log('testExpression item = ', item);
console.log('testExpression criteria = ', criteria);
*/
if (typeof criteria === 'object') {
if (criteria instanceof RegExp) {
disposition = criteria.test(item);
//console.log('testExpression(' + item + ', ' + criteria + ') -> ', disposition);
} else {
return testCriteria(item, criteria);
}
} else {
disposition = (item === criteria);
//console.log('testExpression(' + item + ', ' + criteria + ') -> ', disposition);
}
return disposition;
}
function testCriteria(item, criteria) {
var disposition = true;
var criteriaKeys = Object.keys(criteria);
var criteriaKeysLength = criteriaKeys.length;
/*
console.log('');
console.log('testCriteria item = ', item);
console.log('testCriteria criteria = ', criteria);
*/
for (var criteriaKeyIndex = 0; criteriaKeyIndex < criteriaKeysLength; ++criteriaKeyIndex) {
var criteriaKey = criteriaKeys[criteriaKeyIndex];
var subCriteria = criteria[criteriaKey];
var operator = testOperators[criteriaKey];
if (operator) {
disposition = operator(item, subCriteria);
} else {
var subItem = getSubItem(item, criteriaKey);
disposition = testExpression(subItem, subCriteria);
}
if (disposition !== true) return disposition;
}
return disposition;
}
function makeFind(dataSourceList) {
var find = function (criteria, /* optional */ cb) {
var err;
var resultList = [];
var dataSourceListLength = dataSourceList.length;
for (var i = 0; i < dataSourceListLength; ++i) {
var dataSourceItem = dataSourceList[i];
var disposition = testCriteria(dataSourceItem, criteria);
if (disposition === true) {
resultList.push(dataSourceItem);
} else if (disposition !== false) {
err = disposition;
break;
}
}
if (cb) {
process.nextTick(function () { cb(err, resultList); });
} else if (err) {
console.log('find(), err = ', err);
}
return err ? null : resultList;
};
return find;
}
exports.makeFind = makeFind;