-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncjs.js
1217 lines (1056 loc) · 38.9 KB
/
funcjs.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// fix console access for IE
if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function () { };
if (!window.console.debug) window.console.debug = function () { };
if (!window.console.info) window.console.info = function () { };
/**
* Extends functional JavaScript in the browser. Escpecially suited for SAP/OpenUI5 applications.
* @depdency jQuery
*
*/
function capsFirst(capitalizeMe){
var caps = capitalizeMe.charAt(0).toUpperCase() + capitalizeMe.slice(1);
return caps;
}
/**
* defines a property path in the form of 'p1.p1-1.p1-1-2'
* if not defined in the context (optional, defaults to window).
* @return value of path within context
*/
var declareName, propertyGetSet;
declareName = propertyGetSet = function(sPath, oContext, cDelimeter){
cDelimeter = cDelimeter || '.';
if(sPath.charAt(0)===cDelimeter) sPath = sPath.substr(1);
return sPath.split(cDelimeter).reduce(function(holder, sPath){
holder[sPath] = holder[sPath] || {};
return holder[sPath];
}, oContext || window);
};
// JavaScript inheritance helper function
function extend(extension, parent, extPrototype, chainMethods) {
chainMethods = chainMethods ||[];
extPrototype = extPrototype || [];
function I() {};
I.prototype = parent.prototype;
var ctor = extension;
extension = function(){
parent.apply(this, arguments);
ctor.apply(this, arguments);
}
extension.prototype = new I;
extension.prototype.constructor = extension;
extension.superprototype = parent.prototype;
/*chainMethods.forEach(function(sName){
extension.prototype[sName] = function() {
try {
parent.prototype[sName].apply(this, arguments)
} catch (e) {
}
var method = extension[sName];
if (!!method) method.apply(this, arguments);
}
});*/
for (var prop in extPrototype) {
if( extPrototype.hasOwnProperty( prop ) ) {
if(inArray(prop,chainMethods)){
extension.prototype[prop] = function() {
try {
parent.prototype[prop].apply(this, arguments)
} catch (e) {
}
var method = extPrototype[prop];
if (!!method) method.apply(this, arguments);
}
}else {
extension.prototype[prop] = extPrototype[prop];
}
}
}
return extension;
};
function mashup(a, b, chainMethods){
chainMethods = chainMethods ||[];
for (var prop in b) {
var key = prop;
if(inArray(prop,chainMethods) && !!b[prop].call){
var aprop = a[key];
a[key] = function() {
try {
b[key].apply(this, arguments)
} catch (e) {
}
if (!!aprop && !!aprop.call) aprop.apply(this, arguments);
}
}else {
a[key] = b[key];
}
}
return a;
}
function setLocationHash(hash){
hash = hash.replace( /^#/, '' );
var fx, node = $( '#' + hash );
if ( node.length ) {
node.attr( 'id', '' );
fx = $( '<div></div>' )
.css({
position:'absolute',
visibility:'hidden',
top: '0px'
})
.attr( 'id', hash )
.appendTo( document.body );
}
document.location.hash = hash;
if ( node.length ) {
fx.remove();
node.attr( 'id', hash );
}
}
isEmpty = function(o) {
if(!isObject(o) && !isArray(o) && !!o) return false
else{
for ( var p in o ) {
if ( o.hasOwnProperty( p ) ) { return false; }
}
}
return true;
};
isNotEmpty = function(o){
return !isEmpty(o);
};
function isJsonStr(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
function trim (str, delimiter) {
var delimiter = delimiter && ('\\'+delimiter) || '';
var part = "["+delimiter+"\\s]";
var regex = new RegExp("^"+part+"*|"+part+"*$",'g');
return str.replace(regex, '');
}
/**
* Setting of object properties/deep-nested properties by path
* equivalent of setProperty
* @param obj The object on which to write the value
* @param value
* @param path '.' delimited string representing the property path on which to write
* @param delimiter
* @returns {*}
*/
function setProperty(obj, value, path, delimiter) {
if(!obj) throw new Error("Cannot set property on null, Must pass an object in the 1st argument");
if(arguments.length<3) throw new Error("First 3 parameters must be defined");
//if(!!!value) throw new Error("Must pass a value in the 2nd argument");
if(!arguments[2]){
obj = value;
return obj;
}
var parent = obj;
if(delimiter==undefined || !!!delimiter.length || delimiter.length !== 1) // if delimter is not provided or is invalid
delimiter = '.';
path = path.toString().split(delimiter);
for (var i = 0, pathLen = path.length -1; i < pathLen; i += 1) {
//console.log(parent);
if(path[i].length>0){
if(!!!parent[path[i]]){
parent[path[i]] = isNonNegativeNumber(path[i+1])?[]:{};
}
parent = parent[path[i]];
}
}
parent[path[path.length-1]] = value;
return obj;
}
/**
* return a values array for the given object keys
* @param {Object} obj
* @param {Array} keys
* @return {Array}
*/
function getValuesByKeys(obj, keys){
var values=[];
keys = keys || Object.keys(obj);
for(var i = 0, keysLen = keys.length; i<keysLen; i++){
var value = obj[keys[i]];
if(value) values.push(value);
}
return values;
}
/**
* returns object with a key/value pair for each url param
* @return {Object}
*/
function getUrlVars(url) {
var vars = {};
if(!url){
if(!!window.location.URLVars) return window.location.URLVars;
url = window.location.href;
window.location.URLVars = vars;
}
var parts = url.replace(/[?&]+([^=&#]+)=([^&#]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
/**
* Returns a property from an object by a given path from the root. Traverses getters as well.
* @param obj
* @param path
* @param bCall : boolean (default: true) Whether to call the path components if they are methods
* @param delimiter : string (default: ".")
* @return {*}
*/
function getObjProperty(obj, path, bCall, delimiter){
if(!arguments[1] || !obj)
return obj;
var parent = obj;
delimiter = typeof(bCall)==='string'? bCall: (delimiter || '.');
bCall = bCall===false? false: true;
var delimRegx = new RegExp("\\"+delimiter+'{2,}', "gi"); // fix multiple delimiters
path = path.replace(delimRegx, delimiter);
path = path.replace(new RegExp("\\"+delimiter+'$'), ''); // fix trailing delimiter
path = path.split(delimiter);
for (var i = 0, parts = path.length -1 ; i < parts ; i += 1) {
if(path[i].length>0){
//console.log(parent);
parent = !!parent[path[i]] && !!parent[path[i]].call? parent[path[i]](): parent[path[i]];
if(typeof(parent)=='undefined') return undefined;
}
}
var propPath = path[path.length-1];
return bCall && !!parent[propPath] && !!parent[propPath].call? parent[propPath](): parent[propPath];
}
function hashString(str) {
var hash = 5381,
i = str.length
while (i)
hash = (hash * 33) ^ str.charCodeAt(--i)
/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed
* integers. Since we want the results to be always positive, convert the
* signed int to an unsigned by doing an unsigned bitshift. */
return hash >>> 0;
}
function createUUID() {
// http://www.ietf.org/rfc/rfc4122.txt
var s = [];
var hexDigits = "0123456789ABCDEF";
for ( var i = 0; i < 32; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the
// clock_seq_hi_and_reserved to 01
var uuid = s.join("");
return uuid;
}
function mixin(dest, source, copyFunc) {
var name, s, i, empty = {};
for(name in source){
// the (!(name in empty) || empty[name] !== s) condition avoids copying properties in "source"
// inherited from Object.prototype. For example, if dest has a custom toString() method,
// don't overwrite it with the toString() method that source inherited from Object.prototype
s = source[name];
if(!(name in dest) || (dest[name] !== s && (!(name in empty) || empty[name] !== s))){
dest[name] = copyFunc ? copyFunc(s) : s;
}
}
return dest;
}
function clone(src) {
if(!src || typeof src != "object" || Object.prototype.toString.call(src) === "[object Function]"){
// null, undefined, any non-object, or function
return src; // anything
}
if(src.nodeType && "cloneNode" in src){
// DOM Node
return src.cloneNode(true); // Node
}
if(src instanceof Date){
// Date
return new Date(src.getTime()); // Date
}
if(src instanceof RegExp){
// RegExp
return new RegExp(src); // RegExp
}
var r, i, l;
if(src instanceof Array){
// array
r = [];
for(i = 0, l = src.length; i < l; ++i){
if(i in src){
r.push(clone(src[i]));
}
}
// we don't clone functions for performance reasons
// }else if(d.isFunction(src)){
// // function
// r = function(){ return src.apply(this, arguments); };
}else{
// generic objects
r = src.constructor ? new src.constructor() : {};
}
return mixin(r, src, clone);
}
/**
* This function returns a member function so must be called with a context containing the array in the sPathToArray path
* @param sPathToArray the path to array to be intiallized
* @param {string|string[]} keys the keys to initialized
* @param {value|value[]} values the initial values
* @param {fn} [optional] callback function called after intialize on the decorated context with the arguments for call on the returned function instance
* @returns {Function} the initilializing context dependent function
*/
function arrayInitDecorator(sPathToArray, keys, values, callback){
var args = {sPathToArray:sPathToArray, keys:keys, values: values, callback: callback};
// function that initializes the array in the given path with the corresponding keys and values
return function(){
arrayInit(getObjProperty(this, args.sPathToArray), args.keys, args.values);
/*getObjProperty(this, args.sPathToArray).forEach(function(arrayElement){
var arrElement = arrayElement;
args.keys.forEach(function(key,i){
setProperty(arrElement, args.values[i], key);
});
});*/
if(args.callback) args.callback.apply(this, arguments);
return this;
}
}
function arrayInit(arr, keys, values){
var args = {keys:(isArray(keys)? keys: [keys]), values: (isArray(values)? values: [values])};
arr.forEach(function(arrayElement){
var scopeArrElement = arrayElement;
args.keys.forEach(function(key,i){
setProperty(scopeArrElement, args.values[i], key);
});
});
}
function removeObjProperty(obj, path, delimiter){
if(!!!arguments[1])
return false;
var parent = obj;
if(delimiter==undefined || !!!delimiter.length || delimiter.length !== 1) // if delimter is not provided or is invalid
delimiter = '.';
path = path.split(delimiter);
for (var i = 0, parts = path.length -1; i < parts; i += 1) {
if(path[i].length>0){
//console.log(parent);
parent = parent[path[i]];
}
}
if(jQuery.isArray(parent)){
parent.splice(path[path.length-1],1)
}else{
try{
delete parent[path[path.length-1]];
}catch(ex){}
}
return obj;
}
function keysArrayToObj(aKeys, defaultValue){
var obj={};
for(var i= 0, len = aKeys.length; i<len; i++){
obj[aKeys[i]] = defaultValue;
}
return obj;
}
function diff(template, override) {
var ret = {};
for (var name in template) {
if (name in override) {
if (isObject(override[name]) && !isArray(override[name])) {
var diff = difference(template[name], override[name]);
if (!isEmpty(diff)) {
ret[name] = diff;
}
} else if (!isEqual(template[name], override[name])) {
ret[name] = override[name];
}
}
}
return ret;
}
var isEqual = function ( first, second ) {
/* First, let’s check whether the 2 values are both primitive types because if they are both primitive types, then we can just use the '===” operator to determine whether they are deeply equal to each other. */
/* The isPrimitive function is a helper function I defined below to help us out. */
if ( first === second ) {
/* If this part of the code is reached, it means that both values are primitive types. In that case, we can simply use our '===” operator to test for deep equality. */
return true;
}
/* Second, if we reach this part of the code, it means that either: 1) One of the values is a primitive type while the other one isn’t, or 2) Both values are reference types */
/* The else—if code directly below will test for the (1) scenario. */
else if ( isPrimitive(first) || isPrimitive(second) ) {
/* If the 2 values aren’t even the same types, then we can just return false. */
return false;
}
/* Finally, if we reach this part of the code, then we know that we’re in the (2) scenario mentioned above where we are dealing with 2 reference values. */
else {
/* Since we know that both values are reference types, we can get the keys for each of the objects and compare their keys array. */
var firstKeys = Object.keys( first );
var secondKeys = Object.keys( second );
/* Given these 2 keys arrays, we know that if they don’t even have the same lengths, then they must not be deeply equal. */
if ( firstKeys.length !== secondKeys.length ) {
return false;
}
/* At this point, we know that these 2 values have keys arrays that have the same length. We now want to check whether for EACH of the keys, both objects have the same corresponding values. */
else {
for ( var prop in first ) {
/* We know both keys arrays have the same length, but do they have the same keys in both arrays? This test checks for that. */
if ( second[prop] === undefined ) {
/* If we can’t the same key in both objects, then we know the two objects are not deeply equal. */
return false;
}
/* At this point, we see that both objects have the same particular key but are there corresponding values deeply equal? Well, would you look at that — this is a recursive problem! If we determine that for this particular key, the 2 object’s corresponding values are not deeply equal, then we know that the 2 object’s are also not deeply equal. */
else if (!isEqual(first[prop], second[prop])) {
return false;
}
}
/* If the code reaches this point, then we know we’ve gone through all the key / value pairs in both objects and all their key / value pairs are deeply equal — we can then return true. */
return true;
}
}
};
/* This is a helper function to help us determine whether a JavaScript value is a primitive type. */
var isPrimitive = function ( value ) {
return (value === void 0 || value === null || typeof value === 'number' ||
typeof value === 'boolean' ||
typeof value === 'string');
}
function isPlainObject(a){
return $.isPlainObject(a);
}
isObject = isPlainObject;
function isArray(value){
return value instanceof Array;
}
/**
* Takes extract an array/object by template from the src, will remove
* template from source if splice is set to true.
* @param {Object|Array} src
* @param {Object|Array} template
* @param {booleran} splice
* @return {Object|Array} extracted template from source
* @dependent Prototype library (_isArray, _isObject)
*/
function extractTemplate(src, template, splice){
if(!src) return undefined;
var extraction=isArray(src)?[]:{}, tmpval;
if(isArray(template) && isArray(src)){
for (var i = 0, srcLen = src.length ; i < srcLen ; i++) {
if(tmpval=extractTemplate(src[i],template[0], splice))
extraction[i]=tmpval;
}
}else{
for( var key in template ) {
if(isArray(template)) key = template[key];
if (src.hasOwnProperty(key)) {
if (isPlainObject(template[key]) || isArray(template[key])) {
if (isPlainObject(src[key]) || isArray(template[key]))
extraction[key] = extractTemplate(src[key], template[key], splice);
} else {
extraction[key] = src[key];
if (splice) {
delete src[key];
}
}
}
}
}
return extraction;
}
function mergeOverwriteDst(dst, src){
for (var key in src) {
if(src.hasOwnProperty(key))
dst[key]=src[key];
}
}
function updateObj(dst, src){
for (var key in dst) {
if(src.hasOwnProperty(key))
dst[key]=src[key];
}
}
/**
* takes a destination object and a source object and updates
* missing properties from source to destination, good for integrating defaults
* @param {Object} dst destination object
* @param {Object} objsource object
*/
mergeDiff = function (dst, obj) {
if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object')
for(prop in obj){
if(!dst.hasOwnProperty(prop)){
dst[prop] = obj[prop];
}
}
return dst;
};
function indexOfKeyValue(array,key,value){
for (var i = 0; i < array.length; i++) {
if(isEqual(array[i][key], value) )
return i;
}
return -1;
}
// discuss at: http://phpjs.org/functions/dirname/
// example 1: dirname('/etc/passwd');
// returns 1: '/etc'
// example 2: dirname('c:/Temp/x');
// returns 2: 'c:/Temp'
// example 3: dirname('/dir/test/');
// returns 3: '/dir'
var parentPath, dirname;
parentPath = dirname = function(sPath, upLevels){
if(!!upLevels){
return parentPath(parentPath(sPath), --upLevels);
}
return sPath.replace(/\\/g, '/').replace(/\/[^\/]*\/?$/, '');
}
function sort (prop, arr) {
prop = prop.split('.');
var len = prop.length;
arr.sort(function (a, b) {
var i = 0;
while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
return arr;
}
/**
*
* @param obj array or object to be queried for key value pair
* @param key
* @param value
* @param index a reference passed in that will be set to -1 if the pair is not found or the obj is not indexable, or the index otherwise
* @return {item: object, index: int}|null
*/
function findByKey(obj, key, value, bCall){
if(!key || isEqual(typeof(obj[key])==='function' && bCall ? obj[key].call(obj) : obj[key], value )) {
return {item: obj, index:null};
}
if(!!obj.length){
return arrayFindByKey(obj,key,value, bCall);
}
return null;
}
function arrayFindByKey(array,key,value, bCall){
if(!array || !array.length) return undefined;
for (var i = 0, arrLen = array.length; i < arrLen; i++) {
var item = array[i][key];
if(isEqual(typeof(item)==='function' && bCall ? item.call(array[i]) : item, value ) ){
return {item: array[i], index: i};
}
}
return {item: undefined, index: undefined};
}
/**
* arrayFindByPath allows you to search on an array using complex property paths e.g "size.height.getHeightByPixel"
* @param array array to search on
* @param {string} path
* @param {*} value
* @param {boolean} index whether to return an index or value
* @param {string} [delimiter = '.']
* @return {int|*} if index argument is set then the method will return the index of the found element, otherwise the element itself
*/
function arrayFindByPath(array,path,value, index, delimiter){
for (var i = 0, arrLen = array.length; i < arrLen; i++) {
var item = getObjProperty(array[i], path, delimiter);
if(isEqual(typeof(item)==='function' ? item.call(array[i]) : item, value ) )
return !!index? i: array[i];
}
return null;
}
function removeFromArray(arr, remove) {
remove = isArray(remove)? remove: [remove];
var what, a = arguments, L = remove.length, ax;
while (L > 0 && arr.length) {
what = remove[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
function isNumeric(test){
return jQuery.isNumeric(test);
}
function isNonNegativeNumber(test){
return isNumeric(test) && test>-1;
}
var validate={
/**
* first argument can be notifier a function taking a string message, the rest
* should be in form of {value: val|[val1, val2], validator: handler}
* @return {*}
*/
// validationError: function(){
// for(var i=0; arguments.length>i; i++){
// var validation = arguments[i];
// if(!isArray(validation.value)) validation.value = [validation.value];
// var isValid = validation.validator.apply(null, validation.value);
// if(!isValid){
// return (!!validation.message && validatione.message) || true;
// }
// }
// return false;
// },
/**
* Validates that a string contains only valid integer number.
* @param strValue String to be tested for validity
* @returns true if valid, otherwise false.
*/
isInt: function( strValue ) {
var objRegExp = /(^-?\d\d*$)/;
return objRegExp.test(strValue);
},
isNum: function(v) {
var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
return objRegExp.test(v);
},
/**
* Validates that a string contains only valid integer number.
* @param v String to be tested for validity
* @returns true if valid, otherwise false.
*/
isPosInt: function(v) {
var objRegExp = /(^[1-9]\d*$)/;
return objRegExp.test(v);
},
isNNInt: function(v) {
var objRegExp = /(^[0-9]\d*$)/;
return objRegExp.test(v);
},
min: function(v, p_min){
return v.hasOwnProperty('length') && !validateNum(v)? v.length: v >= p_min ;
},
max: function(v, p_max){
return v.hasOwnProperty('length') && !validateNum(v)? v.length: v <= p_max ;
}
};
function doIntersect(arr1,arr2){
try{
var i = arr1.length, item;
while(item = arr1[--i]){
if(inArray(item, arr2)) return true;
}
}catch(e){
console.debug(e);
}
return false;
}
/**
*
* @param pValue
* @param pArray
* @param {boolean} [true] Whether to return a boolean, if false it will return an index on success, -1 otherwise
* @return {boolean} true if elment is found in the array false otherwise
*/
function inArray(value, aArray, bIsBool){
bIsBool = bIsBool === false? false: true;
try{
var i = aArray.length, item;
while(item = aArray[--i]){
if(isEqual(item, value)) return bIsBool? true: i;
}
}catch(e){
console.debug(e);
}
return false;
}
/**
* Checks whether the value passed is defined and not null
* @param v
* @returns {boolean}
*/
function isDefined(v){
return v !== null && v !== undefined;
}
function isSet(v){
return !!v || v===0;
}
/**
* Take string input in varName and determine whether it is defined object in Javascript
* @param {String} varName
* @return {boolean}
*/
function isDefinedName(varName) {
var retStatus = false;
if (typeof varName == "string") {
try {
var arrCheck = varName.split(".");
var strCheckName = "";
for (var i= 0, arrLen = arrCheck.length ; i < arrLen; i++){
strCheckName = strCheckName + arrCheck[i];
//check wether this exist
if (typeof eval(strCheckName) == "undefined") {
//stop processing
retStatus = false;
break;
} else {
//continue checking next node
retStatus = true;
strCheckName = strCheckName + ".";
}
}
} catch (e) {
//any error means this var is not defined
retStatus = false;
}
} else {
throw "the varName input must be a string like myVar.someNode.anotherNode[]";
}
return retStatus;
}
function renameObjProperty(obj, oldName, newName){
if (obj.hasOwnProperty(oldName)) {
obj[newName] = obj[oldName];
delete obj[oldName];
}
return obj;
}
Function.prototype.defaultTo = function() {
var fn = this, defaults = arguments;
return function() {
var args = arguments,
len = arguments.length,
newArgs = [].slice.call(defaults, 0),
overrideAll = args[len - 1] === undefined;
for(var i = 0; i < len; i++) {
if(overrideAll || args[i] !== undefined) {
newArgs[i] = args[i];
}
}
return fn.apply(this, newArgs);
};
};
function DateInXDays(aDate, xDays){
newDate = new Date(aDate);
newDate.setDate(newDate.getDate()+xDays);
return newDate;
}
/**
* Returns annual week of the date
* @return {number} Annual week number
*/
Date.prototype.getWeek = function() {
var oneJanTime = Date.UTC(this.getUTCFullYear(),0,1);
var firstWeekStartTime = oneJanTime - ((new Date(oneJanTime)).getUTCDay())*86400000;
var thisTime = this.getTime();
var days = (thisTime - firstWeekStartTime) / 86400000;
var weeks = days/7;
return Math.ceil(weeks);
};
Date.prototype.dayStart = function() {
this.setHours(0,0,0,0);
return this;
}
Date.prototype.dayEnd = function() {
this.setHours(23,59,59,999);
return this;
}
/*
@vValue {Number}
@sType {String}
return {Date}
*/
Date.prototype.add = function(nValue, sType) {
var self = this;
var dt = new Date(self.getTime());
switch(sType) {
case 's':
case 'ss':
case 'second':
case 'seconds':
dt.setSeconds(dt.getSeconds() + nValue);
return dt;
case 'm':
case 'mm':
case 'minute':
case 'minutes':
dt.setMinutes(dt.getMinutes() + nValue);
return dt;
case 'h':
case 'hh':
case 'hour':
case 'hours':
dt.setHours(dt.getHours() + nValue);
return dt;
case 'd':
case 'dd':
case 'day':
case 'days':
dt.setDate(dt.getDate() + nValue);
return dt;
case 'M':
case 'MM':
case 'month':
case 'months':
dt.setMonth(dt.getMonth() + nValue);
return dt;
case 'y':
case 'yyyy':
case 'year':
case 'years':
dt.setFullYear(dt.getFullYear() + nValue);
return dt;
}
return dt;
};
/**
* Compare dates
* @param {Date} date
* @return {Number} Results: -1 = this date is earlier than @date, 0 = current date is same as @date, 1 = this date is later than @date
*/
Date.prototype.compare = function(date) {
var self = this;
var r = self.getTime() - date.getTime();
if (r === 0)
return 0;
if (r < 0)
return -1;
return 1;
};
/**
* Compare two dates
* @param {String or Date} d1
* @param {String or Date} d2
* @return {Number} Results: -1 = @d1 is earlier than @d2, 0 = @d1 is same as @d2, 1 = @d1 is later than @d2
*/
Date.compare = function(d1, d2) {
if (typeof(d1) === "string")
d1 = d1.parseDate();
if (typeof(d2) === "string")
d2 = d2.parseDate();
return d1.compare(d2);
};
/*
Format date to string
@format {String}
return {String}
*/
Date.prototype.format = function(format) {
var self = this;
var h = self.getHours();
var m = self.getMinutes().toString();
var s = self.getSeconds().toString();
var M = (self.getMonth() + 1).toString();
var yyyy = self.getFullYear().toString();
var d = self.getDate().toString();
var a = 'AM';
var H = h.toString();
if (h >= 12) {
h -= 12;
a = 'PM';
}
if (h === 0)
h = 12;
h = h.toString();
var hh = h.padLeft(2, '0');
var HH = H.padLeft(2, '0');
var mm = m.padLeft(2, '0');
var ss = s.padLeft(2, '0');
var MM = M.padLeft(2, '0');
var dd = d.padLeft(2, '0');
var yy = yyyy.substring(2);
return format.replace(/yyyy/g, yyyy).replace(/yy/g, yy).replace(/MM/g, MM).replace(/M/g, M).replace(/dd/g, dd).replace(/d/g, d).replace(/HH/g, HH).replace(/H/g, H).replace(/hh/g, hh).replace(/h/g, h).replace(/mm/g, mm).replace(/m/g, m).replace(/ss/g, ss).replace(/s/g, ss).replace(/a/g, a);
};
DateDiff= {
inDays: function(d1, d2) {
var mD1 = new Date(d1);
var mD2 = new Date(d2);
mD1.setHours(0,0,0,0);
mD2.setHours(0,0,0,0);