-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpUtils.js
559 lines (493 loc) · 16.6 KB
/
dpUtils.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/**
* dpUtils Custom (alias dp)
* v2.0.0
*
* Author and contributors:
*/
/**
* @name dpUtils
* @description The dpUtils library
* @namespace dpUtils
* @type module
*/
var dpUtils = (function (fn) {
var BENCHMARK_MESSAGE = 'Function returned in {time}ms',
INVALID_INPUT = 'Invalid input',
COOKIE_GET = 'Cookie {name} is currently set to value {value}',
COOKIE_SET = 'Cookie {name} has been set to {value} with expiry {expiry} for domain {domain}',
COOKIE_REMOVED = 'Cookie {name} has been removed',
ADD_TRIM_TO_IE_INIT = 'addTrimToIE has been initialised';
/**
* @name addTrimToIe
* @description Add trim functionality to strings for IE 8 and below
* @memberof dpUtils
* @function
*/
var addTrimToIE = function () {
var namespace = 'dpUtils.addTrimToIE';
try {
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
fn.log(ADD_TRIM_TO_IE_INIT, namespace);
}
} catch (exception) {
fn.logException(exception, namespace);
}
return null;
};
// Default configuration
var utilsConfig = {
logging: false,
benchmarking: false
};
// Define Alias
window.dp = fn;
/**
* @name Init
* @description Initialises dpUtils by providing a config object
* @namespace dpUtils.init
* @param {object} [config] An object containing configuration keys
* @function
*/
fn.init = function (config) {
// Set config
if (typeof config === 'object') {
// Add defaults to input config object if none are set
for (var key in utilsConfig) {
if (!config[key]) {
config[key] = utilsConfig[key];
}
}
// Reference the input config object
utilsConfig = config;
setAlias(utilsConfig.alias);
// Initialise optional modules
if (config.addTrimToIE) {
addTrimToIE();
}
}
};
/**
* @name Log
* @description Logs a message to the console
* @memberof dpUtils
* @param {string} message The message to log
* @param {string} namespace The namespace to be attached to this message
* @function
*/
fn.log = function (message, namespace) {
// Abort if logging is turned off or if console is not available
if (!fn.config.logging || !window.console) {
return;
}
// Input type validation
if (typeof message !== 'object' && typeof message !== 'string' || typeof namespace !== 'string') {
console.error('[utils.log | ' + namespace.toString() + '] ' + INVALID_INPUT);
return null;
}
// Input defaults
namespace = namespace || '';
if (typeof message === 'object') {
console.log(message);
} else {
// Log namespace and message
var output = this.timeStamp() + ' [' + namespace + '] ' + message;
console.log(output);
}
return;
};
/**
* @name logException
* @description Logs an exception to the console
* @memberof dpUtils
* @param {object} exception The exception to log
* @param {string} namespace The namespace to be attached to this exception
* @function
*/
fn.logException = function (exception, namespace) {
// If console is not available
if (!window.console) {
return;
}
// Input type validation
if (typeof exception !== 'object' && typeof exception !== 'string' || typeof namespace !== 'string') {
console.error('[utils.logException | ' + namespace.toString() + '] ' + INVALID_INPUT);
return null;
}
// Input defaults
namespace = namespace || '';
// Log namespace and message
console.error('[' + namespace + '] ' + exception.toString());
return;
};
/**
* @name timeStamp
* @description Returns a timestamp with the current date and time
* @memberof dpUtils
* @function
* @returns {string}
*/
fn.timeStamp = function () {
var date = new Date(),
day = date.getDate(),
month = date.getMonth(),
year = date.getFullYear(),
hour = date.getHours(),
minutes = date.getMinutes(),
seconds = date.getSeconds(),
miliseconds = date.getMilliseconds(),
template = 'day/month/year - hour:minutes:seconds.miliseconds'
return template
.replace('day', day)
.replace('month', month)
.replace('year', year)
.replace('hour', hour)
.replace('minutes', minutes)
.replace('seconds', seconds)
.replace('miliseconds', miliseconds);
};
/**
* @name Benchmark
* @description Measures execution time (in ms)
* @memberof dpUtils
* @param {string} namespace The namespace to be attached to this benchmark
* @constructor
*/
fn.Benchmark = function (namespace) {
this.namespace = namespace;
if (fn.config.benchmarking) {
this.start = new Date().getTime();
}
};
fn.Benchmark.prototype = (function () {
return {
constructor: fn.Benchmark,
/**
* @name finish
* @description Finish the benchmark
* @memberof dpUtils.Benchmark
* @function
*/
finish: function () {
if (fn.config.benchmarking) {
var finish = new Date().getTime();
var duration = finish - this.start;
var message = BENCHMARK_MESSAGE.replace('{time}', duration.toString());
fn.log(message, this.namespace);
}
}
}
})();
/**
* @name cookies
* @description The cookies library
* @memberof dpUtils
* @type object
*/
fn.cookies = {
/**
* @name get
* @description Get cookie
* @memberof dpUtils.cookies
* @param {string} name The cookie name
* @returns {string} cookie value The cookie value
* @function
*/
get: function (name) {
var namespace = 'dpUtils.cookies.get';
try {
if (typeof name !== 'string') {
throw new Error(INVALID_INPUT);
}
// Get cookies
var cookies = document.cookie.split(';');
for (var i = 0, length = cookies.length; i < length; i++) {
var cookie = cookies[i];
if (cookie.indexOf(name + '=') > -1) { // Cookie found
var cookieValue = cookie.split('=')[1]; // TODO use trim
fn.log(COOKIE_GET
.replace('{name}', name)
.replace('{value}', cookieValue),
namespace);
return cookieValue;
}
}
return '';
} catch (exception) {
fn.logException(exception, namespace);
}
return null;
},
/**
* @name set
* @description Set cookie
* @memberof dpUtils.cookies
* @param {string} name The cookie name
* @param {string} value The cookie value
* @param {number} [expiry] The cookie expiry time (in ms), default is zero
* @param {string} [domain] The domain the cookie applies to, default is '/'
* @function
*/
set: function (name, value, expiry, domain) {
var namespace = 'dpUtils.cookies.set';
try {
// Input validation
if (typeof name !== 'string' ||
typeof value !== 'string' ||
expiry && typeof expiry !== 'number' ||
domain && typeof domain !== 'string') {
throw new Error(INVALID_INPUT);
}
var expires = '';
if (expiry) {
var expiryDate = new Date();
expiryDate.setTime(expiryDate.getTime() + expiry);
expires = '; expires=' + expiryDate.toUTCString();
} else {
// Prevents logging 'undefined' for expiry
expiry = -1;
}
if (!domain) {
domain = '/';
}
// Set cookie
document.cookie = name + '=' + value + expires + '; path=' + domain;
fn.log(COOKIE_SET
.replace('{name}', name)
.replace('{value}', value)
.replace('{expiry}', expiry.toString())
.replace('{domain}', domain),
namespace);
} catch (exception) {
fn.logException(exception, namespace);
}
},
/**
* @name remove
* @description Remove cookie
* @memberof dpUtils.cookies
* @param {string} name The cookie name
* @function
*/
remove: function (name) {
var self = this,
namespace = 'dpUtils.cookies.remove';
try {
if (typeof name !== 'string') {
throw new Error(INVALID_INPUT);
}
// Remove cookie by setting it with a negative expiry
self.set(name, '', -1);
fn.log(COOKIE_REMOVED.replace('{name}', name), namespace);
} catch (exception) {
fn.logException(exception, namespace);
}
},
/**
* @name isCookieSet
* @description Checks whether a cookie is set
* @memberof dpUtils.cookies
* @param {string} name
* @returns {boolean | null} Is cookie set?
*/
isCookieSet: function (name) {
var namespace = 'dpUtils.cookies.isCookieSet';
try {
if (typeof name !== 'string') {
throw new Error(INVALID_INPUT);
}
// Find cookie
var isCookieSet = false;
// We cannot check for "name=" as IE8 does not add the = sign
var cookies = '; ' + document.cookie;
if (cookies.indexOf('; ' + name) > -1) {
isCookieSet = true;
}
return isCookieSet;
} catch (exception) {
fn.logException(exception, namespace);
}
return null;
}
}
/**
* @name isNumber
* @namespace dpUtils.isNumber
* @description Checks if a given value is a valid number
* @function
* @returns {boolean}
*/
fn.isNumber = function (number) {
return !isNaN(parseFloat(number)) && isFinite(number) && number >= 0;
};
/**
* @name toBoolean
* @namespace betslipApp.commonServices.toBoolean
* @description Serialises terms related that equal true
* @function
* @returns {boolean}
*/
fn.toBoolean = function (property) {
var booleanProperty = (typeof(property) === 'string') ? property.toLowerCase() : property;
var values = ['true', 'y', 'yes', '1', true];
return (values.indexOf(booleanProperty) > -1);
};
/**
* @name parseModel
* @namespace dpUtils.parseModel
* @description Parses a stringified model
* @function
* @returns {object}
*/
fn.parseJson = function (jsonString) {
var namespace = 'dpUtils.parseJson';
try {
return (typeof(jsonString) === 'string') ? JSON.parse(jsonString) : jsonString;
} catch (exception) {
fn.logException(exception, namespace);
fn.logException(jsonString, namespace);
return {};
}
};
/**
* @name getDecimalMark
* @namespace dpUtils.getDecimalMark
* @description Gets the decimal mark used
* @function
* @returns {string}
*/
fn.getDecimalMark = function (stringValue) {
var mark = stringValue.match(/[\.,']/); // Gets the current decimal mark used
return (mark) ? mark[0] : '.';
};
/**
* @name normalizeDecimal
* @namespace dpUtils.normalizeDecimal
* @description Converts any decimal mark type to dot (.)
* @function
* @returns {string}
*/
fn.normalizeDecimal = function (stringValue, decimalMark) {
if (stringValue.indexOf(decimalMark)){
return stringValue.replace(decimalMark, '.');
}
return stringValue;
};
/**
* @name ordinalWithSuffix
* @namespace dpUtils.ordinalWithSuffix
* @description Returns any number as ordinal number with the suffix
* @function
* @returns {string}
* @example 2 returns "2nd"
*/
fn.ordinalWithSuffix = function (number) {
var remainder = number % 10;
if (remainder == 1 && number != 11) {
return number + "st";
}
if (remainder == 2 && number != 12) {
return number + "nd";
}
if (remainder == 3 && number != 13) {
return number + "rd";
}
return number + "th";
};
/**
* @name isArray
* @namespace dpUtils.isArray
* @description Checks if object is of type array
* @function
* @returns {boolean}
*/
fn.isArray = function (arrayToCheck) {
return typeof arrayToCheck === 'object' && arrayToCheck instanceof Array;
};
/**
* @name moveArrayPosition
* @namespace dpUtils.moveArrayPosition
* @description Changes an element position in an array
* @function
* @returns {object}
*/
fn.moveArrayPosition = function (arrayList, oldIndex, newIndex) {
var namespace = 'dpUtils.moveArrayPosition';
if (fn.isArray(arrayList)) {
if (newIndex >= arrayList.length) {
newIndex = arrayList.length -1;
}
arrayList.splice(newIndex, 0, arrayList.splice(oldIndex, 1)[0]);
return arrayList;
}
fn.logException('First argument is not an Array', namespace);
return [];
};
/**
* @name isEmpty
* @namespace dpUtils.isEmpty
* @description Checks if an object is empty
* @function
* @returns {boolean}
*/
fn.isEmpty = function (obj) {
// null and undefined are "empty"
if (obj == null) {
return true;
}
// Assume if it has a length property with a non-zero value
// that that property is correct.
if (obj.length > 0) {
return false;
}
if (obj.length === 0) {
return true;
}
// Otherwise, does it have any properties of its own?
// Note that this doesn't handle
// toString and valueOf enumeration bugs in IE < 9
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return false;
}
}
return true;
};
/**
* @name remainingTime
* @namespace dpUtils.remainingTime
* @description Returns the Time difference between a future date and the current date, in milliseconds.
* @function
* @returns {number}
*/
fn.remainingTime = function (time) {
var currentDate = new Date(),
endDate = new Date(time);
return endDate.getTime() - currentDate.getTime();
};
/**
* @name createHashTable
* @namespace dpUtils.createHashTable
* @description Creates a hash table of given array of objects. Requires a defined property name.
* @function
* @params {object, string}
*/
fn.createHashTable = function (arrayList, propertyName) {
var namespace = 'dpUtils.createHashTable',
hashTable = {};
if (propertyName && typeof propertyName === 'string') {
if (fn.isArray(arrayList) && !!arrayList.length) {
for (var i = 0; i < arrayList.length; i++) {
hashTable[arrayList[i][propertyName]] = arrayList[i];
}
}
} else {
fn.logException('Please provide a string type propertyName', namespace);
}
return hashTable;
};
return fn;
})(dpUtils || {});