-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfilter-object.js
More file actions
93 lines (86 loc) · 1.8 KB
/
filter-object.js
File metadata and controls
93 lines (86 loc) · 1.8 KB
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
'use-strict';
const _ = require('lodash');
class FilterObject {
/**
* Object for keeping track of kyruus filters
* @param {string} value - value of filter
* @param {string} type - the conjuction for the filter (| or ^)
* @constructor
*/
constructor(value, type = '') {
if (! _.isArray(this._value)) value = [value];
this._value = value;
this.setType(type);
}
/**
* @function getType
* @summary Returns the conjugation type (or(|) or and(^))
* @return {string}
*/
getType() {
return this._type;
}
/**
* @function getType
* @summary Returns the number of filters in this object
* @return {number}
*/
size() {
return this._value.length;
}
/**
* @function delete
* @summary Removes the value from the filter
* @param {string} value - value to remove
* @return {string}
*/
remove(value) {
_.forEach(this._value, (val) => {
if (val instanceof FilterObject) {
val.remove(value);
if (val._value.length === 0) {
_.pull(this._value, val);
}
}
else {
_.pull(this._value, value);
}
});
return this;
}
/**
* @function setType
* @summary Sets the conjugation type (or(|) or and(^))
* @return {string}
*/
setType(type) {
this._type = type !== '^' ? '|' : '^';
return this;
}
/**
* @function checkType
* @summary Compares the conjugation type (or(|) or and(^))
* @return {boolean}
*/
checkType(type) {
return type === this._type;
}
/**
* @function append
* @summary Adds a value to the filter
* @return {string}
*/
append(value) {
this._value.push(value);
return this;
}
/**
* @function toString
* @summary Converts the filter object into seperating its filters by its conjugation type
* @return {string}
*/
toString() {
return _.join(this._value, this._type);
}
}
module.exports = FilterObject;