-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlistutils.js
134 lines (104 loc) · 2.84 KB
/
listutils.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
.pragma library
function filter(tasks, filter, name) {
//print("Running filter:", name)
var list = []
for (var i = 0; i < length(tasks); i++) {
var task = getItem(tasks, i)
//print("Filtering:", task.name)
if (filter(task))
list.push(task)
}
//print("Filtered list:", list)
return list
}
function filteredCount(model, func, name) {
return filter(model, func, name).length
}
function filteredSum(list, prop, func, name) {
var value = 0
for (var i = 0; i < length(list); i++) {
var item = getItem(list, i)
value += filteredCount(item[prop], func, name)
}
return value
}
function sum(list, prop) {
var value = 0
for (var i = 0; i < length(list); i++) {
var item = getItem(list, i)
value += item[prop]
}
return value
}
function subList(list, prop) {
var value = []
//print("Concat:", prop, length(list))
for (var i = 0; i < length(list); i++) {
var item = getItem(list, i)
value.push(item[prop])
}
//print("Concat:", prop, value, length(value))
return value
}
function toList(model) {
var list = []
for (var i = 0; i < model.count; i++) {
list.push(getItem(model, i))
}
return list
}
function sort(list, prop) {
if (list.hasOwnProperty("count"))
list = toList(list)
//print("Sorting by", prop)
list.sort(function(a, b) {
return b.relevence - a.relevence
});
return list
}
function concat(list, prop, filter) {
var value = []
//print("Concat:", prop, length(list))
for (var i = 0; i < length(list); i++) {
var item = getItem(list, i)
if (filter && !filter(item)) continue
//print("Adding:", item[prop])
if (item[prop].hasOwnProperty("length")) {
value = value.concat(item[prop])
} else {
for (var j = 0; j < item[prop].count; j++) {
value.push(getItem(item[prop], j))
}
}
}
//print("Concat:", prop, value, length(value))
return value
}
function getItem(model, index) {
var item = model.hasOwnProperty("get") ? model.get(index) : model[index]
if (model.hasOwnProperty("get"))
item = item.modelData
return item
}
function length(model) {
if (model === undefined || model === null)
return 0
else
return model.hasOwnProperty("count") ? model.count : model.length
}
Array.prototype.move = function (old_index, new_index) {
if (new_index >= this.length) {
var k = new_index - this.length;
while ((k--) + 1) {
this.push(undefined);
}
}
this.splice(new_index, 0, this.splice(old_index, 1)[0]);
return this; // for testing purposes
};
function objectKeys(obj) {
var keys = []
for(var k in obj)
keys.push(k)
return keys
}