src/types.js   A
last analyzed

Complexity

Total Complexity 18
Complexity/F 1

Size

Lines of Code 104
Function Count 18

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 0
c 3
b 0
f 0
nc 1
dl 0
loc 104
rs 10
wmc 18
mnd 0
bc 0
fnc 18
bpm 0
cpm 1
noi 7

1 Function

Rating   Name   Duplication   Size   Complexity  
A types.js ➔ ??? 0 1 1
1
import {
2
    T,
3
    __,
4
    all,
5
    allPass,
6
    applyTo,
7
    clamp,
8
    complement,
9
    cond,
10
    contains,
11
    curry,
12
    equals,
13
    evolve,
14
    flip,
15
    gte,
16
    identity,
17
    is,
18
    length,
19
    lte,
20
    map,
21
    none,
22
    propEq,
23
    propOr,
24
    replace,
25
    split,
26
    take,
27
    tryCatch,
28
    unary,
29
    unless,
30
    values
31
} from 'ramda';
32
import { isEmail, isHexColor, isURL } from 'validator';
33
import { components } from './input';
34
35
export const Integer = { name: 'Integer' };
36
export const Double = { name: 'Double' };
37
export const DateTime = { name: 'DateTime' };
38
export const Natural = { name: 'Natural' };
39
export const Char = length => ({ name: 'Char', length });
40
export const IntegerRange = (from, to) => ({ name: 'IntegerRange', from, to });
41
export const DoubleRange = (from, to) => ({ name: 'DoubleRange', from, to });
42
export const Money = { name: 'Money' };
43
export const String = { name: 'String' };
0 ignored issues
show
Comprehensibility introduced by
You are shadowing the built-in type String. This makes code hard to read, consider using a different name.
Loading history...
44
export const Color = { name: 'Color' };
45
export const Email = { name: 'Email' };
46
export const Checkbox = { name: 'Checkbox' };
47
export const OneOf = values => ({ name: 'OneOf', values });
48
export const Url = { name: 'Url' };
49
export const IntegerMultiRange = (from, to) => ({ name: 'IntegerMultiRange', from, to });
50
export const Calendar = { name: 'Calendar' };
51
export const AutoComplete = { name: 'AutoComplete' };
52
export const Location = { name: 'Location' };
53
export const SelectBox = values => ({ name: 'SelectBox', values });
54
export const MultiSelectBox = values => ({ name: 'MultiSelectBox', values });
55
export const File = ({ name: 'File' });
56
57
/**
58
 * Returns the human-readable name of a type
59
 *
60
 * @author Marcelo Haskell Camargo
61
 * @param {String} type {anonymous}
62
 * @return {String} {anonymous}
63
 */
64
export const getTypeName = cond([
65
    [propEq('name', 'Char'), t => `Char(${t.length})`],
66
    [propEq('name', 'IntegerRange'), t => `IntegerRange(${t.from}, ${t.to})`],
67
    [propEq('name', 'DoubleRange'), t => `DoubleRange(${t.from}, ${t.to})`],
68
    [propEq('name', 'OneOf'), t => `OneOf([${t.values.join(', ')}])`],
69
    [propEq('name', 'SelectBox'), t => `SelectBox(${JSON.stringify(t.values)})`],
70
    [propEq('name', 'IntegerMultiRange'), t => `IntegerMultiRange(${t.from}, ${t.to})`],
71
    [propEq('name', 'MultiSelectBox'), t => `MultiSelectBox(${JSON.stringify(t.values)})`],
72
    [T, _.name]
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
73
]);
74
75
export const validator = {
76
    Color: isHexColor,
77
    Double: complement(isNaN),
0 ignored issues
show
Bug introduced by
The variable isNaN seems to be never declared. If this is a global, consider adding a /** global: isNaN */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
78
    Email: unary(isEmail),
79
    Integer: complement(isNaN),
80
    IntegerMultiRange: (from, to) => allPass([
81
        length & equals(2),
82
        none(isNaN),
0 ignored issues
show
Bug introduced by
The variable isNaN seems to be never declared. If this is a global, consider adding a /** global: isNaN */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
83
        evolve([gte(__, from), lte(__, to)]) & values & all(identity)]),
84
    Money: complement(isNaN),
85
    Natural: lte(0),
86
    OneOf: flip(contains),
87
    Range: (from, to) => clamp(from, to, _) === _,
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
88
    Url: unary(isURL)
89
};
90
91
export const validate = curry((type, value) =>
92
    propOr(~{}, type.name, components)
93
        | applyTo(type)
94
        | propOr(~true, 'validate')
95
        | applyTo(value));
96
97
export const filter = {
98
    Calendar: date => new Date(date),
99
    Char: take,
100
    Double: parseFloat,
0 ignored issues
show
Bug introduced by
The variable parseFloat seems to be never declared. If this is a global, consider adding a /** global: parseFloat */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
101
    Integer: parseInt(_, 10),
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
102
    IntegerMultiRange: unless(is(Array), split(' ') & map(parseInt(_, 10))),
103
    Money: tryCatch(replace(',', '.') & parseFloat, ~NaN)
104
};
105