-
Notifications
You must be signed in to change notification settings - Fork 12
/
String.js
835 lines (755 loc) · 21.9 KB
/
String.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
//
// JavaScript unit
// Add-on for the string and number manipulation
//
// Copyright (c) 2005, 2006, 2007, 2010, 2011 by Ildar Shaimordanov
//
/*
The following code is described in ECMA drafts and
might be implemented in the future of ECMA
*/
if ( ! String.prototype.repeat ) {
/**
* object.x(number)
* object.repeat(number)
* Transform the string object multiplying the string
*
* @param number Amount of repeating
* @return string
* @access public
* @see http://svn.debugger.ru/repos/jslibs/BrowserExtensions/trunk/ext/string.js
* @see http://wiki.ecmascript.org/doku.php?id=harmony:string_extras
*/
String.prototype.repeat = function(n)
{
n = Math.max(n || 0, 0);
return new Array(n + 1).join(this.valueOf());
};
}
if ( ! String.prototype.startsWith ) {
/**
* Returns true if the sequence of characters of searchString converted
* to a String match the corresponding characters of this object
* (converted to a String) starting at position. Otherwise returns false.
*
* @param string
* @param integer
* @return bollean
* @acess public
*/
String.prototype.startsWith = function(searchString, position)
{
position = Math.max(position || 0, 0);
return this.indexOf(searchString) == position;
};
}
if ( ! String.prototype.endsWith ) {
/**
* Returns true if the sequence of characters of searchString converted
* to a String match the corresponding characters of this object
* (converted to a String) starting at endPosition - length(this).
* Otherwise returns false.
*
* @param string
* @param integer
* @return bollean
* @acess public
*/
String.prototype.endsWith = function(searchString, endPosition)
{
endPosition = Math.max(endPosition || 0, 0);
var s = String(searchString);
var pos = this.lastIndexOf(s);
return pos >= 0 && pos == this.length - s.length - endPosition;
};
}
if ( ! String.prototype.contains ) {
/**
* If searchString appears as a substring of the result of converting
* this object to a String, at one or more positions that are greater than
* or equal to position, then return true; otherwise, returns false.
* If position is undefined, 0 is assumed, so as to search all of the String.
*
* @param string
* @param integer
* @return bollean
* @acess public
*/
String.prototype.contains = function(searchString, position)
{
position = Math.max(position || 0, 0);
return this.indexOf(searchString) != -1;
};
}
if ( ! String.prototype.toArray ) {
/**
* Returns an Array object with elements corresponding to
* the characters of this object (converted to a String).
*
* @param void
* @return array
* @acess public
*/
String.prototype.toArray = function()
{
return this.split('');
};
}
if ( ! String.prototype.reverse ) {
/**
* Returns an Array object with elements corresponding to
* the characters of this object (converted to a String) in reverse order.
*
* @param void
* @return string
* @acess public
*/
String.prototype.reverse = function()
{
return this.split('').reverse().join('');
};
}
/*
The following ode is not described in ECMA specs or drafts.
*/
/**
* String.validBrackets(string)
* Checks string to be valid brackets. Valid brackets are:
* quotes - '' "" `' ``
* single - <> {} [] () %% || // \\
* double - miltiline comments
* /** / C/C++ like (without whitespace)
* <??> PHP like
* <%%> ASP like
* (**) Pascal like
*
* @param string Brackets (left and right)
* @return boolean Result of validity of brackets
* @access static
*/
String.validBrackets = function(br)
{
if ( ! br ) {
return false;
}
var quot = "''\"\"`'``";
var sgl = "<>{}[]()%%||//\\\\";
var dbl = "/**/<??><%%>(**)";
return (br.length == 2 && (quot + sgl).indexOf(br) != -1)
|| (br.length == 4 && dbl.indexOf(br) != -1);
};
/**
* object.bracketize(string)
* Transform the string object by setting in frame of valid brackets
*
* @param string Brackets
* @return string Bracketized string
* @access public
*/
String.prototype.brace =
String.prototype.bracketize = function(br)
{
var string = this;
if ( ! String.validBrackets(br) ) {
return string;
}
var midPos = br.length / 2;
return br.substr(0, midPos) + string.toString() + br.substr(midPos);
};
/**
* object.unbracketize(string)
* Transform the string object removing the leading and trailing brackets
* If the parameter is not defined the method will try to remove existing valid brackets
*
* @param string Brackets
* @return string Unbracketized string
* @access public
*/
String.prototype.unbrace =
String.prototype.unbracketize = function(br)
{
var string = this;
if ( ! br ) {
var len = string.length;
for (var i = 2; i >= 1; i--) {
br = string.substring(0, i) + string.substring(len - i);
if ( String.validBrackets(br) ) {
return string.substring(i, len - i);
}
}
}
if ( ! String.validBrackets(br) ) {
return string;
}
var midPos = br.length / 2;
var i = string.indexOf(br.substr(0, midPos));
var j = string.lastIndexOf(br.substr(midPos));
if (i == 0 && j == string.length - midPos) {
string = string.substring(i + midPos, j);
}
return string;
};
/**
* object.radix(number, number, string)
* Transform the number object to string in accordance with a scale of notation
* If it is necessary the numeric string will aligned to right and filled by '0' character, by default
*
* @param number Radix of scale of notation (it have to be greater or equal 2 and below or equal 36)
* @param number Width of numeric string
* @param string Padding chacracter (by default, '0')
* @return string Numeric string
* @access public
*/
Number.prototype.radix = function(r, n, c)
{
return this.toString(r).padding(-n, c);
// return this.toString(r).padding(-Math.abs(n), c);
};
/**
* object.bin(number, string)
* Transform the number object to string of binary presentation
*
* @param number Width of numeric string
* @param string Padding chacracter (by default, '0')
* @return string Numeric string
* @access public
*/
Number.prototype.bin = function(n, c)
{
return this.radix(0x02, n, c);
// return this.radix(0x02, (n) ? n : 16, c);
};
/**
* object.oct(number, string)
* Transform the number object to string of octal presentation
*
* @param number Width of numeric string
* @param string Padding chacracter (by default, '0')
* @return string Numeric string
* @access public
*/
Number.prototype.oct = function(n, c)
{
return this.radix(0x08, n, c);
// return this.radix(0x08, (n) ? n : 6, c);
};
/**
* object.dec(number, string)
* Transform the number object to string of decimal presentation
*
* @param number Width of numeric string
* @param string Padding chacracter (by default, '0')
* @return string Numeric string
* @access public
*/
Number.prototype.dec = function(n, c)
{
return this.radix(0x0A, n, c);
};
/**
* object.hexl(number, string)
* Transform the number object to string of hexadecimal presentation in lower-case of major characters (0-9 and a-f)
*
* @param number Width of numeric string
* @param string Padding chacracter (by default, '0')
* @return string Numeric string
* @access public
*/
Number.prototype.hexl = function(n, c)
{
return this.radix(0x10, n, c);
// return this.radix(0x10, (n) ? n : 4, c);
};
/**
* object.hex(number, string)
* Transform the number object to string of the hexadecimal presentation
* in upper-case of major characters (0-9 and A-F)
*
* @param number Width of numeric string
* @param string Padding chacracter (by default, '0')
* @return string Numeric string
* @access public
*/
Number.prototype.hex = function(n, c)
{
return this.radix(0x10, n, c).toUpperCase();
};
/**
* object.human([digits[, true]])
* Transform the number object to string in human-readable format (e.h., 1k, 234M, 5G)
*
* @example
* var n = 1001;
*
* // will output 1.001K
* var h = n.human(3);
*
* // will output 1001.000
* var H = n.human(3, true);
*
* @param integer Optional. Number of digits after the decimal point. Must be in the range 0-20, inclusive.
* @param boolean Optional. If true then use powers of 1024 not 1000
* @return string Human-readable string
* @access public
*/
Number.prototype.human = function(digits, binary)
{
var n = Math.abs(this);
var p = this;
var s = '';
var divs = arguments.callee.add(binary);
for (var i = divs.length - 1; i >= 0; i--) {
if ( n >= divs[i].d ) {
p /= divs[i].d;
s = divs[i].s;
break;
}
}
return p.toFixed(digits) + s;
};
/**
* Subsidiary method.
* Stores suffixes and divisors to use in Number.prototype.human.
*
* @param boolean
* @param string
* @param divisor
* @return array
* @access static
*/
Number.prototype.human.add = function(binary, suffix, divisor)
{
var name = binary ? 'div2' : 'div10';
var divs = Number.prototype.human[name] = Number.prototype.human[name] || [];
if ( arguments.length < 3 ) {
return divs;
}
divs.push({s: suffix, d: Math.abs(divisor)});
divs.sort(function(a, b)
{
return a.d - b.d;
});
return divs;
};
// Binary prefixes
Number.prototype.human.add(true, 'K', 1 << 10);
Number.prototype.human.add(true, 'M', 1 << 20);
Number.prototype.human.add(true, 'G', 1 << 30);
Number.prototype.human.add(true, 'T', Math.pow(2, 40));
// Decimal prefixes
Number.prototype.human.add(false, 'K', 1e3);
Number.prototype.human.add(false, 'M', 1e6);
Number.prototype.human.add(false, 'G', 1e9);
Number.prototype.human.add(false, 'T', 1e12);
/**
* object.fromHuman([digits[, binary]])
* Transform the human-friendly string to the valid numeriv value
*
* @example
* var n = 1001;
*
* // will output 1.001K
* var h = n.human(3);
*
* // will output 1001
* var m = h.fromHuman(h);
*
* @param boolean Optional. If true then use powers of 1024 not 1000
* @return number
* @access public
*/
Number.fromHuman = function(value, binary)
{
var m = String(value).match(/^([\-\+]?\d+\.?\d*)([A-Z])?$/);
if ( ! m ) {
return Number.NaN;
}
if ( ! m[2] ) {
return +m[1];
}
var divs = Number.prototype.human.add(binary);
for (var i = 0; i < divs.length; i++) {
if ( divs[i].s == m[2] ) {
return m[1] * divs[i].d;
}
}
return Number.NaN;
};
if ( ! String.prototype.trim ) {
/**
* object.trim()
* Transform the string object removing leading and trailing whitespaces
*
* @return string
* @access public
*/
String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
};
}
if ( ! String.prototype.trimLeft ) {
/**
* object.trimLeft()
* Transform the string object removing leading whitespaces
*
* @return string
* @access public
*/
String.prototype.trimLeft = function()
{
return this.replace(/(^\s*)/, "");
};
}
if ( ! String.prototype.trimRight ) {
/**
* object.trimRight()
* Transform the string object removing trailing whitespaces
*
* @return string
* @access public
*/
String.prototype.trimRight = function()
{
return this.replace(/(\s*$)/g, "");
};
}
/**
* object.dup()
* Transform the string object duplicating the string
*
* @return string
* @access public
*/
String.prototype.dup = function()
{
var val = this.valueOf();
return val + val;
};
/**
* object.padding(number, string)
* Transform the string object to string of the actual width filling by the padding character (by default ' ')
* Negative value of width means left padding, and positive value means right one
*
* @param number Width of string
* @param string Padding chacracter (by default, ' ')
* @return string
* @access public
*/
String.prototype.padding = function(n, c)
{
var val = this.valueOf();
if ( Math.abs(n) <= val.length ) {
return val;
}
var m = Math.max((Math.abs(n) - this.length) || 0, 0);
var pad = Array(m + 1).join(String(c || ' ').charAt(0));
// var pad = String(c || ' ').charAt(0).repeat(Math.abs(n) - this.length);
return (n < 0) ? pad + val : val + pad;
// return (n < 0) ? val + pad : pad + val;
};
/**
* object.padLeft(number, string)
* Wrapper for object.padding
* Transform the string object to string of the actual width adding the leading padding character (by default ' ')
*
* @param number Width of string
* @param string Padding chacracter
* @return string
* @access public
*/
String.prototype.padLeft = function(n, c)
{
return this.padding(-Math.abs(n), c);
};
/**
* object.alignRight(number, string)
* Wrapper for object.padding
* Synonym for object.padLeft
*
* @param number Width of string
* @param string Padding chacracter
* @return string
* @access public
*/
String.prototype.alignRight = String.prototype.padLeft;
/**
* object.padRight(number, string)
* Wrapper for object.padding
* Transform the string object to string of the actual width adding the trailing padding character (by default ' ')
*
* @param number Width of string
* @param string Padding chacracter
* @return string
* @access public
*/
String.prototype.padRight = function(n, c)
{
return this.padding(Math.abs(n), c);
};
/**
* Formats arguments accordingly the formatting string.
* Each occurence of the "{\d+}" substring refers to
* the appropriate argument.
*
* @example
* '{0}is not {1} + {2}'.format('JavaScript', 'Java', 'Script');
*
* @param mixed
* @return string
* @access public
*/
String.prototype.format = function()
{
var args = arguments;
return this.replace(/\{(\d+)\}/g, function($0, $1)
{
return args[$1] !== void 0 ? args[$1] : $0;
});
};
/**
* object.alignLeft(number, string)
* Wrapper for object.padding
* Synonym for object.padRight
*
* @param number Width of string
* @param string Padding chacracter
* @return string
* @access public
*/
String.prototype.alignLeft = String.prototype.padRight;
/**
* sprintf(format, argument_list)
*
* The string function like one in C/C++, PHP, Perl
* Each conversion specification is defined as below:
*
* %[index][alignment][padding][width][precision]type
*
* index An optional index specifier that changes the order of the
* arguments in the list to be displayed.
* alignment An optional alignment specifier that says if the result should be
* left-justified or right-justified. The default is
* right-justified; a "-" character here will make it left-justified.
* padding An optional padding specifier that says what character will be
* used for padding the results to the right string size. This may
* be a space character or a "0" (zero character). The default is to
* pad with spaces. An alternate padding character can be specified
* by prefixing it with a single quote ('). See the examples below.
* width An optional number, a width specifier that says how many
* characters (minimum) this conversion should result in.
* precision An optional precision specifier that says how many decimal digits
* should be displayed for floating-point numbers. This option has
* no effect for other types than float.
* type A type specifier that says what type the argument data should be
* treated as. Possible types:
*
* % - a literal percent character. No argument is required.
* b - the argument is treated as an integer, and presented as a binary number.
* c - the argument is treated as an integer, and presented as the character
* with that ASCII value.
* d - the argument is treated as an integer, and presented as a decimal number.
* u - the same as "d".
* f - the argument is treated as a float, and presented as a floating-point.
* o - the argument is treated as an integer, and presented as an octal number.
* s - the argument is treated as and presented as a string.
* x - the argument is treated as an integer and presented as a hexadecimal
* number (with lowercase letters).
* X - the argument is treated as an integer and presented as a hexadecimal
* number (with uppercase letters).
* h - the argument is treated as an integer and presented in human-readable format
* using powers of 1024.
* H - the argument is treated as an integer and presented in human-readable format
* using powers of 1000.
*/
String.prototype.sprintf = function()
{
var args = arguments;
var index = 0;
var x;
var ins;
var fn;
/*
* The callback function accepts the following properties
* x.index contains the substring position found at the origin string
* x[0] contains the found substring
* x[1] contains the index specifier (as \d+\$ or \d+#)
* x[2] contains the alignment specifier ("+" or "-" or empty)
* x[3] contains the padding specifier (space char, "0" or defined as '.)
* x[4] contains the width specifier (as \d*)
* x[5] contains the floating-point precision specifier (as \.\d*)
* x[6] contains the type specifier (as [bcdfosuxX])
*/
return this.replace(String.prototype.sprintf.re, function()
{
if ( arguments[0] == "%%" ) {
return "%";
}
x = [];
for (var i = 0; i < arguments.length; i++) {
x[i] = arguments[i] || '';
}
x[3] = x[3].slice(-1) || ' ';
ins = args[+x[1] ? x[1] - 1 : index++];
// index++;
return String.prototype.sprintf[x[6]](ins, x);
});
};
String.prototype.sprintf.re = /%%|%(?:(\d+)[\$#])?([+-])?('.|0| )?(\d*)(?:\.(\d+))?([bcdfosuxXhH])/g;
String.prototype.sprintf.b = function(ins, x)
{
return Number(ins).bin(x[2] + x[4], x[3]);
};
String.prototype.sprintf.c = function(ins, x)
{
return String.fromCharCode(ins).padding(x[2] + x[4], x[3]);
};
String.prototype.sprintf.d =
String.prototype.sprintf.u = function(ins, x)
{
return Number(ins).dec(x[2] + x[4], x[3]);
};
String.prototype.sprintf.f = function(ins, x)
{
var ins = Number(ins);
// var fn = String.prototype.padding;
if (x[5]) {
ins = ins.toFixed(x[5]);
} else if (x[4]) {
ins = ins.toExponential(x[4]);
} else {
ins = ins.toExponential();
}
// Invert sign because this is not number but string
x[2] = x[2] == "-" ? "+" : "-";
return ins.padding(x[2] + x[4], x[3]);
// return fn.call(ins, x[2] + x[4], x[3]);
};
String.prototype.sprintf.o = function(ins, x)
{
return Number(ins).oct(x[2] + x[4], x[3]);
};
String.prototype.sprintf.s = function(ins, x)
{
return String(ins).padding(x[2] + x[4], x[3]);
};
String.prototype.sprintf.x = function(ins, x)
{
return Number(ins).hexl(x[2] + x[4], x[3]);
};
String.prototype.sprintf.X = function(ins, x)
{
return Number(ins).hex(x[2] + x[4], x[3]);
};
String.prototype.sprintf.h = function(ins, x)
{
var ins = String.prototype.replace.call(ins, /,/g, '');
// Invert sign because this is not number but string
x[2] = x[2] == "-" ? "+" : "-";
return Number(ins).human(x[5], true).padding(x[2] + x[4], x[3]);
};
String.prototype.sprintf.H = function(ins, x)
{
var ins = String.prototype.replace.call(ins, /,/g, '');
// Invert sign because this is not number but string
x[2] = x[2] == "-" ? "+" : "-";
return Number(ins).human(x[5], false).padding(x[2] + x[4], x[3]);
};
/**
* compile()
*
* This string function compiles the formatting string to the internal function
* to acelerate an execution a formatting within loops.
*
* @example
* // Standard usage of the sprintf method
* var s = '';
* for (var p in obj) {
* s += '%s = %s'.sprintf(p, obj[p]);
* }
*
* // The more speed usage of the sprintf method
* var sprintf = '%s = %s'.compile();
* var s = '';
* for (var p in obj) {
* s += sprintf(p, obj[p]);
* }
*
* @see String.prototype.sprintf()
*/
String.prototype.compile = function()
{
var args = arguments;
var index = 0;
var x;
var ins;
var fn;
/*
* The callback function accepts the following properties
* x.index contains the substring position found at the origin string
* x[0] contains the found substring
* x[1] contains the index specifier (as \d+\$ or \d+#)
* x[2] contains the alignment specifier ("+" or "-" or empty)
* x[3] contains the padding specifier (space char, "0" or defined as '.)
* x[4] contains the width specifier (as \d*)
* x[5] contains the floating-point precision specifier (as \.\d*)
* x[6] contains the type specifier (as [bcdfosuxX])
*/
var result = this.replace(/(\\|")/g, '\\$1').replace(String.prototype.sprintf.re, function()
{
if ( arguments[0] == "%%" ) {
return "%";
}
arguments.length = 7;
x = [];
for (var i = 0; i < arguments.length; i++) {
x[i] = arguments[i] || '';
}
x[3] = x[3].slice(-1) || ' ';
ins = x[1] ? x[1] - 1 : index++;
// index++;
return '", String.prototype.sprintf.' + x[6] + '(arguments[' + ins + '], ["' + x.join('", "') + '"]), "';
});
return Function('', 'return ["' + result + '"].join("")');
};
/**
* Considers the string object as URL and returns it's parts separately
*
* @param void
* @return Object
* @access public
*/
String.prototype.parseUrl = function()
{
var matches = this.match(arguments.callee.re);
if ( ! matches ) {
return null;
}
var result = {
'scheme': matches[1] || '',
'subscheme': matches[2] || '',
'user': matches[3] || '',
'pass': matches[4] || '',
'host': matches[5],
'port': matches[6] || '',
'path': matches[7] || '',
'query': matches[8] || '',
'fragment': matches[9] || ''};
return result;
};
String.prototype.parseUrl.re = /^(?:([a-z]+):(?:([a-z]*):)?\/\/)?(?:([^:@]*)(?::([^:@]*))?@)?((?:[a-z0-9_-]+\.)+[a-z]{2,}|localhost|(?:(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.){3}(?:(?:[01]?\d\d?|2[0-4]\d|25[0-5])))(?::(\d+))?(?:([^:\?\#]+))?(?:\?([^\#]+))?(?:\#([^\s]+))?$/i;
String.prototype.camelize = function()
{
return this.replace(/([^-]+)|(?:-(.)([^-]+))/mg, function($0, $1, $2, $3)
{
return ($2 || '').toUpperCase() + ($3 || $1).toLowerCase();
});
};
String.prototype.uncamelize = function()
{
return this
.replace(/[A-Z]/g, function($0)
{
return '-' + $0.toLowerCase();
});
};