-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
168 lines (134 loc) · 3.87 KB
/
utils.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const _curry = (fn) =>
(...args) =>
args.length >= fn.length
? fn(...args)
: _curry(fn.bind(undefined, ...args));
export const asyncCompose = (...functions) =>
(input) =>
functions.reduceRight(
(chain, func) => chain.then(func),
Promise.resolve(input),
);
export const asyncPipe = (...functions) =>
(input) =>
functions.reduce(
(chain, func) => chain.then(func),
Promise.resolve(input),
);
export const compose = (...fns) =>
(data) => fns.reduceRight((value, fn) => fn(value), data);
export const isString = (obj) =>
Object.prototype.toString.call(obj) === "[object String]";
export const isObject = (obj) =>
Object.prototype.toString.call(obj) === "[object Object]";
export const isArray = (obj) => Array.isArray(obj);
export const isFunction = (obj) =>
Object.prototype.toString.call(obj) === "[object Function]";
export const isAsyncFunction = (obj) =>
Object.prototype.toString.call(obj) === "[object AsyncFunction]";
export const path = _curry((p, o) =>
p.reduce((xs, x) => (xs && xs[x] ? xs[x] : null), o)
);
export const has = (prop, obj) =>
Object.prototype.hasOwnProperty.call(obj, prop);
export const isEmpty = (obj) => {
if (isNil(obj)) {
return false;
} else if (isArray(obj)) {
return obj.length === 0;
} else if (isString(obj)) {
return obj === "";
} else if (isObject(obj)) {
return Object.keys(obj).length === 0 && obj.constructor === Object;
}
return false;
};
export const isNil = (obj) => typeof obj === "undefined" || obj === null;
export const ifElse = _curry((condition, onTrue, onFalse) =>
(...args) =>
condition.apply(this, ...args)
? onTrue.apply(this, ...args)
: onFalse.apply(this, ...args)
);
export const setTo = (obj, chunk) => Object.assign({}, obj, chunk);
export const tryCatch = (fn1, fn2) => {
try {
return fn1();
} catch (_e) {
return fn2();
}
};
export const pickBy = (o, prop) => Object.keys(o).find;
export const tap = _curry((logger, any) => {
logger(any);
return any;
});
export const dissoc = (prop, o) => {
var result = {};
for (var p in o) {
result[p] = o[p];
}
delete result[prop];
return result;
};
export const containsAll = (inputObj, obj) => {
var idx = 0;
const names = Object.keys(inputObj);
var len = names.length;
while (idx < len) {
var name = names[idx];
if (!obj[name] || obj[name] !== inputObj[name]) {
return false;
}
idx += 1;
}
return true;
};
export const cond = (conditions) =>
(method) => {
const _cond = conditions.find(
(cond) => has("when", cond) && cond.when(method),
);
if (!_cond) {
return (props) => props;
}
return path(["use"], _cond);
};
export const eq = (val) => (comp) => val === comp;
export const when = (condFunc, applyFunc) =>
(props) => {
if (condFunc(props)) {
return applyFunc(props);
}
return props;
};
// lenses
const prop = _curry((k, obj) => (obj ? obj[k] : undefined));
const assoc = _curry((k, v, obj) => {
if (!Number.isNaN(Number(k))) {
obj[k] = v;
return [...obj];
}
return { ...obj, [k]: v };
});
const lens = _curry((getter, setter) =>
(F) => (target) => F(getter(target)).map((focus) => setter(focus, target))
);
const lensProp = (k) => lens(prop(k), assoc(k));
export const lensPath = (path) => compose(...path.map(lensProp));
const always = (a) => () => a;
const setFunctor = (x) =>
Object.freeze({
value: x,
map: (f) => setFunctor(f(x)),
});
const getFunctor = (x) =>
Object.freeze({
value: x,
map: (f) => getFunctor(x),
});
const over = _curry((lens, f, obj) => lens((y) => setFunctor(f(y)))(obj).value);
export const view = _curry((lens, obj) => lens(getFunctor)(obj).value);
export const set = _curry((lens, val, obj) => over(lens, always(val), obj));
export const setLens = ({ path, content, obj }) =>
set(lensPath(path))(content)(obj);