-
Notifications
You must be signed in to change notification settings - Fork 0
/
fp.js
74 lines (59 loc) · 1.85 KB
/
fp.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
const pipe = (...fns) => initialArg => fns.reduce((result, fn) => fn(result), initialArg);
const curry = (originalFunction, initialParams = []) => {
return (...nextParams) => {
const curriedFunction = (params) => {
if (params.length === originalFunction.length) {
return originalFunction(...params);
}
return curry(originalFunction, params);
};
return curriedFunction([...initialParams, ...nextParams]);
};
};
const map = curry((fn, items) => items.map(i => fn(i)));
const reduce = curry((fn, initialValue, items) => items.reduce(fn, initialValue));
// const prop = name => obj => obj[name];
const prop = curry((propName, obj) => obj[propName]);
// const filter = list => predicate => list.filter(predicate);
const filter = curry((predicate, list) => list.filter(predicate));
const find = curry((predicate, list) => list.find(predicate));
const findIndex = curry((predicate, list) => list.findIndex(predicate));
// isEq = a => b => a === b;
const isEq = curry((a, b) => a === b);
// const = item => list => [...list, item];
const concat = curry((item, list) => [...list, item]);
// const not = cond => !cond
const not = curry(cond => !cond);
// const tap = fn => el => fn(el) && el;
const tap = curry((fn, el) => {
fn(el);
return el;
});
const multiply = curry((a, b) => a * b);
const copyObject = curry(object => JSON.parse(JSON.stringify(object)));
const update = curry((index, newVal, list) => {
return pipe(
copyObject,
curry(listCopy => {
listCopy[index] = Object.assign({}, listCopy[index], newVal);
return listCopy;
}),
)(list);
});
module.exports = {
curry,
pipe,
prop,
filter,
isEq,
tap,
concat,
not,
map,
reduce,
multiply,
find,
copyObject,
findIndex,
update
};