forked from JO3-W3B-D3V/ToolKit-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolKit.js
More file actions
2114 lines (1829 loc) · 58.1 KB
/
ToolKit.js
File metadata and controls
2114 lines (1829 loc) · 58.1 KB
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
/**
* @file
* @name ToolKit.js
* @author Joseph Evans <[email protected]>
* @version 0.0.2
* @license MIT-License
* @copyright Joseph Evans 2018
* @description the purpose of this file is to allow for
* some pretty useful developer tools, however
* this file does rely on other files that have been
* published in this project, version 0.0.2 has begun now,
* which means that you should see added features and functions
* to the file, keep note that all changes will be noted,
* if you scroll to the bottom of the file, you'll see a few functions
* using the '@since'.
* @todo add documentation
* @todo carry out detailed testing
* @todo break components up into their own files for dev purposes
*/
/**
* @global
* @function
* @name log
* @param {*} args this is the data that you want to log
* @description the purpose of this function is to save typing console.log,
* with this function it is as simple as going log(something);
*/
window.log = function (args) {
try {
console.log(args);
} catch (e) {
alert(args);
}
};
/**
* @global
* @function
* @name makeTemplateObject
* @param {String} key this is simply the key of the object that
* you'd like to define
* @description the purpose of this function is to simply generate a basic object
* with some basic properties already assigned such as a get ans set method
*/
window.makeTemplateObject = function (key) {
if (typeof key != "string") {
return console.log("Please provide a string.");
}
if (key == null || key == "values") { key = "data"; }
var obj = {};
Object.defineProperty(obj, key, {
values: {},
enumerable: false,
configurable: false,
get: function () { return this.values; },
set: function (data) { return this.values = data; }
});
return obj;
};
/**
* @global
* @function
* @name addEventHandler
* @param {Object} obj this is the object you're trying
* to target
* @param {String} eventType this is the event which you wish to
* fire the callback function on
* @param {Function} callback this is the function you wish to run
* once a certain event has occurred
* @description the purpose of this function is to add an event
* handler to some object, whether it's the document
* or just some dom object
*/
window.addEventHandler = function (obj, eventType, callback) {
if (!isDefined(obj)) { return; }
if (obj.addEventListener) {
obj.addEventListener(eventType, callback, false);
} else if (obj.attachEvent) {
obj.attachEvent('on' + eventType, callback);
} else {
obj["on"+eventType] = callback;
}
};
/**
* @global
* @function
* @name removeEventHandler
* @param {Object} obj this is the object that you wish to
* remove the given event from
* @param {String} eventType this is the event that you want to
* stop listening for
* @param {Function} callback this is the function that you wish
* to stop running
* @description the purpose of this function is to remove an event
* handler, this should in theory help clear up some
* memory
*/
window.removeEventHandler = function (obj, eventType, callback) {
if (!isDefined(obj)) { return; }
if (obj.removeEventListener) {
obj.removeEventListener(eventType, callback, false);
} else { obj[eventType + callback] = null; }
};
/**
* @global
* @function
* @name ready
* @param {Function} callback this is the function you wish to run
* once the dom is ready
* @description the purpose of this function is to run some js once the
* dom has been loaded
*/
window.ready = function (callback) {
try {
setTimeout(addEventHandler(document, "DOMContentLoaded", callback), 20);
} catch (e) {
addEventHandler(document, "DOMContentLoaded", callback);
log(e.message);
}
};
/**
* @global
* @function
* @name isDefined
* @param {*} obj this is the variable that you want to check
* if it's defined or not
* @return {Boolean}
* @description the purpose of this function is to see whether or not
* a given variable is defined or not
*/
window.isDefined = function (obj) {
if (obj != null && typeof obj != 'undefined' && obj != '') { return true; }
else { return false; }
};
/**
* @global
* @function
* @name isDOM
* @param {*} obj this is the data that you want to
* test against
* @return {Boolean}
* @description the purpose of this function is to test if
* the given variable is a html element or not
*/
window.isDOM = function (obj) {
// this works for newer browsers
try { return obj instanceof HTMLElement; }
// this works for older browsers
catch (e) {
return (typeof obj==="object") &&
(obj.nodeType===1) && (typeof obj.style === "object") &&
(typeof obj.ownerDocument ==="object");
}
};
/**
* @global
* @function
* @name isList
* @param {*} obj this is the variable that you want to check
* whether it's a list or not
* @return {Boolean}
* @description the purpose of this function is to check if the given
* variable is a list or not
*/
window.isList = function (obj) {
if ( obj instanceof HTMLCollection
|| obj instanceof Array
|| (isDefined(obj.length) && obj.length > 1)
&& !isDOM(obj)
) { return true; }
else { return false; }
};
/**
* @global
* @function
* @name isIE
* @return {Boolean}
* @description the purpose of this function is to do a basic test as to whether
* or not the user is using IE or not, because we all know how
* annoying
* IE can be
*/
window.isIE = function () {
try { return navigator.userAgent.match(/Trident/g)
|| navigator.userAgent.match(/MSIE/g);
} catch (e) { log(e); return false; }
};
/**
* @global
* @function
* @name byID
* @param {String} ID this is the id of the element that you're
* trying to target
* @return {Object}
* @description the purpose of this function is to save typing the full
* "document.getElementById"
*/
window.byID = function (ID) { return document.getElementById(ID); };
/**
* @global
* @function
* @name byTag
* @param {String} tag this is the tag that you're looking for
* @return {HTMLCollection}
* @description the purpose of this function is to save typing the full
* "document.getElementByTagName"
*/
window.byTag = function(tag) { return document.getElementsByTagName(tag); };
/**
* @global
* @function
* @name byClass
* @param {String} className this is the class that you're
* searching for
* @return {HTMLCollection}
* @description the purpose of this function is to get all elements in the dom
* that have the given class name
*/
window.byClass = function (className) {
return document.getElementsByClassName(className);
};
/**
* @global
* @function
* @name getType
* @param {*} obj this is the variable that you want to get the data
* type of
* @return {String}
* @description the purpose of this function is to discover what data type
* the given variable is
*/
window.getType = function (obj) { return typeof obj; };
/**
* @global
* @function
* @name
* @param {*} obj this is the variable that you're
* testing
* @param {String} type this is the type that you're testing the
* given variable against
* @return {Boolean}
* @description the purpose of this function is to discover
* if the given variable matches the given type
*/
window.isType = function (obj, type) {
if (typeof type !== "string") { type = type.toString(); }
if (typeof obj === type) { return true; } else { return false; }
};
/**
* @global
* @function
* @name trim
* @return {String}
* @description the purpose of this function is to provide a trim function
*/
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
/**
* @global
* @function
* @name equals
* @param {String} str this is the string that you want to compare
* @return {Boolean}
* @description the purpose of this function is to see if two strings are the
* the same while ignoring the case of the strings
*/
String.prototype.equals = function (str) {
if (typeof str !== "string") { return; }
if (str.toLowerCase() === this.toLowerCase()) { return true; }
return false;
};
/**
* @global
* @function
* @name contains
* @param {*} item this is the item that you're
* searching for
* @return {Boolean}
* @description the purpose of this function is to test if
* the given array contains the given item
*/
Array.prototype.contains = function (item) {
if (this.indexOf(item) > -1) { return true; } else { return false; }
};
/**
* @global
* @function
* @name remove
* @param {*} item this is the item that you want to
* remove
* @return {Array}
* @description the purpose of this function is to remove
* one element from the array
*/
Array.prototype.remove = function (item) {
if(!this.contains(item)) { return this; }
return this.splice(this.indexOf(item), 1);
};
/**
* @global
* @function
* @name empty
* @return {Array}
* @description the purpose of this function is to empty an array
*/
Array.prototype.empty = function() {
for (var i = 0, s = this.length; i < s; i++) { this.pop(); }
return this;
};
/**
* @global
* @function
* @name removeAll
* @param {*} item this is the item that you want to
* completely remove from the given array
* @return {Array}
* @description the purpose of this function is to remove all
* instances of a given element from the given array
*/
Array.prototype.removeAll = function(item) {
var result = [];
for (var i = 0, j = 0, s = this.length; i < s; i++) {
if (this[i] != item) { result[j++] = this[i]; }
}
this.empty();
for (var i = 0, s = result.length; i < s;) { this.push(result[i++]); }
};
/**
* @global
* @function
* @name parent
* @return {Object}
* @description the purpose of this function is to find the given
* html elements parent
*/
Element.prototype.parent = function () {
return this.parentNode;
};
/**
* @global
* @function
* @name children
* @return {HTMLCollection}
* @description the purpose of this function is to find all of the
* children of the given html element
*/
Element.prototype.children = function () {
return this.childNodes;
};
/**
* @global
* @function
* @name addClass
* @param {String} className this is the class name that you want to
* give to the current html element
* @description the purpose of this function is to add a class
* to the given html element
*/
Element.prototype.addClass = function (className) {
try { this.classList.add(className); }
catch (e) { this.className += " " + className; }
};
/**
* @global
* @function
* @name removeClass
* @param {String} className this is the class that you want to remove
* @description the purpose of this function is to remove a class
* from a given html element
*/
Element.prototype.removeClass = function (className) {
try { this.classList.remove(className); }
catch (e) { this.className.replace(className, ""); }
};
/**
* @global
* @function
* @name hasClass
* @param {String} className this is the class that you want to look for
* @return {Boolean}
* @description the purpose of this function is to discover if the given
* html element has the given class
*/
Element.prototype.hasClass = function (className) {
try { return this.classList.contains(className); }
catch (e) { return this.className.indexOf(className) > -1; }
};
/**
* @global
* @function
* @name toggleClass
* @param {String} className this is the class that you want to
* toggle
* @description the purpose of this function is to simply switch
* the given element class, if it has it, then remove,
* if not, then add it to the given element
*/
Element.prototype.toggleClass = function (className) {
try { this.classList.toggle(className); }
catch (e) {
if (this.hasClass(className)) { this.addClass(className); }
else { this.removeClass(className); }
}
};
/**
* @global
* @function
* @name byClass
* @param {String} className this is the class that you're
* searching for
* @return {HTMLCollection}
* @description the purpose of this function is to find all
* elements that can be found within the given html
* element
*/
Element.prototype.byClass = function (className) {
return this.getElementsByClassName(calssName);
};
/**
* @global
* @function
* @name find
* @param {String} query this is the query string
* @return {HTMLCollection}
* @description the purpose of this function is to find
* certain html elements that match a query string that
* can be found within the given html element
*/
Element.prototype.find = function (query) {
return this.querySelectorAll(query);
};
/**
* @global
* @function
* @name
* @return
* @description the purpose of this function is to get the version of IE
* @since 0.0.2
*/
window.getIEVersion = function () {
if (!isIE()) { return undefined; }
var match = navigator.userAgent.match(/(?:MSIE |Trident\/.*; rv:)(\d+)/);
return match ? parseInt(match[1]) : undefined;
};
/**
* @global
* @function
* @name getBrowser
* @return {}
* @description the purpose of this function is to get the browser that the
* current user is utilizing
* @since 0.0.2
*/
window.getBrowser = function () {
var ua = navigator.userAgent;
var tem;
var regEx = new RegExp("/(opera|chrome|safari|firefox|msie|"
+ "trident(?=\/))\/?\s*(\d+)/i");
var M = ua.match(regEx) || [];
if (/trident/i.test(M[1])) {
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return 'IE';
} else if (M[1] === 'Chrome') {
tem = ua.match(/\bOPR\/(\d+)/);
if (tem != null) { return 'Opera'; }
}
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
if ((tem = ua.match(/version\/(\d+)/i)) != null) { M.splice(1, 1, tem[1]); }
return M[0];
};
/**
* @global
* @function
* @name getOS
* @return {Object}
* @description the purpose of this function is to get the OS of the current
* device
* @since 0.0.2
*/
window.getOS = function (testAgent) {
var userAgent;
var windowsReg = new RegExp("/win64|win32|win16|win95|win98|windows 2000|"
+ "windows xp|msie|windows nt 6.3; trident|windows nt|windows/i");
if (!testAgent){
userAgent = navigator.userAgent || navigator.vendor || window.opera;
} else { userAgent = testAgent; }
userAgent = userAgent.toLowerCase();
if (/windows phone/i.test(userAgent)) {
return { os:"windows phone", userAgent:userAgent };
} else if (/samsungbrowser/i.test(userAgent)) {
return { os:"android", userAgent:userAgent };
} else if (/android/i.test(userAgent)) {
return { os:"android", userAgent:userAgent };
} else if (/ipad|iphone|ipod/i.test(userAgent)) {
return { os:"ios", userAgent:userAgent };
} else if (windowsReg.test(userAgent)) {
return { os:"windows", userAgent:userAgent };
} else if (/os x/i.test(userAgent)) {
return { os:"osx", userAgent:userAgent };
} else if (/macintosh|osx/i.test(userAgent)) {
return { os:"osx", userAgent:userAgent };
} else if (/openbsd/i.test(userAgent)) {
return { os:"open bsd", userAgent:userAgent };
} else if (/sunos/i.test(userAgent)) {
return { os:"sunos", userAgent:userAgent };
} else if (/crkey/i.test(userAgent)) {
return { os:"chromecast", userAgent:userAgent };
} else if (/appletv/i.test(userAgent)) {
return { os:"apple tv", userAgent:userAgent };
} else if (/wiiu/i.test(userAgent)) {
return { os:"nintendo wiiu", userAgent:userAgent };
} else if (/nintendo 3ds/i.test(userAgent)) {
return { os:"nintendo 3ds", userAgent:userAgent };
} else if (/playstation/i.test(userAgent)) {
return { os:"playstation", userAgent:userAgent };
} else if (/kindle/i.test(userAgent)) {
return { os:"amazon kindle", userAgent:userAgent };
} else if (/ cros /i.test(userAgent)) {
return { os:"chrome os", userAgent:userAgent };
} else if (/ubuntu/i.test(userAgent)) {
return { os:"ubuntu", userAgent:userAgent};
} else if (/googlebot/i.test(userAgent)) {
return { os:"google bot", userAgent:userAgent };
} else if (/bingbot/i.test(userAgent)) {
return { os:"bing bot", userAgent:userAgent };
} else if (/yahoo! slurp/i.test(userAgent)) {
return { os:"yahoo bot", userAgent:userAgent };
} else { return { os: false, userAgent:userAgent }; }
};
/**
* @public
* @function
* @name getDeviceDetails
* @return {Object}
* @description the purpose of this function is to get all of the
* details about the user's device
*/
window.getDeviceDetails = function () {
try { return navigator; }
catch (e) { return log(e); }
};
/**
* @global
* @function
* @name validEmail
* @return {Boolean}
* @description the purpose of this function is to get the OS of the current
* device
* @since 0.0.2
*/
window.validEmail = function (email) {
var re = new RegExp('/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*'
+ ')|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|('
+ '([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/');
return re.test(email);
};
/**
* @global
* @function
* @name getDateTime
* @return {Object}
* @description the purpose of this function is to get the OS of the current
* device
* @since 0.0.2
*/
window.getDateTime = function () {
var date = new Date();
var months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
var days = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
];
var dateTimeObject= {
day: date.getDate(),
month: date.getMonth() + 1,
year: date.getFullYear(),
dayString: days[date.getDate()],
monthString: months[date.getMonth()]
};
return dateTimeObject;
};
/**
* @global
* @class
* @constructor
* @name ToolKit
* @classdesc the purpose of this class is to allow a
* developer to create an object of this class
* then run any of the provided functions that
* they wish to run
*/
function ToolKit () {
this.urlstring = '';
this.debugMode = false;
this.mobile = this.isMobile();
this.loadedImages = 0;
this.toatlImages = 0;
if (!isDefined(ToolKit.singleton)) { ToolKit.singleton = this; }
else { return ToolKit.singleton; }
}
/**
* @public
* @function
* @name inView
* @param {Object} obj the purpose of this parameter is to test
* if it's currently in the users view
* @return {Boolean}
* @description the purpose of this function is to accept some input, then
* test if it's currently in the user's view, even if it's just
* partial, it doesn't have to be dead center
*/
ToolKit.prototype.inView = function (obj) {
var elm = obj.getBoundingClientRect();
return elm.top <= window.innerHeight && elm.bottom >= 0;
};
/**
* @public
* @function
* @name lazyLoad
* @description the purpose of this function is to load images
* dynamically, it's believed that features such as
* this helps improve SEO, which makes sense, if someone
* is using a mobile device, and they have a poor
* 3G connection, then loading all of the data at once
* will dramatically decrease the overall ui performance
*/
ToolKit.prototype.lazyLoad = function () {
var images = byTag("img");
this.toatlImages = images.length;
var recurse = function() {
for (var i = 0, s = images.length; i < s; i++) {
var current = images[i];
var tool = new ToolKit();
// only load an image once it's in view
if (current.hasClass("lazy") && tool.inView(current)) {
var src = current.getAttribute("data-src");
current.src = src;
current.onload = function () {
this.loadedImages ++;
};
}
// if all images have loaded, remove on scroll
if (this.loadedImages >= this.toatlImages) {
log("remove event");
removeEventHandler(document, "scroll", recurse);
}
}
};
addEventHandler(document, "scroll", recurse);
// IE has issues using the above event handler
// so this is the back up solution, purely for IE
if(isIE()) {
ready(recurse);
}
recurse();
};
/**
* @public
* @function
* @name internal
* @return {Boolean}
* @description the purpose of this function is to load features that
* may be undergoing a lot of work, if you have a certain
* url string setup, then this tests to see if the current
* url contains that string, if not, then the content won't
* load
*/
ToolKit.prototype.internal = function () {
if (!isDefined(this.urlstring)) { return false; }
if (window.location.href.indexOf(this.urlstring) == -1) {
return false;
} else { return true; }
};
/**
* @public
* @function
* @name isMobile
* @return {Boolean}
* @description the purpose of this function is to test whether or not the
* current device is a mobile device or not
*/
ToolKit.prototype.isMobile = function () {
var check = false;
var mobstr = new RegExp("/(android|bb\d+|meego).+mobile|avantgo|bada\/|"
+ "blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|"
+ "ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|"
+ "netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|"
+ "pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|"
+ "wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a)|"
+ "|/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|"
+ "abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|"
+ "ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)"
+ "|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|"
+ "cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|"
+ "do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|"
+ "ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|"
+ "go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)"
+ "|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|"
+ "i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|"
+ "jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|"
+ "le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|"
+ "ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|"
+ "bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|"
+ "n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|"
+ "nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|"
+ "phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|"
+ "qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|"
+ "s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|"
+ "mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|"
+ "so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|"
+ "tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|"
+ "up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|"
+ "voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|"
+ "wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i");
(function(a){
if(mobstr.test(a.substr(0,4)))
check = true;
})(navigator.userAgent||navigator.vendor||window.opera);
return check;
};
/**
* @public
* @function
* @name unitTest
* @param {*} input this is the data that
* you input into the
* function
* @param {Function} tempFunc this is the function that
* you would like to test
* @param {*} expected this is the data you
* expect to get back from
* the provided function
* @return {Object}
* @description the purpose of this function is to allow for some
* basic unit testing
*/
ToolKit.prototype.unitTest = function (input, tempFunc, expected) {
if (this.debugMode) { debugger; }
var results = [];
// if both inputs and results are lists execute this
if (isList(input) && isList(expected)) {
for (var i = 0, s = input.length; i < s; i++) {
var tempdata = { title:"", text:"", result:"", in:"", out:"" };
// i+1 because of index 0
tempdata.title = "Test " + (i + 1);
tempdata.in = input[i];
tempdata.out = expected[i];
try {
var trial = tempFunc(input[i]);
if (trial == expected[i]) {
tempdata.text = "Success";
tempdata.result = true;
results.push(tempdata);
}
else {
tempdata.text = "Fail";
tempdata.result = false;
results.push(tempdata);
}
}
// catch in case the function causes an error
// or the input and expected params are a different
// length, etc
catch (e) {
if (this.debugMode) { console.trace(e); }
tempdata.text = "Error";
tempdata.result = false;
tempdata.report = e;
results.push(tempdata);
}
}
return results;
}
else {
if(isList(input)) { input = input[0]; }
if (isList(expected)) { expected = expected[0]; }
var data = {
title:"Stand Alone Test",
text:"",
result:"",
in:input,
out:expected
};
try {
var trial = tempFunc(input);
if (trial == expected) {
data.text = "Success";
data.result = true;
return data;
} else {
data.text = "Fail";
data.result = false;
return data;
}
}
catch (e) {
if (this.debugMode) { console.trace(e); }
data.text = "Error";
data.result = false;
data.report = e;
return data;
}
}
};
/**
* @public
* @function
* @name garbage
* @return {Undefined}
* @todo implement a more elegant solution for some form
* of js garbage collection
* @description the purpose of this function is to simply TRY to support the
* browser with memory management, as you can't formally do this,
* I feel that this is the next best thing.
*/
ToolKit.prototype.garbage = function () { return undefined; };
/**
* @public
* @function
* @name isNum
* @param {Object} obj this van be any input, as the function will just
* state whether or not the provided input is a
* number or not
* @return {Boolean}
* @todo test & debug
* @description the purpose of this function is to decide whether or not the
* provided param is a numeric value or not
* @since 0.0.2
*/
ToolKit.prototype.isNum = function (obj) {
if (!isNaN(obj)) { return true; }
else { return false; }
};
/**
* @public
* @function
* @name storageSize
* @return {Log}
* @description the purpose of this function is simple, it gets the amount of
* KB that's occupied with local storage
* @since 0.0.2
*/
ToolKit.prototype.storageSize = function () {