-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhiggle.js
227 lines (221 loc) · 8.01 KB
/
higgle.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
(function() {
// Array wrapper
function HiggleCollection() {
Array.call(this, arguments);
}
HiggleCollection.prototype = [];
HiggleCollection.prototype.each = function each(callback) {
return this.forEach(callback);
};
HiggleCollection.prototype.clear = function() {
while (this.length > 0) {
this.pop();
}
};
// Handles callback
function checkCall(cb, arg) {
if (cb)
cb(arg);
else
return arg;
}
// Inserts a JSON document element into an array.
HiggleCollection.prototype.insert = function(element) {
this.push(element);
};
// An internal function used for finding the query matches within a collection
HiggleCollection.prototype.matchQuery = function(doc, query) {
// Tokenize the query
var queryKeys = Object.keys(query),
docKeys = Object.keys(doc),
queryKeysNum = queryKeys.length,
queryValues = [],
docValues = [],
buffer = [], // Set up the true collector
k, i;
for (i = 0; i < queryKeys.length; i++) {
queryValues.push(query[queryKeys[i]]);
}
for (i = 0; i < docKeys.length; i++) {
docValues.push(doc[docKeys[i]]);
}
// Begin scanning the doc for matches
var len = docKeys.length;
for (i = 0; i < len; i++) {
for (k = 0; k < queryKeys.length; k++) {
if (docKeys[i] == queryKeys[k]) {
var dvalue = docValues[i];
var qvalue = queryValues[k];
// if they are not of the same type and there are no higgle operators
if ((typeof dvalue !== typeof qvalue) && (qvalue.constructor.name !== "HiggleOp")) {
continue;
}
// if they are not of the same type and higgle operators are present
if ((typeof dvalue !== typeof qvalue) && (qvalue.constructor.name === "HiggleOp")) {
if (qvalue.type === 'less') {
// check if the value is actually less than
if (dvalue < qvalue.less) {
buffer.push(true);
}
}
if (qvalue.type === 'great') {
// check if the value is actually greater than
if (dvalue > qvalue.great) {
buffer.push(true);
}
}
if (qvalue.type === 'range') {
// check if the value is actually greater than
if ((dvalue > lower) && (dvalue < upper)) {
buffer.push(true);
}
}
if (qvalue.type === 'or') {
// check if the value matches one of the possibilities
var z;
for (z = 0; z < qvalue.possibilities.length; z++) {
if (qvalue.possibilities[z] === dvalue) {
buffer.push(true);
break;
}
}
}
} else {
// determine what the type is
var type = typeof qvalue;
// check if arrays
if (Array.isArray(qvalue)) {
if (JSON.stringify(qvalue) === JSON.stringify(dvalue)) {
buffer.push(true);
}
}
// check if json
else if (type === 'object') {
if (JSON.stringify(qvalue) === JSON.stringify(dvalue)) {
buffer.push(true);
}
}
// check if anything else
if (qvalue === dvalue) {
buffer.push(true);
}
}
}
}
}
if (buffer.length === queryKeysNum) {
return true;
} else {
return false;
}
};
// Returns the results of a query
HiggleCollection.prototype.find = function(query, callback) {
// handle returning any empty queries
if (!query || JSON.stringify(query) === '{}') {
return this;
}
// handle any real queries
else {
// this is the result array
// it will be packed with matching json documents
var result = new HiggleCollection();
// loop through each document in the collection
var i;
for (i = 0; i < this.length; i++) {
// setup the current document and see if it matches
var currentDoc = this[i];
if (result.matchQuery(currentDoc, query)) {
result.push(currentDoc);
}
}
if (result.length !== 0) {
return checkCall(callback, result);
} else {
return checkCall(callback, false);
}
}
};
// Constructor for the Higgle object
this.Higgle = function Higgle() {
// Represents all the collections within the database
this.collections = [];
};
// Allows user to create a new collection
Higgle.prototype.createCollection = function(name, callback) {
var k = new HiggleCollection();
this.collections.push([name, k]);
};
// Returns a collection if it exists.
Higgle.prototype.collection = function(name, callback) {
var i;
for (i = 0; i < this.collections.length; i++) {
if (name == this.collections[i][0]) {
return checkCall(callback, this.collections[i][1]);
}
}
return checkCall(callback, false);
};
// Closes the database
Higgle.prototype.close = function() {
return true;
};
// Dumps the database to a file
Higgle.prototype.dump = function(name) {
require('fs').writeFile(name, JSON.stringify(this.collections), function(err) {
if (err) throw err;
});
};
// Load a database from a file
Higgle.prototype.load = function(name, cb) {
require('fs').readFile(name, 'utf8', function(err, buffer) {
if (err) throw err;
var newone = new Higgle();
newone.collections = JSON.parse(buffer);
cb(newone);
});
};
// Constructor for the Higgle operator object
this.HiggleOp = function HiggleOp(type) {
this.type = type;
};
// Methods for the higgle operator
HiggleOp.prototype.setLess = function(less) {
this.less = less;
};
HiggleOp.prototype.setGreat = function(great) {
this.great = great;
};
HiggleOp.prototype.setRange = function(lower, upper) {
this.lower = lower;
this.upper = upper;
};
HiggleOp.prototype.setPossibleValues = function(values) {
this.possibilities = values;
};
// Create the global higgle operator functions
this.less = function(num) {
var op = new HiggleOp('less');
op.setLess(num);
return op;
};
this.great = function(num) {
var op = new HiggleOp('great');
op.setGreat(num);
return op;
};
this.range = function(lower, upper) {
var op = new HiggleOp('range');
op.setRange(lower, upper);
return op;
};
this.or = function(values) {
var op = new HiggleOp('or');
op.setPossibleValues(values);
return op;
};
})(this);
// Node.js exportss
if (typeof exports !== 'undefined') {
module.exports = Higgle;
}