-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.sake.js
2724 lines (2330 loc) · 81.1 KB
/
jquery.sake.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
/**
* KUtils
* @version 2.0.0
* @author Kevin Lucich
*/
(function(window){
var KUtils = null,
KUtilsHelps = null,
check_patterns = {
'text': {
'all': "^(.)__OF_B__$", // __RANGE__
'alphanumeric': "^[a-zA-Z0-9 ]+$",
'no_space': "^[\\S]+$",
'codicefiscale': "^[a-z]{6}[0-9]{2}[a-z][0-9]{2}[a-z][0-9]{3}[a-z]$",
'piva': "^[0-9]{11}$"
},
'number': {
'all': "^(([-+]?[0-9]+)|([-+]?([0-9]__OF_B__\\.[0-9]__OF_A__)))$",
'int': "^[-+]?[0-9]+$",
'float': "^[-+]?([0-9]__OF_B__\\.[0-9]__OF_A__)$"
},
'ip':{
'all': "^([1][0-9][0-9]|2[0-4][0-9]|25[0-5])\.([1][0-9][0-9]|2[0-4][0-9]|25[0-5])\.([1][0-9][0-9]|2[0-4][0-9]|25[0-5])\.([1][0-9][0-9]|2[0-4][0-9]|25[0-5])$"
},
'email':{
'all': "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[A-Za-z]{2,6}$"
},
'username':{
'all': "^([a-zA-Z0-9_-]__OF_B__)$"
},
'password':{
'all': "^([a-zA-Z0-9_-]__OF_B__)$"
},
'phone':{
'all': "^([0-9-()+ ]__OF_B__)$"
},
'date':{
'all': "^(((0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.]([0-9]{4}))|((0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.]([0-9]{4}))|(([0-9]{4})[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))|(([0-9]{4})[- /.](0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])))$",
'Y-m-d': "^(([0-9]{4})[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))$",
'Y-d-m': "^(([0-9]{4})[- /.](0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012]))$",
'm-d-Y': "^((0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.]([0-9]{4}))$",
'd-m-Y': "^((0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.]([0-9]{4}))$"
},
'space':{
'all': "[ \t\r\n]",
'onlyspaces': " ",
'onlytabs': "\t",
'onlybreakline': "[\n\r]"
},
'url':{
'all': "^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$",
'onlywww': "^w{3}([0-9]+)?\.([a-zA-Z0-9]([a-zA-Z0-9\-]{0,65}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}"
},
'creditcard':{
'all': "^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$",
'visa': "^4[0-9]{12}(?:[0-9]{3})?$",
'mastercard': "^5[1-5][0-9]{14}$",
'americaexpress': "^3[47][0-9]{13}$",
'dinersclub': "^3(?:0[0-5]|[68][0-9])[0-9]{11}$",
'discover': "^6(?:011|5[0-9]{2})[0-9]{12}$",
'jcb': "^(?:2131|1800|35\d{3})\d{11}$",
'expdate': "^(0[1-9]|1[012])\/([12][0-9])$"
},
'image':{
'all': "([^\s]+(?=\.(jpeg|jpg|gif|png|tiff))\.\2)$",
'jpeg': "([^\s]+(?=\.(jpeg))\.\2)$",
'jpg': "([^\s]+(?=\.(jpg))\.\2)$",
'gif': "([^\s]+(?=\.(gif))\.\2)$",
'png': "([^\s]+(?=\.(png))\.\2)$",
'tiff': "([^\s]+(?=\.(tiff))\.\2)$"
},
'color':{
'all': "^(rgb\((\d+),\s*(\d+),\s*(\d+)\))|(#?([a-f0-9]{6}|[a-f0-9]{3}))$",
'rgb': "^rgb\((\d+),\s*(\d+),\s*(\d+)\)$",
'hex': "^#?([a-fA-Z0-9]{6}|[a-fA-Z0-9]{3})$"
},
'html':{
'all': "(\<(/?[^\>]+)\>)"
}
};
/*******************************************************************/
/*******************************************************************/
/*******************************************************************/
/*******************************************************************/
// Functions used into KUtils :)
KUtilsHelps = {
'isWindow': function a( obj ){
return obj != null && obj === obj.window;
},
'isPlainObject': function( obj ){
// Not plain objects:
// - Any object or value whose internal [[Class]] property is not "[object Object]"
// - DOM nodes
// - window
if ( typeof obj !== 'object' || obj.nodeType || KUtilsHelps.isWindow(obj) ) {
return false;
}
// Support: Firefox <20
// The try/catch suppresses exceptions thrown when attempting to access
// the "constructor" property of certain host objects, ie. |window.location|
// https://bugzilla.mozilla.org/show_bug.cgi?id=814622
try {
if( obj.constructor && !core_hasOwn.call(obj.constructor.prototype,'isPrototypeOf') ){
return false;
}
}catch( e ){
return false;
}
// If the function hasn't returned already, we're confident that
// |obj| is a plain object, created by {} or constructed with new Object
return true;
}
};
/*******************************************************************/
/*******************************************************************/
/*******************************************************************/
/*******************************************************************/
KUtils = {
'plugins_version': {
'KUtils': '2.0.0'
},
/**
* KUtils.version
* Ritorna una mappa con le versioni delle plugin
*/
'version': function( plugins ){
if( typeof plugins !== 'undefined' ){
for( p in plugins ){
KUtils.plugins_version[ p ] = plugins[p];
}
return;
}
console.dir( KUtils.plugins_version );
},
/**
$.check( params|typeCheck, value, [, subtype] );
$( __ELEMENT__ ).check( params|typeCheck, [, subtype] ); Check the value of __ELEMENT__ with the "typeCheck"
@version 1.2
@author Kevin Lucich
@param params {Object|string} The type of will be the value or List of params
@param _subtype {string} A specified type (default: 'all') // it meas "all" of typeCheck
@param _value {string|Number} The value to check
@return {Boolean} Return True if the value is valid
*/
'check': function( params, _value, _subtype ){
var methods = {
'check': {
'init': function( params, _value, _subtype ){
// Posso passare i parametri come oggetto o tre valori separati (type,value,subtype)
if( (typeof params !== 'undefined') && (typeof params != 'object') ){
params = {
'type': params,
'subtype': (typeof _subtype === 'undefined') ? 'all' : _subtype,
'value': _value
};
}
var paramsDefault = {
'type': 'text',
'subtype': 'all',
'value': false,
'rules': {
'range': '', // {'from': 0, 'to': 1},
'of': '+',
'replace': false
}
};
return KUtils.extend( true, {}, paramsDefault, params );
},
'_do': function(params){
var check_param_of_range = function(p,nameParam){
if( !(/^([*]{1})$|^([0-9]+)$|^([0-9]+,[0-9]+)$/).test(p) ){
console_action('PRM_IGN','$.check',nameParam);
return false;
}
return true;
};
var _regex = check_patterns[ params.type ][ params.subtype ];
var __OF_A__ = '+';
var __OF_B__ = params.rules.of;
if( params.rules.range != '' ){
__OF_B__ = params.rules.range;
if( params.subtype == 'float' ){ // Formato: 1,2;8,9 => range
if( /^[0-9]+,[0-9]+;[0-9]+,[0-9]+$/.test( params.rules.range ) ){
var r_split = (params.rules.range).split(';');
__OF_A__ = (check_param_of_range(r_split[0],'params.rules.range')) ? r_split[0] : '';
__OF_B__ = (check_param_of_range(r_split[1],'params.rules.range')) ? r_split[1] : '+';
}
}else{
// Formato: [num] => viene trasformato in 0,[num] così da diventare un range
if( /^[0-9]+$/.test( __OF_B__ ) ){
__OF_B__ = '0,'+ __OF_B__;
}else{
// Formato: 1,9 => range
__OF_B__ = (check_param_of_range( __OF_B__,'params.rules.range' )) ? __OF_B__ : '+';
}
}
} // End PRR
if( __OF_B__ != '+' && !check_param_of_range( __OF_B__, 'params.rules.of' ) ){
__OF_B__ = '+';
}
if( (__OF_A__ != '+')&&(__OF_A__ != '*') )
__OF_A__ = '{'+ __OF_A__ +'}';
if( (__OF_B__ != '+')&&(__OF_B__ != '*') )
__OF_B__ = '{'+ __OF_B__ +'}';
_regex = _regex.replace(/__OF_A__/, __OF_A__ ).replace(/__OF_B__/, __OF_B__ );
var regex = new RegExp( _regex );
return regex.test( params.value );
}
}
};
params = (methods.check['init']).apply( undefined, arguments );
return (methods.check['_do']).apply( undefined, [params] );
},
'stripslashes': function( str ){
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Ates Goral (http://magnetiq.com)
// + fixed by: Mick@el
// + improved by: marrtins
// + bugfixed by: Onno Marsman
// + improved by: rezna
// + input by: Rick Waldron
// + reimplemented by: Brett Zamir (http://brett-zamir.me)
// + input by: Brant Messenger (http://www.brantmessenger.com/)
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// * example 1: stripslashes('Kevin\'s code');
// * returns 1: "Kevin's code"
// * example 2: stripslashes('Kevin\\\'s code');
// * returns 2: "Kevin\'s code"
return (str + '').replace(/\\(.?)/g, function (s, n1) {
switch (n1) {
case '\\':
return '\\';
case '0':
return '\u0000';
case '':
return '';
default:
return n1;
}
});
},
/**
* Return length of Object
* @author Kevin Lucich <lucichkevin@gmail.com>
* @param obj {Object} The Object to count
* @return {int} The lenght of Object
*/
'objSize': function( obj ){
if( obj == null || (obj.constructor != Object && obj.constructor != Array) ){
return 0;
}
return Object.keys(obj).length;
},
/**
* Return a copy of Object
* @author Kevin Lucich <lucichkevin@gmail.com>
* @param obj {Object} The Object to count
* @return {Object} A copy of Object
*/
'objClone': function( obj ) {
if( null == obj || obj.constructor != Object ){
return obj;
}
var copy = obj.constructor();
for( var attr in obj ){
if( attr in obj ){
copy[attr] = obj[attr];
}
}
return copy;
},
'objJoin': function( obj, glue, separator ){
obj = (typeof obj !== 'undefined') ? obj : {};
glue = (typeof glue !== 'undefined') ? glue : '=';
separator = (typeof separator !== 'undefined') ? separator : ',';
var pieces = [];
for( var i in obj ){
if ( obj.hasOwnProperty( i ) ){
pieces.push( i + glue + obj[i] );
}
}
return pieces.join( separator );
},
'obj2Array': function( obj ){
var array = [];
for( var k in obj ){
array.push( obj[k] );
}
return array;
},
'objKeys': function( obj ){
var keys = [];
if( (typeof obj === 'undefined') || (obj == null) ){
return keys;
}
switch( obj.constructor ){
case Object:
// If exists "Object.keys" (native method) use it, otherwise use case "Array" !
if( typeof Object.keys !== 'undefined' ){
return Object.keys(obj);
}
case Array:
for( var i in obj ){
if( obj.hasOwnProperty(i) )
keys.push(i);
}
return keys;
default:
return keys;
}
},
'objFlip': function( obj ){
var key=null, tmp = {};
for( key in obj ){
if ( obj.hasOwnProperty( key ) ){
tmp[ obj[key] ] = key;
}
}
return tmp;
},
'objIntersectKey': function(){
var argv = [].slice.call(arguments);
var result = argv[0];
var len = argv.length;
for( var i=1; i<len; i++ ){
var array = argv[i];
for( var key in result ){
if( typeof array[key] === 'undefined' ){
delete( result[key] );
}
}
}
return result;
},
'objKeyAllowed': function( obj, allowed ){
obj = obj || {};
allowed = allowed || [];
return KUtils.objIntersectKey( obj, KUtils.objFlip(allowed) );
},
'trim': function( str ){
if( str == null || typeof str === 'undefined' || str.constructor !== String ){
return str;
}
if( typeof String.prototype.trim !== 'undefined' ){
return str.trim();
}
if( typeof String.trim !== 'undefined' ){
return String.trim(str);
}
return str.replace(/^\s+|\s+$/g,''); // Colpa di IE :'(
},
/**
* Return boolean if the el is in array
* @param needle mixed The searched value.
* @param haystack array The array
* @return {Boolean}
*/
'inArray': function( needle, haystack ){
if( typeof needle === 'undefined' || haystack.constructor !== Array ){
return false;
}
var i = 0;
var len = null;
if( needle.constructor === Array ){
len = needle.length;
for( i=0; i<len; i++ ){
if( !KUtils.inArray( needle[i], haystack ) ){
return false;
}
}
return true;
}
if( typeof [].indexOf !== 'undefined' ){
return (haystack.indexOf(needle)!=-1);
}else{
len = haystack.length;
for( i=0; i<len; i++ ){
if( haystack[i] == needle ){
return true;
}
}
return false;
}
},
/**
* $.MD5( str ); Return string cripted with MD5 method
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* @author Paul Johnston, Greg Holt
* @updated Kevin Lucich <lucichkevin@gmail.com>
* @param str {String|Object} The string (or object/array) to cript with MD5 method
* @return {string} String cripted with MD5 method
*/
'MD5': function( str ){
// In this case "str" is an Object or Array
if( (str != null) && (str.constructor == Array || str.constructor == Object) ){
var __describe_object = function( obj, str ){
if( !obj ){
return str;
}
for( i in obj ){
if( obj.hasOwnProperty(i) == false ){
continue;
}
var md5 = (obj[i] != null) && ((obj[i]).constructor == Array || (obj[i]).constructor == Object) ? __describe_object(obj[i],str) : i +':'+ obj[i];
str = KUtils.MD5( str +'_'+ md5 );
}
return str;
};
str = __describe_object(str,'');
}
// Convert a 32-bit number to a hex string with ls-byte first
var hex_chr = "0123456789abcdef";
function rhex(num){
str = "";
for(var j=0; j <= 3; j++)
str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) + hex_chr.charAt((num >> (j * 8)) & 0x0F);
return str;
}
// Convert a string to a sequence of 16-word blocks, stored as an array. Append padding bits and the length, as described in the MD5 standard.
function str2blks_MD5(str){
var nblk = ((str.length + 8) >> 6) + 1;
var blks = new Array(nblk * 16);
for(i=0; i < nblk * 16; i++) blks[i] = 0;
for(i=0; i < str.length; i++)
blks[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8);
blks[i >> 2] |= 0x80 << ((i % 4) * 8);
blks[nblk * 16 - 2] = str.length * 8;
return blks;
}
// Add integers, wrapping at 2^32. This uses 16-bit operations internally to work around bugs in some JS interpreters.
function add(x, y){
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
// Bitwise rotate a 32-bit number to the left
function rol(num, cnt){ return (num << cnt) | (num >>> (32 - cnt)); }
// These functions implement the basic operation for each round of the algorithm.
function cmn(q, a, b, x, s, t){ return add(rol(add(add(a, q), add(x, t)), s), b); }
function ff(a, b, c, d, x, s, t){ return cmn((b & c) | ((~b) & d), a, b, x, s, t); }
function gg(a, b, c, d, x, s, t){ return cmn((b & d) | (c & (~d)), a, b, x, s, t); }
function hh(a, b, c, d, x, s, t){ return cmn(b ^ c ^ d, a, b, x, s, t); }
function ii(a, b, c, d, x, s, t){ return cmn(c ^ (b | (~d)), a, b, x, s, t); }
// Take a string and return the hex representation of its MD5.
var x = str2blks_MD5(str);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var xlen = x.length;
for(var i=0; i < xlen; i += 16){
olda = a;
oldb = b;
oldc = c;
oldd = d;
a = ff(a, b, c, d, x[i], 7 , -680876936);
d = ff(d, a, b, c, x[i+ 1], 12, -389564586);
c = ff(c, d, a, b, x[i+ 2], 17, 606105819);
b = ff(b, c, d, a, x[i+ 3], 22, -1044525330);
a = ff(a, b, c, d, x[i+ 4], 7 , -176418897);
d = ff(d, a, b, c, x[i+ 5], 12, 1200080426);
c = ff(c, d, a, b, x[i+ 6], 17, -1473231341);
b = ff(b, c, d, a, x[i+ 7], 22, -45705983);
a = ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
d = ff(d, a, b, c, x[i+ 9], 12, -1958414417);
c = ff(c, d, a, b, x[i+10], 17, -42063);
b = ff(b, c, d, a, x[i+11], 22, -1990404162);
a = ff(a, b, c, d, x[i+12], 7 , 1804603682);
d = ff(d, a, b, c, x[i+13], 12, -40341101);
c = ff(c, d, a, b, x[i+14], 17, -1502002290);
b = ff(b, c, d, a, x[i+15], 22, 1236535329);
a = gg(a, b, c, d, x[i+ 1], 5 , -165796510);
d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
c = gg(c, d, a, b, x[i+11], 14, 643717713);
b = gg(b, c, d, a, x[i], 20, -373897302);
a = gg(a, b, c, d, x[i+ 5], 5 , -701558691);
d = gg(d, a, b, c, x[i+10], 9 , 38016083);
c = gg(c, d, a, b, x[i+15], 14, -660478335);
b = gg(b, c, d, a, x[i+ 4], 20, -405537848);
a = gg(a, b, c, d, x[i+ 9], 5 , 568446438);
d = gg(d, a, b, c, x[i+14], 9 , -1019803690);
c = gg(c, d, a, b, x[i+ 3], 14, -187363961);
b = gg(b, c, d, a, x[i+ 8], 20, 1163531501);
a = gg(a, b, c, d, x[i+13], 5 , -1444681467);
d = gg(d, a, b, c, x[i+ 2], 9 , -51403784);
c = gg(c, d, a, b, x[i+ 7], 14, 1735328473);
b = gg(b, c, d, a, x[i+12], 20, -1926607734);
a = hh(a, b, c, d, x[i+ 5], 4 , -378558);
d = hh(d, a, b, c, x[i+ 8], 11, -2022574463);
c = hh(c, d, a, b, x[i+11], 16, 1839030562);
b = hh(b, c, d, a, x[i+14], 23, -35309556);
a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
d = hh(d, a, b, c, x[i+ 4], 11, 1272893353);
c = hh(c, d, a, b, x[i+ 7], 16, -155497632);
b = hh(b, c, d, a, x[i+10], 23, -1094730640);
a = hh(a, b, c, d, x[i+13], 4 , 681279174);
d = hh(d, a, b, c, x[i], 11, -358537222);
c = hh(c, d, a, b, x[i+ 3], 16, -722521979);
b = hh(b, c, d, a, x[i+ 6], 23, 76029189);
a = hh(a, b, c, d, x[i+ 9], 4 , -640364487);
d = hh(d, a, b, c, x[i+12], 11, -421815835);
c = hh(c, d, a, b, x[i+15], 16, 530742520);
b = hh(b, c, d, a, x[i+ 2], 23, -995338651);
a = ii(a, b, c, d, x[i], 6 , -198630844);
d = ii(d, a, b, c, x[i+ 7], 10, 1126891415);
c = ii(c, d, a, b, x[i+14], 15, -1416354905);
b = ii(b, c, d, a, x[i+ 5], 21, -57434055);
a = ii(a, b, c, d, x[i+12], 6 , 1700485571);
d = ii(d, a, b, c, x[i+ 3], 10, -1894986606);
c = ii(c, d, a, b, x[i+10], 15, -1051523);
b = ii(b, c, d, a, x[i+ 1], 21, -2054922799);
a = ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
d = ii(d, a, b, c, x[i+15], 10, -30611744);
c = ii(c, d, a, b, x[i+ 6], 15, -1560198380);
b = ii(b, c, d, a, x[i+13], 21, 1309151649);
a = ii(a, b, c, d, x[i+ 4], 6 , -145523070);
d = ii(d, a, b, c, x[i+11], 10, -1120210379);
c = ii(c, d, a, b, x[i+ 2], 15, 718787259);
b = ii(b, c, d, a, x[i+ 9], 21, -343485551);
a = add(a, olda);
b = add(b, oldb);
c = add(c, oldc);
d = add(d, oldd);
}
return (rhex(a) + rhex(b) + rhex(c) + rhex(d));
},
/**
* Return hash of string
* KUtils.hash( str );
* @version 1.0
* @author Kevin Lucich
* @param str string The string to conver in hash
* @return Number The hash
*/
'hash': function( str ){
if( !str || str.constructor != String ){
return 0;
}
var hash = 0, i, c,
len = str.length;
if( len == 0 ){
return hash;
}
for( i=0; i < len; i++ ){
c = str.charCodeAt(i);
hash = ((hash<<5)-hash)+c;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
},
/**
* Return if a variable is empty (null, 0, empty string, false)
* @version 1.0
* @param variable {mixed} The variable to check
* @return {Boolean}
*/
'empty': function( variable ){
if( variable == null ){
return true;
}
switch( variable.constructor ){
case Number:
return (isNaN(variable)) || (variable === 0);
case String:
return (variable.length === 0 || variable==='0');
case Array:
case Object:
return (KUtils.objSize(variable) === 0);
case Boolean:
return (variable === false);
default:
return false;
}
},
/**
* .rgbToHex()
* @version 1.0
* @author Kevin Lucich
*
* @desc
* $.rgbToHex( r, g, b );
* Converter a RGB color to HEX
*
* @param {string} r Code for red color
* @param {string} g Code for green color
* @param {string} b Code for blue color
* @return {string} HEX color (es. #00FF00 )
*/
'rgbToHex': function( r, g, b ){
var componentToHex = function (c){
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
};
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
},
/**
* .hexToRgb()
* @version 1.1
* @author Kevin Lucich
*
* @desc
* $.hexToRgb( hex [, return_string] );
* Converter a HEX color to RBG
*
* @param {string} hex The color in hex code
* @param {(Boolean|null)} return_string False will return an obj {r,g,b}, if true return a string
* @return {object|string} if return_string param is True: return "r,g,b", otherwise an object
*/
'hexToRgb': function( hex, return_string ){
if( typeof hex === 'undefined' ){
hex = '#000000';
}
if( typeof return_string === 'undefined' ){
return_string = false;
}
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec( hex );
var rgb = result ? {
'r': parseInt(result[1], 16),
'g': parseInt(result[2], 16),
'b': parseInt(result[3], 16)
} : null;
if( return_string ){
return rgb.r +','+ rgb.g +','+ rgb.b;
}
return rgb;
},
/**
* .extend()
* @version 1.0
* @author jQuery Foundation
*/
'extend': (typeof jQuery !== 'undefined' && jQuery.extend) || function(){
var isPlainObject = function( obj ){
// Not plain objects:
// - Any object or value whose internal [[Class]] property is not "[object Object]"
// - DOM nodes
// - window
if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
return false;
}
if ( obj.constructor && !hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {
return false;
}
// If the function hasn't returned already, we're confident that
// |obj| is a plain object, created by {} or constructed with new Object
return true;
};
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
// Handle a deep copy situation
if ( typeof target === "boolean" ) {
deep = target;
// Skip the boolean and the target
target = arguments[ i ] || {};
i++;
}
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
}
// Extend jQuery itself if only one argument is passed
if ( i === length ) {
target = this;
i--;
}
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
if ( (options = arguments[ i ]) != null ) {
// Extend the base object
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
// Prevent never-ending loop
if ( target === copy ) {
continue;
}
// Recurse if we're merging plain objects or arrays
if ( deep && copy && ( isPlainObject(copy) || (copyIsArray = Array.isArray(copy)) ) ) {
if( copyIsArray ){
copyIsArray = false;
clone = src && Array.isArray(src) ? src : [];
}else{
clone = src && isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
target[ name ] = KUtils.extend( deep, clone, copy );
// Don't bring in undefined values
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
// Return the modified object
return target;
},
/**
* .range()
* @param start {mixed}
* @param end {mixed}
* @param step {number}
* @return array
*/
'range': function( start, end, step ){
var range = [];
var typeofStart = typeof start;
var typeofEnd = typeof end;
if (step === 0) {
throw new TypeError("Step cannot be zero.");
}
if (typeofStart == "undefined" || typeofEnd == "undefined") {
throw new TypeError("Must pass start and end arguments.");
} else if (typeofStart != typeofEnd) {
throw new TypeError("Start and end arguments must be of same type.");
}
typeof step == "undefined" && (step = 1);
if (end < start) {
step = -step;
}
if (typeofStart == "number") {
while (step > 0 ? end >= start : end <= start) {
range.push(start);
start += step;
}
}else if (typeofStart == "string") {
if (start.length != 1 || end.length != 1) {
throw new TypeError("Only strings with one character are supported.");
}
start = start.charCodeAt(0);
end = end.charCodeAt(0);
while (step > 0 ? end >= start : end <= start) {
range.push(String.fromCharCode(start));
start += step;
}
}else{
throw new TypeError("Only string and number types are supported");
}
return range;
},
/**
var test_fns = {
'ucFirst1': function(){
var string = 'aaaaaaaaaaa';
return string.charAt(0).toUpperCase() + string.slice(1);
},
'ucFirst2': function(){
var string = 'aaaaaaaaaaa';
return string.substring(0, 1).toUpperCase() + string.substring(1).toLowerCase();
},
'LOL': function(){
var string = 'aaaaaaaaaaa';
return string;
},
};
console.dir( KUtils.benchmark( test_fns) );
*/
'benchmark': function( millisecond, howmany, fns ){
// function( ) => error
if( (typeof millisecond === 'undefined') ){
console.error('Error: missing fns param');
return;
}
// function( fns )
if( millisecond.constructor == Object ){
fns = millisecond;
millisecond = 1000; // one second
howmany = 1;
}
// function( millisecond, fns )
if( howmany.constructor == Object ){
fns = howmany;
howmany = 1;
}
var results = {
'fns': {},
'statistics': {
'faster': {
'fn': null,
'loop': Infinity
},
'slower': {
'fn': null,
'loop': -Infinity
}
}
};
for( fn_name in fns ){
var fn = fns[ fn_name ];
var array_loops = [];
for(var i=0; i<howmany; i++ ){
var second = (new Date().getTime()) + millisecond;
var current_loop = 0;
while( (new Date().getTime()) < second ){
current_loop++;
fn();
}
array_loops.push( current_loop );
}
// Average
number_of_execution = (function(array){
var avg = 0;
for( a in array ){
avg += array[a];
}
return (avg/(array.length));
})(array_loops);
if( results.statistics.faster.fn == null || results.statistics.faster.number_of_execution < number_of_execution ){
results.statistics.faster = {
'fn': fn_name,
'number_of_execution': number_of_execution
};
}
if( results.statistics.slower.fn == null || results.statistics.slower.number_of_execution > number_of_execution ){
results.statistics.slower = {
'fn': fn_name,
'number_of_execution': number_of_execution
};
}
results['fns'][ fn_name ] = number_of_execution;
}
var tmp = (results.statistics.faster.number_of_execution / results.statistics.slower.number_of_execution);
results.statistics.result = 'The function '+ results.statistics.faster.fn +' is faster than '+ results.statistics.slower.fn +' of '+ ((tmp*100)-100).toFixed(2) +'% ('+ tmp.toFixed(2) +' times)';
return results;
},
'ucFirst': function( string ){
return (!string) ? "" : string[0].toUpperCase() + string.slice(1);
},
'dotdotdot': function( string, how_many_words ){
if( typeof string === 'undefined' ){
return '';
}
if( typeof how_many_words === 'undefined' ){
how_many_words = pieces.length/2;
}
var pieces = string.split(' ');
return (pieces.slice(0, how_many_words ).join(' ')) +'...';
},
'consoleCheck': function(){
if( typeof window.console !== 'undefined' ){
return true;
}
var c = {};
var fns = [
'log','debug','info','warn','exception','assert','dir','dirxml','trace','group','groupEnd',
'groupCollapsed','profile','profileEnd','count','clear','time','timeEnd','timeStamp','table','error'
];
for( f in fns ){
c[ fns[f] ] = function(){};
}
window.console = c;
},
'stack': function( show_anonymous ){
show_anonymous = (typeof show_anonymous !== 'undefined') ? show_anonymous : false;
var stack = [];
var _caller = arguments.callee.caller;
while( _caller != null ){
var fn_name = /function ([^(]*)/.exec( _caller+"" );
fn_name = (fn_name != null && typeof fn_name[1] !== 'undefined') ? fn_name[1] : fn_name;
if( show_anonymous && fn_name == '' )
fn_name = 'anonymous';
if( fn_name != '' )
stack.push( fn_name );
_caller = _caller.caller;
}
stack.reverse();
if( stack.length ){
return stack.join('() -> ') +'()';
}
return null;
},