-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (132 loc) · 3.41 KB
/
index.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
var extend = require('extend'),
criteria = require('./lib/criteria.js'),
filters = require('./lib/filters.js'),
handlers = require('./lib/handlers.js');
var rotatelib = {
/**
* Add a new criteria item.
*/
addCriteria: function(key, obj) {
criteria[key] = obj;
},
/**
* Figure out the applicable criteria items for this request.
*/
getApplicableCriteria: function(params) {
var criteriaItems = [];
for (var property in criteria) {
if (criteria.hasOwnProperty(property) && property.substr(0, 1) !== '_') {
var criteriaItem = criteria[property];
if (criteriaItem.applies(params)) {
if (params.debug) {
console.log(property);
}
criteriaItems.push(criteriaItem);
}
}
}
return criteriaItems;
},
/**
* Figure out the applicable filters for this request.
*/
getApplicableFilters: function(params) {
var filterItems = [];
for (var filter in filters) {
if (filter.substr(0, 1) !== '_' && filters.hasOwnProperty(filter)) {
var filterItem = filters[filter];
if (filterItem.applies(params)) {
filterItems.push(filterItem);
}
}
}
return filterItems;
},
/**
* Figure out what handler to use
*/
getHandler: function(params) {
for (var handler in handlers) {
if (handlers.hasOwnProperty(handler)) {
var thisHandler = handlers[handler];
if (thisHandler.applies(params)) {
return thisHandler;
}
}
}
},
/**
* List out items that match the criteria.
*/
list: function(params) {
var items = [];
var self = this;
var applyCriteria = rotatelib.getApplicableCriteria(params);
var applyFilters = rotatelib.getApplicableFilters(params);
if (typeof params.debug === 'undefined') {
params.debug = false;
}
// list items from the items param
if (params.hasOwnProperty('items')) {
params.items.forEach(function(item) {
if (rotatelib.matchesCriteria(item, params, applyCriteria)) {
if (params.debug) {
console.log(item);
}
items.push(item);
}
});
if (applyFilters) {
applyFilters.forEach(function(filter) {
items = filter.filter(items, params);
});
}
return items;
}
// this is handled elsewhere
var handler = rotatelib.getHandler(params);
if (handler) {
handler.rotatelib = this;
return handler.list(params);
}
return items;
},
/**
* List archives.
*/
listArchives: function(params) {
params.patterns = '*.gz, *.bz2, *.tgz';
return list(params);
},
/**
* Check if the item matches the criteria.
*/
matchesCriteria: function(item, params, criteria) {
if (criteria.length === 0) {
if (params.debug) {
console.log('no criteria');
}
return false;
}
for (var i = 0, count = criteria.length; i < count; i++) {
var result = criteria[i].matches(item, params);
if (!result) {
// stop right now
return false;
}
}
return true;
},
/**
* Remove items.
*/
removeItems: function(items, params) {
// figure out the handler for this case
var handler = rotatelib.getHandler(params);
if (handler) {
handler.rotatelib = this;
return handler.removeItems(items, params);
}
}
};
module.exports = rotatelib;