-
Notifications
You must be signed in to change notification settings - Fork 2
/
mixin.js
executable file
·145 lines (137 loc) · 4.29 KB
/
mixin.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
module.exports = {
extractFacilityData: (facilityId, data, callback) => {
let facData = data.filter((dt) => {
return dt.facility_id === facilityId
})
return callback(facData)
},
translateAgeGroup: (ageGroup, callback) => {
var ageGroups = []
var ageOper = []
if (ageGroup.includes("||")) {
ageGroups = ageGroup.split("||")
} else if (ageGroup.includes("&&")) {
ageGroups = ageGroup.split("&&")
} else {
ageGroups = [ageGroup]
}
for (var index in ageGroups) {
var age = ''
var ageGroup = ageGroups[index]
if (ageGroup == '') {
return callback('', 'Empty Age Group')
}
//convert to lower case
ageGroup = ageGroup.toLowerCase()
//replace all empty strings
ageGroup = ageGroup.replace(/\s/g, '')
var dimension = null
var operator = ageGroup.charAt(0)
if (operator == '<' || operator == '>') {
// if the operator was >= or <= then get the = sign
if (ageGroup.charAt(1) === '=') {
operator += '='
}
for (let char of ageGroup) {
if (!isNaN(char)) {
age += char
}
}
if (age == '') {
return callback('', 'No age found on the age group ')
}
var dim = ageGroup.replace(age, '')
dim = dim.replace(/<|>/g, '')
if (dim.includes('week'))
dimension = 'DAY'
else if (dim.includes('month'))
dimension = 'MONTH'
else if (dim.includes('year'))
dimension = 'YEAR'
else
return callback('', 'Age group must contain either of the string Years or Months or Weeks')
// convert weeks to days
if (dim.includes('week')) {
age = age * 7
}
ageOper.push({
operator: operator,
age: age + ' ' + dimension
})
// for month, catch +30 days i.e if 3 months then get 3 and 1 day, 3 and 2 days etc
// if (dimension == 'MONTH') {
// ageOper.push({
// operator: '>=',
// age: age + ' ' + dimension
// })
// if(!ageGroup.includes('&&') && !ageGroup.includes('||')) {
// ageOper.push({
// operator: '<=',
// age: age + '.9 ' + dimension
// })
// }
// } else {
// ageOper.push({
// operator: operator,
// age: age + ' ' + dimension
// })
// }
} else if (operator === '=') {
// remove = sign
ageGroup = ageGroup.replace('=', '')
for (let char of ageGroup) {
if (!isNaN(char)) {
age += char
}
}
if (age == '') {
return callback('', 'No age found on the age group ')
}
var dim = ageGroup.replace(age, '')
dim = dim.trim()
dim = dim.toLowerCase()
if (dim.includes('week')) {
dimension = 'DAY'
var position = dim.indexOf("week")
} else if (dim.includes('month')) {
dimension = 'MONTH'
var position = dim.indexOf("month")
} else if (dim.includes('year')) {
dimension = 'YEAR'
var position = dim.indexOf("year")
} else {
return callback('', 'Age group must contain either of the string Years or Months or Weeks ')
}
// convert weeks to days
if (dim.includes('week')) {
age = age * 7
}
//make sure the position of dimension is at 0
if (position != 0) {
return callback('', 'Invalid Age Group Definition ')
}
// for month, catch +30 days i.e if 3 months then get 3 and 1 day, 3 and 2 days etc
if (dimension == 'MONTH') {
ageOper.push({
operator: '>=',
age: age + ' ' + dimension
})
if(!ageGroup.includes('&&') && !ageGroup.includes('||')) {
ageOper.push({
operator: '<=',
age: age + '.9 ' + dimension
})
}
} else {
ageOper.push({
operator: '=',
age: age + ' ' + dimension
})
}
} else {
return callback('', 'Unknown operation,expected age range e.g 10-12Years or operators < or > ')
}
}
callback(ageOper, false)
}
}