1
|
|
|
import { |
2
|
|
|
__, |
3
|
|
|
T, |
4
|
|
|
all, |
5
|
|
|
allPass, |
6
|
|
|
clamp, |
7
|
|
|
complement, |
8
|
|
|
cond, |
9
|
|
|
equals, |
10
|
|
|
evolve, |
11
|
|
|
gte, |
12
|
|
|
identity, |
13
|
|
|
length, |
14
|
|
|
lte, |
15
|
|
|
map, |
16
|
|
|
none, |
17
|
|
|
propEq, |
18
|
|
|
replace, |
19
|
|
|
split, |
20
|
|
|
take, |
21
|
|
|
tryCatch, |
22
|
|
|
unary, |
23
|
|
|
values |
24
|
|
|
} from 'ramda'; |
25
|
|
|
import { isEmail, isHexColor, isURL } from 'validator'; |
26
|
|
|
|
27
|
|
|
export const Integer = { name: 'Integer' }; |
28
|
|
|
export const Double = { name: 'Double' }; |
29
|
|
|
export const DateTime = { name: 'DateTime' }; |
30
|
|
|
export const Natural = { name: 'Natural' }; |
31
|
|
|
export const Char = length => ({ name: 'Char', length }); |
32
|
|
|
export const IntegerRange = (from, to) => ({ name: 'IntegerRange', from, to }); |
33
|
|
|
export const DoubleRange = (from, to) => ({ name: 'DoubleRange', from, to }); |
34
|
|
|
export const Money = { name: 'Money' }; |
35
|
|
|
export const String = { name: 'String' }; |
|
|
|
|
36
|
|
|
export const Color = { name: 'Color' }; |
37
|
|
|
export const Email = { name: 'Email' }; |
38
|
|
|
export const Checkbox = { name: 'Checkbox' }; |
39
|
|
|
export const OneOf = values => ({ name: 'OneOf', values }); |
40
|
|
|
export const Url = { name: 'Url' }; |
41
|
|
|
export const IntegerMultiRange = (from, to) => ({ name: 'IntegerMultiRange', from, to }); |
42
|
|
|
export const Calendar = { name: 'Calendar' }; |
43
|
|
|
export const AutoComplete = { name: 'AutoComplete' }; |
44
|
|
|
export const Location = { name: 'Location' }; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns the human-readable name of a type |
48
|
|
|
* |
49
|
|
|
* @author Marcelo Haskell Camargo |
50
|
|
|
* @param {String} type {anonymous} |
51
|
|
|
* @return {String} {anonymous} |
52
|
|
|
*/ |
53
|
|
|
export const getTypeName = cond([ |
54
|
|
|
[propEq('name', 'Char'), t => `Char(${t.length})`], |
55
|
|
|
[propEq('name', 'IntegerRange'), t => `IntegerRange(${t.from}, ${t.to})`], |
56
|
|
|
[propEq('name', 'DoubleRange'), t => `DoubleRange(${t.from}, ${t.to})`], |
57
|
|
|
[propEq('name', 'OneOf'), t => `OneOf([${t.values.join(', ')}])`], |
58
|
|
|
[propEq('name', 'IntegerMultiRange'), t => `IntegerMultiRange(${t.from}, ${t.to})`], |
59
|
|
|
[T, _.name] |
|
|
|
|
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
export const validator = { |
63
|
|
|
Color: isHexColor, |
64
|
|
|
Double: complement(isNaN), |
|
|
|
|
65
|
|
|
Email: unary(isEmail), |
66
|
|
|
Integer: complement(isNaN), |
67
|
|
|
IntegerMultiRange: (from, to) => allPass([ |
68
|
|
|
length & equals(2), |
69
|
|
|
none(isNaN), |
|
|
|
|
70
|
|
|
evolve([gte(__, from), lte(__, to)]) & values & all(identity)]), |
71
|
|
|
Money: complement(isNaN), |
72
|
|
|
Natural: lte(0), |
73
|
|
|
Range: (from, to) => clamp(from, to, _) === _, |
|
|
|
|
74
|
|
|
Url: unary(isURL) |
75
|
|
|
}; |
76
|
|
|
|
77
|
|
|
export const filter = { |
78
|
|
|
Calendar: date => new Date(date), |
79
|
|
|
Char: take, |
80
|
|
|
Double: parseFloat, |
|
|
|
|
81
|
|
|
Integer: parseInt(_, 10), |
|
|
|
|
82
|
|
|
IntegerMultiRange: split(' ') & map(parseInt(_, 10)), |
83
|
|
|
Money: tryCatch(replace(',', '.') & parseFloat, ~NaN) |
84
|
|
|
}; |
85
|
|
|
|