-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetter.js
42 lines (34 loc) · 981 Bytes
/
setter.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
"use strict";
const { set, lensPath, mergeRight, over, identity } = require("ramda");
class Setter {
constructor(propertyPath, interceptFn) {
this.propertyPath = propertyPath;
this.interceptFn = interceptFn || identity;
}
set(value, target) {
const interceptedValue = this.interceptFn(value);
return set(lensPath(this.propertyPath), interceptedValue, target);
}
merge(value, target) {
const interceptedValue = this.interceptFn(value);
return over(lensPath(this.propertyPath), mergeRight(interceptedValue), target);
}
child(key) {
return new this.constructor([...this.propertyPath, key]);
}
intercept(interceptFn) {
return new this.constructor(this.propertyPath, interceptFn);
}
}
class DummySetter extends Setter {
set(value, target) {
return target;
}
merge(value, target) {
return target;
}
}
module.exports = {
createSetter: propertyPath => new Setter(propertyPath),
createDummySetter: propertyPath => new DummySetter(propertyPath)
}