-
Notifications
You must be signed in to change notification settings - Fork 3
/
jLinq.js
1676 lines (1439 loc) · 77.9 KB
/
jLinq.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* jLinq 2.2.1
* ---------------------------------
* www.hugoware.net
* License: Attribution-Share Alike
* http://creativecommons.org/licenses/by-sa/3.0/us/
*/
var jLinq;
(function() {
//contains functionality to create a new library for jLinq
var library = function(settings) {
var _jLinq = this;
var _p = {};
_p.settings = settings;
//locks a jLinq object entirely
_jLinq.finish = function(lock) {
_jLinq.finish = null;
_p.loaded = true;
_p.lock = lock;
};
//utilities
_p.util = {
format:function(msg,args) {
return msg.toString().replace(/%[0-9]+%/gi, function(match) {
var index = parseInt(match.replace(/%/gi, ""));
return args[index];
});
},
allValues:function(list) {
var actual = [];
for(var i = 0; i < list.length; i++) {
if (list[i] == null) { return actual; }
actual.push(list[i]);
}
return actual;
},
trim:function(val) {
if (val == null) { return ""; }
return val.toString().replace(/^\s+|\s+$/, "");
},
empty:function(list) {
for(var i = 0; i < list.length; i++) {
if (list[i]) { return false; }
}
return true;
},
type:function(val) {
//if null, return null
if (val == null) { return "null"; }
if (val == undefined) { return "null"; }
//check each type that has been saved
for (var item in _p.types) {
try {
if (_p.types[item](val)) { return item; }
}
catch (e) {
//no real concern here
}
}
//if nothing was found, just return the typeof value
return (typeof(val)).toString().toLowerCase();
},
when:function(val, actions) {
var type = _p.util.type(val);
if (!actions[type]) {
if (actions.empty && (val == null || val == undefined)) { return actions.empty(val); }
if (actions.other) { return actions.other(val); }
return false;
}
try {
return actions[type](val);
}
catch (e) {
return false;
}
},
as:function(val, actions) {
var type = _p.util.type(val);
if (!actions[type]) {
if (actions.empty && (val == null || val == undefined)) { return actions.empty(val); }
if (actions.other) { return actions.other(val); }
return null;
}
try {
return actions[type](val);
}
catch (e) {
return null;
}
},
each:function(array, action) {
var results = [];
for (var i = 0; i < array.length; i++) {
try {
results.push(action(array[i], i));
}
catch(e) {
results.push(e);
}
}
return results;
},
clone: function(obj) {
function gen(){};
gen.prototype = obj;
return new gen();
}
};
//Types to evaluate for using .when()
_jLinq.addType = function(name, compare) {
name = _p.util.trim(name).toLowerCase();
_p.types[name] = compare;
};
_jLinq.removeType = function(name) {
name = _p.util.trim(name).toLowerCase();
_p.types[name] = function() { return false; };
};
_p.types = settings.types ? settings.types : {};
//Extends functionality onto the jLinq object
//===============================================================================
_jLinq.extend = function(params) {
params.name = _p.util.trim(params.name);
params.namespace = _p.util.trim(params.namespace);
//check if this is locked
if (_p.extend.hasCmd(params)) {
if (_p.lock) { throw "Exception: Library is locked."; }
_p.extend.removeCmd(params);
}
//add this command to the list
_p.extend.addCmd(params);
//source extensions are evaluated immediately
if (params.type.match(/source/i)) {
//determine the correct source
if (params.namespace && !_jLinq[params.namespace]) { _jLinq[params.namespace] = {}; }
var target = params.namespace == "" ? _jLinq : _jLinq[params.namespace];
//apply this command
target[params.name] = function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
//prepare the command for use
var results = params.method({
helper:_p.util
}, v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
//make sure this is an array
if (_p.util.type(results) != "array") {
throw "Exception: A 'Source' extension must return an array for a jLinq query.";
}
//if this should generate a source
return new _p.query(results);
};
}
};
_p.extend = {
cmd:[],
hasCmd:function(params) {
return (_p.extend.findCmd(params) != null);
},
addCmd:function(params) {
_p.extend.removeCmd(params);
_p.extend.cmd.push(params);
},
removeCmd:function(params) {
var index = _p.extend.findCmd(params);
if (index) {
_p.extend.cmd.splice(index, 1);
}
},
findCmd:function(params) {
for(var i = 0; i < _p.extend.cmd.length; i++) {
var method = _p.extend.cmd[i];
if (method.name == params.name && method.namespace == params.namespace) {
return i;
}
}
return null;
}
};
//return a list of the commands available
_jLinq.showCommands = function() {
return _p.util.clone(_p.extend.cmd);
};
//add all of the current extension methods
for (var item in settings.extend) {
_jLinq.extend(settings.extend[item]);
}
//Generates the actual query for use
//===============================================================================
_p.query = function(data) {
var _query = this;
//duplicate this object
data = _p.util.clone(data);
//State of the query
var _s = {};
_s.state = {
properties:true,
lastCommand:null,
lastField:null,
lastCommandName:null,
paramCount:0,
ignoreCase:true,
or:false,
not:false,
data:data,
useProperties:false,
operator:"",
debug:{
onEvent:function(msg) { },
log:function(msg,args) {
_s.state.debug.onEvent(_p.util.format(msg,args));
}
}
};
//determine the type of data this is
if (data == null) { return null; }
if (_p.util.type(data) == "array" && data.length > 0) {
_s.state.useProperties = (_p.util.type(data[0]) == "object");
}
//Query evaluation
_s.query = {
cache:[],
str:[],
appendCmd:function(action) {
_s.query.cache.push(action);
//set the true or false value
var not = _s.state.not ? "!" : "";
//clear up an existing
if (_s.query.cache.length == 0) {
_s.state.operator = "";
}
//undo any state changes
_s.state.or = false;
_s.state.not = false;
//append the items
_s.query.str.push([_s.state.operator, "(", not, "(_s.query.cache[", (_s.query.cache.length - 1), "](record)))"].join(""));
//update the operator as needed
_s.state.operator = "&&";
},
select:function() {
//if there hasn't been a command, return everything
if (_s.query.str.length == 0) {
return {
selected:_s.state.data,
remaining: []
};
}
//get the query string
var queryString = ["query = function(record) {" + " return (", _s.query.str.join(""), "); };" ].join("");
//evaluate the result
var query;
eval(queryString);
//eval and select each result
var selected = []; var remaining = [];
for(var i = 0; i < _s.state.data.length; i++) {
var item = _s.state.data[i];
try {
if (query(item)) {
selected.push(item);
}
else {
remaining.push(item);
}
}
catch (e) {
_s.state.debug.log("Exception when evaluating the query for selection: %0%. query: %1%", [e,query]);
remaining.push(item);
}
}
//return the final results
return {
selected:selected,
remaining:remaining
};
},
getArgs:function(args) {
//start by clearing any null data
var values = []; var found = false;
for (var i = args.length; i-- > 0;) {
if (args[i] == null && !found) { continue; }
found = true;
values.push(args[i]);
}
values.reverse();
return values;
},
prepCmd:function(params, args) {
//prepare this command
//set the known data
_s.state.lastCommand = params.command;
_s.state.paramCount = params.count;
_s.state.lastCommandName = params.name;
//get the actual values for the query
var values = _s.query.getArgs(args);
//detetmine if a field was set in this
//if the data list count is greater than
//the required parameters, then the first
//param should be the name of the field
if (_s.state.useProperties && values.length == params.count + 1) {
_s.state.lastField = values.shift();
}
//return an object to execute with
return {
arg: values,
field: _s.state.lastField
};
},
repeatCmd:function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
if (_s.helper.empty([v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25])) { return _query; }
return _s.state.lastCommand(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
},
performSort:function(records, field, desc) {
records.sort(function(a,b) {
a = eval("a." + field);
b = eval("b." + field);
return (a < b) ? -1 : (a > b) ? 1 : 0;
});
if (desc) { records.reverse(); }
}
};
//Helper methods
_s.helper = {
toRegex:function(val) {
if (val == null) { return ""; }
return val.toString().replace(
/\*|\(|\)|\\|\/|\?|\.|\*|\+|\<|\>|\[|\]|\=|\&|\$|\^/gi,
function(match) { return ("\\" + match); }
);
},
getNumericValue:function(obj) {
if (obj.length) { return obj.length; }
return obj;
},
trim:_p.util.trim,
match:function(val,exp) {
if (!(val && exp)) { return false; }
if (_s.helper.type(exp) == "regexp") { exp = exp.source; }
exp = new RegExp(exp, "g"+(_s.state.ignoreCase?"i":""));
return (val.match(exp));
},
propsEqual:function(val1, val2) {
if (val1 == null && val2 == null) { return true; }
if (val1 == null || val2 == null) { return false; }
for(var name in val1) {
if (val2[name] == undefined) { return false; }
if (!_s.helper.equals(val1[name], val2[name])) { return false; }
}
return true;
},
equals:function(val1,val2) {
try {
//check for null values first
if (val1 == null && val2 == null) { return true; }
if ((val1 == null && val2) || (val1 && val2 == null)) { return false; }
//if this is a string, check for case
var val1Type = _s.helper.type(val1);
var val2Type = _s.helper.type(val2);
if (val1Type != val2Type) {
return false;
}
if (val1Type == "string" && val2Type == "string") {
return _s.helper.match(val1, "^"+val2+"$");
}
if (val1Type == "string" && val2Type == "regexp") {
return _s.helper.match(val1, val2);
}
if (val1Type == "number" || val1Type == "bool") {
return (val1 == val2);
}
else if (val1Type == "array" || val1Type == "object") {
if (val1.length != val2.length) { return false; }
for (var i = 0; i < val1.length; i++) {
if (!_s.helper.equals(val1[i], val2[i])) { return false; }
}
return true;
}
else {
return (val1 == val2);
}
}
catch (e) {
return false;
}
},
allEqual:function(val1,val2) {
if (_p.helper.type(val1) != "array") { val1 = [val1]; }
for (var item in val1) {
if (!_p.helper.equals(val1[item], val2)) { return false; }
}
return true;
},
anyEqual:function(val1,val2) {
if (_p.helper.type(val1) != "array") { val1 = [val1]; }
for (var item in val1) {
if (!_p.helper.equals(val1[item], val2)) { return true; }
}
return false;
},
sort:function(records, sorting, desc) {
//if no sorting was provided
if (sorting == null) {
records.sort();
if (desc) { query.state.data.reverse(); }
return records;
}
//recursively handle sorting
var index = 0;
var doSort = function(records) {
//clone to fix a BIZZARE IE7 bug
records = _p.util.clone(records);
var field = sorting[index].field;
var desc = sorting[index].desc;
//if at the end, just sort it
if (index == sorting.length - 1) {
_s.query.performSort(records, field, desc);
return records;
};
//increment forward to the next command
_s.query.performSort(records, field, desc);
var dist = _s.helper.distinct(records, field);
index++;
//sort and gather values - call _doSort again if
//to check for futher commands
var results = [];
for (var j = 0; j < dist.length; j++) {
var sorted = doSort(dist[j].items);
for (var k = 0; k < sorted.length; k++) {
results.push(sorted[k]);
}
};
//return the results for this section
return results;
};
return doSort(_s.state.data);
},
distinct:function(records, field) {
var dist = [];
for (var i = 0; i < records.length; i++) {
var val = records[i];
var key = (field != null) ? eval(["(val.", field, ")"].join("")) : val;
//check if the value exists yet
var added = false;
for (var item in dist) {
if (dist[item].key === key) {
added = true;
dist[item].items.push(val);
break;
}
}
//make sure it was added
if (!added) { dist.push({ key:key, items:[ val ]}); }
}
//return the final object
return dist;
},
empty:_p.util.empty,
type:_p.util.type,
when:_p.util.when,
each:_p.util.each,
format:_p.util.format,
clone:_p.util.clone,
all:_p.util.allValues,
as:_p.util.as
};
//apply each of the extended functions
for(var item in _p.extend.cmd) {
(function(params) {
//make sure this works
if (!(params.type || params.name || params.method)) { return; }
//source commands are not added to a query
if (params.type.match(/source/i)) { return; }
//determine the correct source
if (params.namespace && !_query[params.namespace]) { _query[params.namespace] = {}; }
var target = params.namespace ? _query[params.namespace] : _query;
//apply this command
var method = (function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
//set the state of the command
_s.state.debug.log("Called command %0% '%1%()'.", [params.type, params.name]);
var state = {
add:function(cmd) { _s.query.str.push(cmd); },
query:_query,
state:_s.state,
helper:_s.helper,
repeat:function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
_s.query.repeatCmd(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
}
};
//depending on the type, set how this acts
if (params.type.match(/^query$/i)) {
//prepare this command for use
var cmd = _s.query.prepCmd({
command:target[params.name],
count:params.count,
name:params.name
}, [v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25]);
//prepare the command for use
_s.query.appendCmd(function(record) {
//try and get the value
try {
state.value = _s.state.useProperties ? eval("record."+cmd.field) : record;
}
catch (e) {
_s.state.debug.log("Exception when calling '%0%()' : %1%.", [params.name, e]);
state.value = null;
}
//prepare the command
state.record = record;
state.type = state.helper.type(state.value);
state.when = function(actions) {
return _s.helper.when(state.value, actions);
};
//query the method
return params.method(state, cmd.arg[0], cmd.arg[1], cmd.arg[2], cmd.arg[3], cmd.arg[4],
cmd.arg[5], cmd.arg[6], cmd.arg[7], cmd.arg[8], cmd.arg[9], cmd.arg[10], cmd.arg[11], cmd.arg[12], cmd.arg[13], cmd.arg[14],
cmd.arg[15], cmd.arg[16], cmd.arg[17], cmd.arg[18], cmd.arg[19], cmd.arg[20], cmd.arg[21], cmd.arg[22], cmd.arg[23], cmd.arg[24]);
});
//return the query object
return _query;
}
//if an action, do the action and return the query
else if (params.type.match(/^action$/i)) {
//execute the method and return the query
try {
params.method(state, v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
}
catch (e) {
_s.state.debug.log("Exception when calling '%0%()' : %1%.", [params.name, e]);
}
return _query;
}
//if selecting, return whatever the method returns
else if (params.type.match(/^selection$/i)) {
//if this query wants to manually select the results
state.results = params.manual ? [] : _s.query.select();
state.select = _s.query.select;
//execute the selection method
return params.method(state, v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
}
//return the query
return _query;
});
//next, assign a regular version
target[params.name] = method;
//next, query methods receive extra names
if (params.type.match(/^query$/i) &&
(_p.settings.generate == null || _p.settings.generate) &&
(params.generate == null || params.generate)) {
//then assign a second "or" version
var altName = params.name.substr(0,1).toUpperCase() + params.name.substr(1, params.name.length - 1);
//create an automatic OR version
target["or"+altName] = function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
_s.state.operator = "||";
return method(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
};
//create an automatic AND version
target["and"+altName] = function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
_s.state.operator = "&&";
return method(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
};
//create an automatic NOT version
target["not"+altName] = function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
_s.state.not = !_s.state.not;
return method(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
};
//create an automatic NOT version
target["andNot"+altName] = function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
_s.state.not = !_s.state.not;
_s.state.operator = "&&";
return method(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
};
//create an automatic NOT version
target["orNot"+altName] = function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
_s.state.not = !_s.state.not;
_s.state.operator = "||";
return method(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
};
}
})(_p.extend.cmd[item]);
}
//finally, apply any default operator methods
//flags the query for || - can be used to repeat a command
_query["or"] = function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
_s.state.operator = "||";
return _s.query.repeatCmd(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
};
//flags the query for ! - can be used to repeat a command
_query["not"] = function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
_s.state.not = !_s.state.not;
return _s.query.repeatCmd(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
};
//flags the query for && - can be used to repeat a command
_query["and"] = function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
_s.state.operator = "&&";
return _s.query.repeatCmd(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
};
//flags the query for || and ! - can be used to repeat a command
_query["orNot"] = function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
_s.state.operator = "||";
_s.state.not = !_s.state.not;
return _s.query.repeatCmd(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
};
//flags the query for && and ! - can be used to repeat a command
_query["andNot"] = function(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25) {
_s.state.operator = "||";
_s.state.not = !_s.state.not;
return _s.query.repeatCmd(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25);
};
};
};
//creates a default jLinq library
var _defaultLibrary = function() {
return {
locked:true,
generate:true,
types:{
array:function(val) {
return (val.push && val.pop && val.reverse && val.slice && val.splice);
},
"function":function(val) {
return ((typeof(val)).toString().match(/^function$/i));
},
string:function(val) {
return ((typeof(val)).toString().match(/^string$/i));
},
number:function(val) {
return ((typeof(val)).toString().match(/^number$/i));
},
bool:function(val) {
return ((typeof(val)).toString().match(/^boolean$/i));
},
regexp:function(val) {
return (val.ignoreCase != null && val.global != null && val.exec);
},
date:function(val) {
return (val.getTime && val.setTime && val.toDateString && val.toTimeString);
}
},
extend:[
//Selection Methods
//============================================================
//default selection routine - selects from an array
{name:"from", type:"source",
method:function(query, source) {
return query.helper.when(source, {
"function":function() { return source(); },
array:function() { return source; },
other:function() { return [source]; }
});
}},
//Action Methods
//============================================================
//enters into debug mode - options available
{name:"debug", type:"action", operators:false,
method:function(query, delegate) {
query.state.debug.onEvent = delegate;
}},
//makes comparisons ignore case
{name:"reverse", type:"action",
method:function(query) {
query.state.data.reverse();
}},
//makes comparisons ignore case
{name:"ignoreCase", type:"action",
method:function(query) {
query.state.ignoreCase = true;
}},
//makes string comparisons case sensitive
{name:"useCase", type:"action",
method:function(query) {
query.state.ignoreCase = false;
}},
//combines all query selections into an expression with &&
{name:"combine", type:"action",
method:function(query, delegate) {
query.add(query.state.operator+"("+(query.state.not?"!":""));
query.state.operator = "";
delegate(query.query);
query.add(")");
}},
//combines all query selections into an expression with ||
{name:"orCombine", type:"action",
method:function(query, delegate) {
query.state.operator = "||";
query.add(query.state.operator+"("+(query.state.not?"!":""));
query.state.operator = "";
delegate(query.query);
query.add(")");
}},
//Query Methods
//=============================================================
//runs a delegate as a query, this method is definitely cheating
//since -1 is telling it not to expect a field...
{name:"where", count:-1, type:"query",
method:function(query, delegate) {
return delegate(query.record, query.helper);
}},
//checks if two fields are equal or not
{name:"has", count:1, type:"query",
method:function(query, value) {
var use = parseInt(value.toString(), 16);
var compare = parseInt(query.value.toString(), 16);
return ((compare & use) == use);
}},
//checks if two fields are equal or not
{name:"equals", count:1, type:"query",
method:function(query, value) {
return query.helper.equals(query.value, value);
}},
//returns if a string starts with the phrase
//returns if the first element in array is equal
{name:"startsWith", count:1, type:"query",
method:function(query, value) {
//allow arrays to be passed as comparisons
if (query.helper.type(value) != "array") {
value = [value];
}
//check for each value
for (var item in value) {
var match = value[item];
if (query.when({
array:function() {
return query.helper.equals(query.value[0], match)
},
other:function() {
return query.helper.match(query.value.toString(), "^"+match.toString());
}
})) { return true; };
}
}},
//returns if a string ends with a phrase
//returns if the last element in array is equal
{name:"endsWith", count:1, type:"query",
method:function(query, value) {
//allow arrays to be passed as comparisons
if (query.helper.type(value) != "array") {
value = [value];
}
//check for each value
for (var item in value) {
var match = value[item];
if (query.when({
array:function() {
return query.helper.equals(query.value[query.value.length - 1], match)
},
other:function() {
return query.helper.match(query.value.toString(), match.toString()+"$");
}
})) { return true; };
}
}},
//returns if a string contains a phrase
//returns if any element in array is equal
{name:"contains", count:1, type:"query",
method:function(query, value) {
if (value == null) { return false; }
//allow arrays to be passed as comparisons
if (query.helper.type(value) != "array") {
value = [value];
}
//check for each value
for (var item in value) {
var match = value[item];
if (query.when({
array:function() {
for (var i = 0; i < query.value.length; i++) {
if (query.helper.equals(query.value[i], match)) { return true; }
}
},
other:function() {
return query.helper.match(query.value.toString(), "^.*" + query.helper.toRegex(match) + ".*$");
}
})) { return true; };
}
}},
//evaluates each item with a regular expression
{name:"match", count:1, type:"query",
method:function(query, value) {
//allow arrays to be passed as comparisons
if (query.helper.type(value) != "array") {
value = [value];
}
//TODO : Convert items into regular expressions
//array:(item1|item2|item3|item4)
//check for each value
for (var item in value) {
var match = value[item];
if (query.when({
array:function() {
for (var i = 0; i < query.value.length; i++) {
if (query.helper.match(query.value[i], match)) { return true; }
}
},
other:function() {
return query.helper.match(query.value.toString(), match);
}
})) { return true; };
}
}},
//returns if a number is less
//returns if a string has less characters
//returns if an array has less elements
{name:"less", count:1, type:"query",
method:function(query, value) {
//get the value to use with this
value = query.helper.when(value, {
number:function() { return value; },
date:function() { return value; },
other:function() { return value.length; }
});
return query.when({
string:function() {
return (query.value.length < value);
},
array:function() {
return (query.value.length < value);
},
other:function() {
return (query.value < value);
}
});
}},
//returns if a number is more
//returns if a string has more characters
//returns if an array has more elements
{name:"greater", count:1, type:"query",
method:function(query, value) {
//get the value to use with this
value = query.helper.when(value, {
number:function() { return value; },
date:function() { return value; },
other:function() { return value.length; }
});
return query.when({
string:function() {
return (query.value.length > value);
},
array:function() {
return (query.value.length > value);
},
other:function() {
return (query.value > value);
}
});
}},
//returns if a number is less
//returns if a string has less characters
//returns if an array has less elements
{name:"lessEquals", count:1, type:"query",
method:function(query, value) {
//get the value to use with this
value = query.helper.when(value, {
number:function() { return value; },
date:function() { return value; },
other:function() { return value.length; }
});
return query.when({
string:function() {
return (query.value.length <= value);
},
array:function() {
return (query.value.length <= value);
},
other:function() {
return (query.value <= value);
}