forked from influentialpublishers/awesomize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (37 loc) · 1.47 KB
/
index.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
const _ = require('ramda');
const Check = require('./lib/check');
const Field = require('./lib/field');
const Action = require('./lib/action');
const MSG_USER_CTX = 'context parameter (ctx) must be an object';
const MSG_FIELD_FACTORY = 'field_factory parameter must be a function';
const MSG_FIELD_CONFIG = 'field_factory must return an object';
const checkParam = (test, msg) =>
_.unless(test, () => { throw new TypeError(msg) });
const getValidated = _.prop('validated');
const getData = _.prop('data');
const hasError = _.compose(
_.reduce(_.or, false)
, _.map(_.compose(_.not, _.isNil))
, _.values
, getValidated
);
const parseActions = (user_ctx, field_factory) => _.compose(
_.values
, Field.configToActionList
, checkParam(_.is(Object), MSG_FIELD_CONFIG)
, field_factory
)(Check, user_ctx)
const Awesomize = _.curry((user_ctx, field_factory) => {
checkParam(_.is(Object), MSG_USER_CTX)(user_ctx)
checkParam(_.is(Function), MSG_FIELD_FACTORY)(field_factory);
const actions = parseActions(user_ctx, field_factory);
return (request, current) => Action.Runner({ request, current }, actions);
});
const dataOrError = (error) => _.curry((user_ctx, field_factory) => _.composeP(
_.ifElse(hasError, _.compose(error, getValidated), getData)
, Awesomize(user_ctx, field_factory)
));
Awesomize.Result = { hasError };
Awesomize.dataOrError = dataOrError
Awesomize.MSG = Check.MSG;
module.exports = Awesomize;