forked from influentialpublishers/awesomize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.js
73 lines (51 loc) · 1.53 KB
/
check.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
const _ = require('ramda');
const Bluebird = require('bluebird');
// PASS : Nothing
const PASS = _.always(null);
// FAIL : String -> (* -> String)
const FAIL = (msg) => _.always(msg);
const MSG = {
REQUIRED : 'required'
, CANNOT_BE_EQUAL : 'cannot_be_equal'
, NOT_FUNCTION : 'not_function'
, NOT_ARRAY : 'not_array'
, LIST_ITEM_NOT_SPEC : 'list_item_not_spec'
};
// basic : (a -> Bool), String -> (a -> Maybe String)
const basic = (test, msg) => _.ifElse(test, PASS, FAIL(msg));
// extern : (a -> Bool), String -> (a -> Maybe String)
const extern = (test, msg) => _.composeP(
basic(_.equals(true), msg)
, _.when(_.isNil, _.T)
, _.compose(Bluebird.resolve, test)
);
// required : a -> Maybe String
const required = basic(
_.compose(_.not, _.either(_.isEmpty, _.isNil))
, MSG.REQUIRED
);
required.always_run = true;
// notEqual : a -> Maybe String
const notEqual = (x) => basic(
_.compose(_.not, _.equals(x))
, MSG.CANNOT_BE_EQUAL
);
// isArray : a -> Maybe String
const isFunction = basic(_.is(Function), MSG.NOT_FUNCTION);
// isArray : a -> Maybe String
const isArray = basic(_.is(Array), MSG.NOT_ARRAY);
// listOf : (a -> Bool), String -> Array a -> Promise Maybe String
const listOf = (check, msg) => (list) => {
const validator = extern(check, msg || MSG.LIST_ITEM_NOT_SPEC);
const reducer = (result, item) => result ? result : validator(item);
return Bluebird.reduce(list, reducer, null);
};
module.exports = {
MSG
, extern
, required
, notEqual
, isFunction
, isArray
, listOf
};