This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparseBoolean.js
65 lines (59 loc) · 1.68 KB
/
parseBoolean.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
const R = require('ramda');
const { isBoolean, getValue } = require('../predicates');
const {
createMemberValueNotBooleanWarning,
createMemberValueNotBooleanError,
} = require('./annotations');
/**
* Returns true iff the given member elements value is a boolean
* @param member {MemberElement}
* @returns {boolean}
* @private
*/
const isValueBoolean = R.compose(isBoolean, getValue);
/**
* Ensures that the given member value is a boolean, or return error
*
* @param member {MemberElement}
*
* @returns {ParseResult<MemberElement<BooleanElement>>}
* @private
*/
const parseOptionalBoolean = (context, name, member) => new context.namespace.elements.ParseResult([
R.unless(
isValueBoolean,
createMemberValueNotBooleanWarning(context.namespace, name),
member
),
]);
/**
* Ensures that the given member value is a boolean, or return error
*
* @param member {MemberElement}
*
* @returns {ParseResult<MemberElement<BooleanElement>>}
* @private
*/
const parseRequiredBoolean = (context, name, member) => new context.namespace.elements.ParseResult([
R.unless(
isValueBoolean,
createMemberValueNotBooleanError(context.namespace, name),
member
),
]);
/**
* Parse a boolean from a member
* @pram namespace
* @pram name {string}
* @pram required {boolean} - Whether the member is required, indicates if we return a warning or an error
* @pram member {MemberElement}
* @returns {ParseResult<MemberElement<BooleanElement>>}
* @private
*/
function parseBoolean(context, name, required, member) {
if (required) {
return parseRequiredBoolean(context, name, member);
}
return parseOptionalBoolean(context, name, member);
}
module.exports = R.curry(parseBoolean);