forked from socialpoint-labs/sheetfu-apps-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselector.gs
110 lines (99 loc) · 2.97 KB
/
selector.gs
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
/**
* Constructor which creates a Selector object to query Items in a Table.
* @param {Table} table: The Table object where to evaluate the criteria.
* @param {Array} criteria: an array used as filter as an AND of ORs (see CNF). Examples:
* >>> [{date: today}, [{tag: 1},{tag: 2}]] // (date === today && (tags === 1 || tags === 2))
* >>> [[{assigneeId: 'GO'}, {assigneeId: 'AM'}]] // (assigneeId === 'GO' || assigneeId === 'AM')
* >>> [{name: 'Guillem'}, {surname: 'Orpinell'}] // (name === 'Guillem' && surname === 'Orpinell')
* >>> {name: 'Guillem', surname: 'Orpinell'} // (name === 'Guillem' && surname === 'Orpinell')
* @constructor
*/
function Selector(table, criteria) {
this.table = table;
this.criteria = criteria;
this.queryItems = new GridArray();
}
/**
* Method to get the query items in a Selector object.
*/
Selector.prototype.getQueryItems = function() {
return this.queryItems;
}
/**
* Method to evaluate a criteria within a Table object.
*/
Selector.prototype.evaluate = function() {
if (Array.isArray(this.criteria)) {
var andsArray = this.criteria;
}
else if (isObject(this.criteria)) {
var andsArray = [this.criteria];
} else {
throw 'Oops! Criteria should be an Array or an Object. Fix it and try again.'
}
for (var i = 0; i < this.table.items.length; i++) {
var item = this.table.items[i];
if (isMatching(item, andsArray)) {
this.queryItems.push(item);
}
}
return this
}
/**
* Function to evaluate a criteria within an Item object.
* @param {Item} item: The Item object where to evaluate the criteria.
* @param {Array} criteria: an array used as filter as an AND of ORs (see CNF).
@return {Boolean}
*/
function isMatching(item, andsArray) {
for (var i=0; i < andsArray.length; i++) {
var clause = andsArray[i];
if (isObject(clause) && someUnmatch(item, clause)) { //AND logic
return false;
}
else if (Array.isArray(clause) && noneMatches(item, clause)) { //OR logic
return false;
}
}
return true;
}
/**
* Function
*/
function someUnmatch(item, object) {
for (var field in object) {
if (!valuesMatch(object[field], item.getFieldValue(field))) {
return true;
}
}
return false;
}
/**
* Function
*/
function noneMatches(item, orsArray) {
for (var i=0; i < orsArray.length; i++) {
var object = orsArray[i];
if (!isObject(object)) {
throw 'Oops! The ORs array must be an array of Objects. Fix it and try again.'
}
for (var field in object) {
if (valuesMatch(object[field], item.getFieldValue(field))) {
return false;
}
}
}
return true;
}
/**
* Function to check a matching between two values, considering also value as a Date.
*/
function valuesMatch(value1, value2) {
return ((value1 instanceof Date && value1.getTime() === value2.getTime()) || value1 === value2)
}
/**
* Returns if a value is an object
*/
function isObject (value) {
return value && typeof value === 'object' && value.constructor === Object;
}