-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparsers.js
68 lines (62 loc) · 1.65 KB
/
parsers.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
var exports = module.exports = {}
/*
* this was a probably uncesary project that Ryan did in order to help validate user input
* for example, the 'validOrDefault' function is used for taking a user input, checking if its valid
* and then providing a default value if the user value isn't valid
* it's used in pixel.js sparsleey to validate .config options for node-red
*
*/
/*
* Used to convert types in validateOrDefault, easy to add more if needed
*/
let converters = new Map([
['number', function(num) {
let out = Number(num)
//Number() doesn't output NaN when given an empty string
if(num === '') return false
if(isNaN(out)) return false
return out
}]
])
/*
* Typically just for parsing HTML input fields, checks if a value is valid
* based on validation function if provided, otherwise checks if it's the right
* data type, if not right type or validated, it returns the default value
*/
exports.validateOrDefault = function(input, d, v = false)
{
let parsed
//if we have a validation function, use it
if(v !== false)
{
parsed = v(input) ? input : d
}
//if no validation function, check if input is the right type
else if(typeof input === typeof d)
{
parsed = input
}
//if not, try to convert it to the right type
else if(converters.has(typeof d))
{
parsed = converters.get(typeof d)(input)
if(parsed === false) parsed = d
}
//if we can't do anything, use default value
else
{
parsed = d
}
return parsed
}
/*
* Validation function for RGB sequences
*/
exports.validateRGBSequence = function(str)
{
if(str.includes('R') && str.includes('G') && str.includes('B') && str.length === 3) {
return true
} else {
return false
}
}