-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperators.js
47 lines (33 loc) · 1.1 KB
/
operators.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
var operators = {
"+": append,
"^": prepend,
"+.": addClasses,
"-.": removeClasses
};
function append(currentValue, newValue, options) {
return "" + (options.attributeName ? currentValue : options.element.innerHTML) + newValue;
}
function prepend(currentValue, newValue, options) {
return "" + newValue + (options.attributeName ? currentValue : options.element.innerHTML);
}
function addClasses(current, classes) {
current = current.split(" ").filter(truthy);
classes = classes.split(" ").filter(truthy);
return current.concat(classes.filter(negate(contains.bind(null, current)))).join(" ");
}
function removeClasses(current, classes) {
var doesntContain = negate(contains.bind(null, classes.split(" ").filter(truthy)));
return current.split(" ").filter(truthy).filter(doesntContain).join(" ");
}
function contains(container, thing) {
return container.indexOf(thing) >= 0;
}
function truthy(thing) {
return !!thing;
}
function negate(fn) {
return function () {
return !fn.apply(null, arguments);
};
}
module.exports = operators;