This repository was archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathhelpers.js
147 lines (128 loc) · 3.1 KB
/
helpers.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
/* eslint-disable no-restricted-syntax */
/* eslint-disable guard-for-in */
import {
daysAgo,
today,
extractDate,
validDate,
} from '../components/Section/History/dateranges'
export const validGenericTextfield = (obj) => {
if (!obj || !obj.value) {
return false
}
return true
}
/**
* Validates a phone number
*/
export const validPhoneNumber = (phone, opts = { numberType: false }) => {
if (!phone) {
return false
}
if (phone.noNumber) {
return true
}
if (!phone.numberType && opts.numberType) {
return false
}
if (!phone.timeOfDay) {
return false
}
if (!phone.number) {
return false
}
const trimmed = parseInt(phone.number, 10) !== 0
? `${parseInt(phone.number, 10)}`
: phone.number.trim()
switch (phone.type) {
case 'Domestic':
return trimmed.length === 10
case 'International':
return trimmed.length > 10
default:
return false
}
}
/**
* Validates a date
*/
export const validDateField = (obj = {}) => validDate(obj)
export const withinSevenYears = (from, to) => {
const sevenYearsAgo = daysAgo(today, 365 * 7)
const fromDate = extractDate(from)
const toDate = extractDate(to)
if (
(fromDate && fromDate >= sevenYearsAgo)
|| (toDate && toDate >= sevenYearsAgo)
) {
return true
}
return false
}
/**
* Checks if a branch value is within the set of possible valid yes/no values.
*/
export const validBranch = (value, yesValue = 'Yes', noValue = 'No') => (
value === yesValue || value === noValue
)
export const battery = (tests, validator, fn) => (
tests.forEach((test) => {
// This allows us to pass whatever test parameters in to the validation
// function as we see fit just as long as the match the order in the
// function signature
const props = []
for (const p in test) {
props.push(test[p])
}
// eslint-disable-next-line new-cap
expect(new validator(...props)[fn]()).toBe(test.expected)
})
)
export const validSSN = (ssn) => {
if (ssn.notApplicable === true) {
return true
}
// Legacy system only excluded explicit values
const fullSSN = `${ssn.first}-${ssn.middle}-${ssn.last}`
const legacyInvalid = ['999-99-9999', '123-45-6789']
if (legacyInvalid.some(x => x === fullSSN)) {
return false
}
return (
!!ssn.first
&& !!ssn.middle
&& !!ssn.last
&& ssn.first.length === 3
&& ssn.middle.length === 2
&& ssn.last.length === 4
)
}
export const nameIsEmpty = (name) => {
switch (true) {
case !name:
case !name.first && !name.middle && !name.last:
return true
default:
return false
}
}
export const pickDate = (dates, max = true) => {
const buildsDates = dates.map(extractDate)
const findMinDate = (finalDate, dateItem) => {
if (finalDate <= dateItem) {
return finalDate
}
return dateItem
}
const findMaxDate = (finalDate, dateItem) => {
if (finalDate >= dateItem) {
return finalDate
}
return dateItem
}
if (max) {
return buildsDates.reduce(findMaxDate)
}
return buildsDates.reduce(findMinDate)
}
export const alphaNumericRegEx = '^[a-zA-Z0-9]*$'