-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguards.js
181 lines (135 loc) · 3.86 KB
/
guards.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
169
170
171
172
173
174
175
176
177
178
179
180
181
import { ap, apply, curry, head, init, last, map, splitEvery, zip } from 'ramda';
import check from 'check-types';
// Description
// ----------------------------------------------------------------------------
// The `guard` function helps to verify an input parameters and result of your
// functions.
// You can use it as follow
// const fun = guard(guards, fn);
// where:
// `guards` is a list of validator functions,
// `fn` is your function.
//
// Example
// ----------------------------------------------------------------------------
// > const tenDividedBy = guard([notZero, isLess(4)], (x) => 10 / x);
// > tenDividedBy(2) // 5
// > tenDividedBy(0) // Error: The value is zero!
// > tenDividedBy(5) // Error: The value is less than 4!
//
// Note
// ----------------------------------------------------------------------------
// This implementation of guard function is now for the case of a list
//
// const fun = guard([g1, g2, gR], (x1, x2) => x1 + x2);
// where function g1 is guard for x1, function g2 for x2 and gR is guard for
// result of function (x1, x2).
//
// Also guard can be implemented as an object
//
// const fun = guard({ pre: { x: g1, y: g2 }, post: gR }, (x, y) => x + y);
//
// Guards
export function guard(guards, fn) {
if (!(isListFunctions(guards) && isFunction(fn) && guards.length == arguments[1].length + 1)) {
throw new Error('Size of validators list has to be same as a number of fn arguments.');
}
return (...args) => {
const a = splitEvery(1, [...args]);
const f = splitEvery(1, init(guards));
map((e) => (ap(head(e), last(e))), zip(f, a));
return apply(last(guards), [fn(...args)]);
};
}
// Validators
export function notZero(v) {
if (check.zero(v)) {
throw new Error('The value is zero!');
}
return v;
}
export function notEmptyString(v) {
if (check.emptyString(v)) {
throw new Error('The string is empty!');
}
return v;
}
export function isList(v) {
if (check.not.array(v)) {
throw new Error('The value is not a list!');
}
return v;
}
export function isEmptyList(v) {
if (check.emptyArray(v)) {
throw new Error('The list is an empty!');
}
return v;
}
export function isInteger(v) {
if (check.not.integer(v)) {
throw new Error('The value is not an integer!');
}
return v;
}
export function isFloat(v) {
if (check.not.float(v)) {
throw new Error('The value is not a float!');
}
return v;
}
export function isNumber(v) {
if (check.not.number(v)) {
throw new Error('The value is not a number!');
}
return v;
}
export const isLess = curry((x0, v) => {
if (check.less(v, x0)) {
throw new Error('The value is less than ' + x0 + '!');
}
return v;
});
export const isLessOrEqual = curry((x0, v) => {
if (check.lessOrEqual(v, x0)) {
throw new Error('The value is less or equal to ' + x0 + '!');
}
return v;
});
export const isGreater = curry((x0, v) => {
if (check.greater(v, x0)) {
throw new Error('The value is greater than ' + x0 + '!');
}
return v;
});
export const isGreaterOrEqual = curry((x0, v) => {
if (check.greaterOrEqual(v, x0)) {
throw new Error('The value is greater or equal to ' + x0 + '!');
}
return v;
});
export const inRange = curry((min, max, v) => {
if (check.not.inRange(min, max, v)) {
throw new Error('The value is not in range [' + min + ',' + max + ']!');
}
return v;
});
export const between = curry((min, max, v) => {
if (check.not.between(min, max, v)) {
throw new Error('The value is not in range (' + min + ',' + max + ')!');
}
return v;
});
// Helpers
export function isFunction(fn) {
if (check.not.function(fn)) {
throw new Error('The value is not function!');
}
return true;
}
export function isListFunctions(listFn) {
if (check.any(check.map(listFn, check.not.function))) {
throw new Error('The list does not contain functions!');
}
return true;
}