-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhc-utils.js
346 lines (331 loc) · 12 KB
/
hc-utils.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
(function() {
var MIN_AGE=18,
MAX_AGE=120,
MAX_WEIGHT=600,
MAX_BUDGET=99999,
ALPHA_NUMERIC=/^[a-z0-9]*$/i,
ALPHA=/^[a-z]*$/i,
PASSWORD=/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/, // 1 uppercase,1 lowrcase, 1 numeric , minlength=8,
SSN=/^(?!98765432[0-9])(?!666|000|9\d{2})\d{3}(?!00)\d{2}(?!0{4})\d{4}$/,
SSN_HYPHENATED=/^(?!987-65-432[0-9])(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$/,
validateMap = {};
var getQueryStringValue = function(key) {
return encodeURIComponent(unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1")));
},
removeItemFromArray = function(arr, item) {
if (arr && arr.length) {
var itemIndex = arr.indexOf(item);
if (itemIndex != -1)
arr.splice(itemIndex, 1);
}
},
formatDate = function(dateObj, dateFormat) {
var momentWrapper = moment(dateObj);
return momentWrapper.format(dateFormat);
},
isLessThan = function(a,b){
if(angular.isNumber(a) && angular.isNumber(b)){
return a<b;
}else {
if(a !== null && b !== null){
a=parseFloat(a);
b=parseFloat(b);
return a<b;
}
else
{
return true;
}
}
},
isLessThanEqual = function(a,b){
if(angular.isNumber(a) && angular.isNumber(b)){
return a<=b;
}else {
if(a !== null && b !== null){
a=parseFloat(a);
b=parseFloat(b);
return a<=b;
}
else
{
return true;
}
}
},
isGreaterThanEqual = function(a,b){
if(angular.isNumber(a) && angular.isNumber(b)){
return a>=b;
}else {
if(a !== null && b !== null){
a=parseFloat(a);
b=parseFloat(b);
return a>=b;
}
else
{
return true;
}
}
},
isGreaterThan = function(a,b){
if(angular.isNumber(a) && angular.isNumber(b)){
return a>b;
}else {
if(a !== null && b !== null){
a=parseFloat(a);
b=parseFloat(b);
return a>b;
}
else
{
return true;
}
}
},
isEqual = function(a,b){
return a===b;
},
isNotEqual = function(a,b){
return a!==b;
},
isValidSize = function(input,size){
//assuming input to be a string
return input.length===size;
},
isValidUserName = function(uname){
return uname.length>=6;
},
isValidAge = function(age){
return this.MIN_AGE<=age && this.MAX_AGE>=age;
},
isValidWeight = function(w){
return w>=0 && w<=this.MAX_WEIGHT;
},
getMomentDate = function(date,dateFormat){
if(moment.isMoment(date)){
return date;
}else if(date instanceof Date){
return moment(date);
}else{
return moment(date,dateFormat,true);
}
},
_isValidDate = function(date,dateFormat)
{
if(date){
var d = this.getMomentDate(date,dateFormat);
return d.isValid();
}else{
return true;
}
},
isValidDob = function(date,range,dateFormat){
if(angular.isObject(range))
{
this.MIN_AGE = angular.isDefined(range.minAge) ? range.minAge : 18;
this.MAX_AGE = angular.isDefined(range.maxAge) ? range.maxAge : 120;
}
var d = this.getMomentDate(date,dateFormat),
age = moment().diff(d, 'years', true),
currentDate = new Date(),
_date = new Date(date);
//'moment().diff()' function returns greater floating point value when dates are same
//below code will handle this case
if(currentDate.getDate() === _date.getDate())
{
age = parseInt(age);
}
return this.isValidAge(age);
},
isDateAfter = function(d,date,dateFormat){
//d : date to be check
//date : specfic date
if(typeof d === "undefined") return;
var _d=this.getMomentDate(d,dateFormat);
var _date=this.getMomentDate(date,dateFormat);
//if dates are invalid do not validate
if(!_d.isValid() || !_date.isValid()){
return true;
}
return _d.isAfter(_date,'day');
},
isDateAfterEqual = function(d,date,dateFormat){
if(typeof d === "undefined") return;
var _d=this.getMomentDate(d,dateFormat);
var _date=this.getMomentDate(date,dateFormat);
return _d.isAfter(_date,'day') || _d.isSame(_date,'day');
},
isDateBefore = function(d,date,dateFormat){
if(typeof d === "undefined") return;
var _d=this.getMomentDate(d,dateFormat);
var _date=this.getMomentDate(date,dateFormat);
return _d.isBefore(_date,'day');
},
isDateBeforeEqual = function(d,date,dateFormat){
if(typeof d === "undefined") return;
var _d=this.getMomentDate(d,dateFormat);
var _date=this.getMomentDate(date,dateFormat);
return _d.isBefore(_date,'day') || _d.isSame(_date,'day');
},
isValidBudget = function(b){
return b>=0 && b<=this.MAX_BUDGET;
},
isAlphaNumeric = function(str){
return this.ALPHA_NUMERIC.test(str);
},
isAlpha = function(str){
return this.ALPHA.test(str);
},
isValidPassword = function(str){
return this.PASSWORD.test(str);
},
isValidNonMilitaryHour = function(a){
return a>=0 && a<=12;
},
isValidMilitaryHour = function(a){
return a>=0 && a<=24;
},
isUniqueStringInGroup = function(group,id,value) {
this.validateMap[group] = this.validateMap[group] || {};
for (var i in this.validateMap[group]) {
if (this.validateMap[group][i] == value && i != id) {
return false;
}
}
this.validateMap[group][id] = value;
return true;
},
isValidSSN = function(ssn){
if(_.isString(ssn)){
if(ssn.indexOf('-') !== -1){
return this.SSN_HYPHENATED.test(ssn);
}
return this.SSN.test(ssn);
}
return true;
},
//inspired from http://stackoverflow.com/questions/5900840/post-nested-object-to-spring-mvc-controller-using-json
serializeJSON = (function() {
// copy from jquery.js
var r20 = /%20/g,
rbracket = /\[\]$/,
serializeJSON = function(a) {
var s = [],
add = function(key, value) {
// If value is a function, invoke it and return its value
value = _.isFunction(value) ? value() : value;
s[s.length] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
};
for (var prefix in a) {
buildParams(prefix, a[prefix], add);
}
// Return the resulting serialization
return s.join("&").replace(r20, "+");
}
/* private method*/
function buildParams(prefix, obj, add) {
if (_.isArray(obj)) {
// Serialize array item.
_.each(obj, function(v, i) {
if (rbracket.test(prefix)) {
// Treat each array item as a scalar.
add(prefix, v);
} else {
buildParams(prefix + "[" + (typeof v === "object" || _.isArray(v) ? i : "") + "]", v, add);
}
});
} else if (obj != null && typeof obj === "object") {
// Serialize object item.
for (var name in obj) {
buildParams(prefix + "." + name, obj[name], add);
}
} else {
// Serialize scalar item.
add(prefix, obj);
}
};
return serializeJSON;
})();
/**
* Tries to decode the URI component without throwing an exception.
*
* @private
* @param str value potential URI component to check.
* @returns {boolean} True if `value` can be decoded
* with the decodeURIComponent function.
*/
function tryDecodeURIComponent(value) {
try {
return decodeURIComponent(value);
} catch (e) {
// Ignore any invalid uri component
}
}
/**
* Parses an escaped url query string into key-value pairs.
* @returns {Object.<string,boolean|Array>}
*/
function parseKeyValue(/**string*/keyValue) {
var obj = {}, key_value, key;
_.each((keyValue || "").split('&'), function(keyValue) {
if (keyValue) {
key_value = keyValue.replace(/\+/g,'%20').split('=');
key = tryDecodeURIComponent(key_value[0]);
if (!_.isUndefined(key)) {
var val = !_.isUndefined(key_value[1]) ? tryDecodeURIComponent(key_value[1]) : true;
if (!hasOwnProperty.call(obj, key)) {
obj[key] = val;
} else if (_.isArray(obj[key])) {
obj[key].push(val);
} else {
obj[key] = [obj[key],val];
}
}
}
});
return obj;
}
window["HCUtils"] = {
SSN: SSN,
SSN_HYPHENATED: SSN_HYPHENATED,
MIN_AGE: MIN_AGE,
MAX_AGE: MAX_AGE,
MAX_WEIGHT: MAX_WEIGHT,
MAX_BUDGET: MAX_BUDGET,
ALPHA_NUMERIC: ALPHA_NUMERIC,
ALPHA: ALPHA,
PASSWORD: PASSWORD, // 1 uppercase,1 lowrcase, 1 numeric , minlength=8
validateMap: validateMap,
getQueryStringValue: getQueryStringValue,
removeItemFromArray: removeItemFromArray,
formatDate: formatDate,
isLessThan: isLessThan,
isLessThanEqual: isLessThanEqual,
isGreaterThanEqual: isGreaterThanEqual,
isGreaterThan: isGreaterThan,
isEqual: isEqual,
isNotEqual: isNotEqual,
isValidSize: isValidSize,
isValidUserName: isValidUserName,
isValidAge: isValidAge,
isValidWeight: isValidWeight,
getMomentDate: getMomentDate,
_isValidDate: _isValidDate,
isValidDob: isValidDob,
isDateAfter: isDateAfter,
isDateAfterEqual: isDateAfterEqual,
isDateBefore: isDateBefore,
isDateBeforeEqual: isDateBeforeEqual,
isValidBudget: isValidBudget,
isAlphaNumeric: isAlphaNumeric,
isAlpha: isAlpha,
isValidPassword: isValidPassword,
isValidNonMilitaryHour: isValidNonMilitaryHour,
isValidMilitaryHour: isValidMilitaryHour,
isUniqueStringInGroup: isUniqueStringInGroup,
isValidSSN: isValidSSN,
serializeJSON: serializeJSON,
parseKeyValue: parseKeyValue
};
})();