-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathstring.js
824 lines (693 loc) · 24.1 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
/*jslint
bitwise: false,
browser: true,
newcap: false,
nomen: false,
debug: true,
forin: true,
plusplus: false,
undef: true,
white: false,
onevar: false
*/
var sc;
/**
* determines if a string contains the given screen name prefixed with a @
* this is mainly used for determining if a message should be considered a 'mention'
* @param {string} str the string to check
* @param {string} sn the screen name to look for
* @return {boolean}
* @member sc.helpers
*/
sc.helpers.containsScreenName = function(str, sn) {
var re = new RegExp('(?:\\s|\\b|^[[:alnum:]]|^)@('+sn+')(?:\\s|\\b|$)', 'gi');
if ( re.test(str) ) {
return true;
}
return false;
};
/**
* @param {string} str string to search for usernames
* @@param {array} without array of usernames to skip
*/
sc.helpers.extractScreenNames = function(str, without) {
str = str.toLowerCase(); // normalize to lowercase
// var re_uname = /(^|\s|\(\[|,|\.|\()@([a-zA-Z0-9_]+)([^a-zA-Z0-9_]|$)/gi;
var re_uname = /(?:^|\s|\(\[|,|\.|\()@([a-zA-Z0-9_]+)/gi;
var usernames = [];
var ms = [];
var wo_args = [];
while (ms = re_uname.exec(str))
{
/*
sometimes we can end up with a null instead of a blank string,
so we need to force the issue in javascript.
*/
for (var x=0; x<ms.length; x++) {
if (!ms[x]) {
ms[x] = '';
}
}
if(ms[1] != ''){
usernames.push(ms[1]);
}
}
if (usernames.length > 0) {
usernames = _.uniq(usernames); // make unique
if (sch.isString(usernames)) { // at least in webOS 1.4.5, if you only had one item it qould return as a string, not an array
usernames = [usernames];
}
if (without) { // remove any usernames we want to skip
wo_args = [usernames];
for (var i=0; i < without.length; i++) {
wo_args.push(without[i].toLowerCase());
}
usernames = _.without.apply(this, wo_args);
}
}
return usernames||[];
};
/**
* find URLs within the given string
* @member sc.helpers
*/
sc.helpers.extractURLs = function(str) {
// var wwwlinks = /(^|\s)((https?|ftp)\:\/\/)?([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?([✪a-z0-9-.]*)\.([a-z]{2,3})(\:[0-9]{2,5})?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?(\s|$)/gi;
var wwwlinks = /(^|\s|\(|:)(((http(s?):\/\/)|(www\.))([\w✪]+[^\s\)<]+))/gi;
var ms = [];
var URLs = [];
while ( (ms = wwwlinks.exec(str)) !== null ) {
for (var x=0; x<ms.length; x++) {
if (!ms[x]) {
ms[x] = '';
}
}
var last = ms[7].charAt(ms[7].length - 1);
if (last.search(/[\.,;\?]/) !== -1) { // if ends in common punctuation, strip
ms[7] = ms[7].slice(0,-1);
}
URLs.push(ms[3]+ms[7]);
}
return URLs;
};
/**
* given as string and a mapping object, replace multiple values in the string (or vice versa)
* map should be of format
* {
* 'searchforme':'replacewithme',
* 'searchforme2':'replacewithme2',
* 'searchforme3':'replacewithme3'
* }
* @param {string} str
* @param {object} map
* @return {string}
* @member sc.helpers
*/
sc.helpers.replaceMultiple = function(str, map) {
for (var key in map) {
str = str.replace(key, map[key]);
}
return str;
};
/**
* This is a port of the CodeIgniter helper "autolink" to javascript.
* It finds and links both web addresses and email addresses. It will ignore
* links within HTML (as attributes or between tag pairs)
*
* @param {string} str
* @param {string} type 'email', 'url', or 'both' (default is 'both')
* @param {boolean} extra_code a string that will be inserted verbatim into <a> tag
* @param {integer} maxlen the maximum length the link description can be (the string inside the <a></a> tag)
* @return {string}
* @member sc.helpers
*/
sc.helpers.autolink = function(str, type, extra_code, maxlen) {
if (!type) {
type = 'both';
}
var re_nohttpurl = /((^|\s)(www\.)?([a-zA-Z_\-]+\.)(com|net|org|uk)($|\s))/gi;
var re_noemail = /(^|[\s\(:。])((http(s?):\/\/)|(www\.))([\w✪]+[^\s\)<]+)/gi;
var re_nourl = /(^|\s|\()([a-zA-Z0-9_\.\-\+]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)([^\s\)<]+)/gi;
var x, ms, period = '';
if (type !== 'email')
{
while ((ms = re_nohttpurl.exec(str))) { // look for URLs without a preceding "http://"
/*
sometimes we can end up with a null instead of a blank string,
so we need to force the issue in javascript.
*/
for (x=0; x<ms.length; x++) {
if (!ms[x]) {
ms[x] = '';
}
}
if (extra_code) {
extra_code = ' '+extra_code;
} else {
extra_code = '';
}
var desc = ms[3]+ms[4]+ms[5];
if (maxlen && maxlen > 0 && desc.length > maxlen) {
desc = desc.substr(0, maxlen)+'...';
}
var newstr = ms[2]+'<a href="http://'+ms[3]+ms[4]+ms[5]+'"'+extra_code+'>'+desc+'</a>'+ms[6];
sch.error(newstr);
str = str.replace(ms[0], newstr);
}
while ((ms = re_noemail.exec(str))) {
/*
sometimes we can end up with a null instead of a blank string,
so we need to force the issue in javascript.
*/
for (x=0; x<ms.length; x++) {
if (!ms[x]) {
ms[x] = '';
}
}
if (extra_code) {
extra_code = ' '+extra_code;
} else {
extra_code = '';
}
/*
if the last character is one of . , ; ?, we strip it off and
stick it on the end of newstr below as "period"
*/
var last = ms[6].charAt(ms[6].length - 1);
if (last.search(/[\.,;\?]/) !== -1) {
ms[6] = ms[6].slice(0,-1);
period = last;
}
var desc = ms[5]+ms[6];
if (maxlen && maxlen > 0 && desc.length > maxlen) {
desc = desc.substr(0, maxlen)+'...';
}
var newstr = ms[1]+'<a href="http'+ms[4]+'://'+ms[5]+ms[6]+'"'+extra_code+'>'+desc+'</a>'+period;
str = str.replace(ms[0], newstr);
}
}
if (type !== 'url')
{
while ((ms = re_nourl.exec(str)))
{
period = '';
if ( /\./.test(ms[5]) ) {
period = '.';
ms[5] = ms[5].slice(0, -1);
}
/*
sometimes we can end up with a null instead of a blank string,
so we need to force the issue in javascript.
*/
for (x=0; x<ms.length; x++) {
if (!ms[x]) {
ms[x] = '';
}
}
str = str.replace(ms[0], ms[1]+'<a href="mailto:'+ms[2]+'@'+ms[3]+'.'+ms[4]+ms[5]+'">'+ms[2]+'@'+ms[3]+'.'+ms[4]+ms[5]+'</a>'+period);
}
}
return str;
};
/**
* turns twitter style username refs ('@username') into links
* by default, the template used is <a href="http://twitter.com/#username#">@#username#<a/>
* pass the second param to give it a custom template
*
* @param {string} str
* @param {string} tpl default is '<a href="http://twitter.com/#username#">@#username#</a>'
* @return {string}
* @member sc.helpers
*/
sc.helpers.autolinkTwitterScreenname = function(str, tpl) {
if (!tpl) {
tpl = '<a href="http://twitter.com/#username#">@#username#</a>';
}
var re_uname = /(^|\s|\(\[|,|\.|\()@([a-zA-Z0-9_]+)([^a-zA-Z0-9_]|$)/gi;
var ms = [];
while (ms = re_uname.exec(str))
{
/*
sometimes we can end up with a null instead of a blank string,
so we need to force the issue in javascript.
*/
for (var x=0; x<ms.length; x++) {
if (!ms[x]) {
ms[x] = '';
}
}
var repl_tpl = tpl.replace(/#username#/gi, ms[2]);
str = str.replace(ms[0], ms[1]+repl_tpl+ms[3]);
}
return str;
};
/**
* turns twitter style hashtags ('#hashtag') into links
* by default, the template used is <a href="http://search.twitter.com/search?q=#hashtag_enc#">##hashtag#<a/>
* pass the second param to give it a custom template
*
* @param {string} str
* @param {string} tpl default is '<a href="http://search.twitter.com/search?q=#hashtag_enc#">##hashtag#<a/>'
* @return {string}
* @member sc.helpers
*/
sc.helpers.autolinkTwitterHashtag = function(str, tpl) {
if (!tpl) {
tpl = '<a href="http://search.twitter.com/search?q=#hashtag_enc#">##hashtag#</a>';
}
var re_hashtag = /(^|\s|\()#([a-zA-Z0-9\-_\.+:=]{1,}\w)([^a-zA-Z0-9\-_+]|$)/gi;
var ms = [];
while (ms = re_hashtag.exec(str))
{
/*
sometimes we can end up with a null instead of a blank string,
so we need to force the issue in javascript.
*/
for (var x=0; x<ms.length; x++) {
if (!ms[x]) {
ms[x] = '';
}
}
var repl_tpl = tpl.replace(/#hashtag#/gi, ms[2]);
repl_tpl = repl_tpl.replace(/#hashtag_enc#/gi, encodeURIComponent(ms[2]));
str = str.replace(ms[0], ms[1]+repl_tpl+ms[3]);
}
return str;
};
/**
* Applies autolink, autolinkTwitterScreenname, autolinkTwitterHashtag
*
* @param {string} str
* @param {oobject} opts
*
* Opts structure:
* {
* 'autolink': {
* 'type' :'both', (email, url, or both)
* 'extra_code':'',
* 'maxlen' :20
* },
* 'screenname': {
* 'tpl':'' // should contain macro '#username#'
* },
* 'hashtag': {
* 'tpl':'' // should contain macros '#hashtag#' and '#hashtag_enc#'
* }
* }
* @member sc.helpers
*/
sc.helpers.makeClickable = function(str, opts) {
var autolink_type, autolink_extra_code, autolink_maxlen, screenname_tpl, hashtag_tpl;
if (!opts) {
opts = {};
}
if (opts.autolink) {
var autolink_type = opts.autolink.type || null;
var autolink_extra_code = opts.autolink.extra_code || null;
var autolink_maxlen = opts.autolink.maxlen || null;
}
if (opts.screenname) {
var screenname_tpl = opts.screenname.tpl || null;
}
if (opts.hashtag) {
var hashtag_tpl = opts.hashtag.tpl || null;
}
str = sc.helpers.autolink(str, autolink_type, autolink_extra_code, autolink_maxlen);
str = sc.helpers.autolinkTwitterScreenname(str, screenname_tpl);
str = sc.helpers.autolinkTwitterHashtag(str, hashtag_tpl);
return str;
};
/**
* Simple html tag remover
* @param {string} str
* @return {string}
* @member sc.helpers
*/
sc.helpers.stripTags = function(str) {
var re = /<[^>]*>/gim;
str = str.replace(re, '');
return str;
};
/**
* Converts the following entities into regular chars: < > " '
* @member sc.helpers
*/
sc.helpers.fromHTMLSpecialChars = function(str) {
str = str.replace(/</gi, '<');
str = str.replace(/>/gi, '>');
str = str.replace(/"/gi, '"');
str = str.replace(/'/gi, '\'');
str = str.replace(/&/gi, '&');
return str;
};
sc.helpers.escape_html = function(string) {
return sc.helpers.htmlspecialchars(string, 'ENT_QUOTES');
};
sc.helpers.htmlspecialchars = function(string, quote_style) {
// http://kevin.vanzonneveld.net
// + original by: Mirek Slugen
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Nathan
// + bugfixed by: Arno
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// - depends on: get_html_translation_table
// * example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');
// * returns 1: '<a href='test'>Test</a>'
var histogram = {}, symbol = '', tmp_str = '', i = 0;
tmp_str = string.toString();
if (false === (histogram = sc.helpers._get_html_translation_table('HTML_SPECIALCHARS', quote_style))) {
return false;
}
// first, do &
tmp_str = tmp_str.split('&').join(histogram['&']);
// then do the rest
for (symbol in histogram) {
if (symbol != '&') {
entity = histogram[symbol];
tmp_str = tmp_str.split(symbol).join(entity);
}
}
return tmp_str;
};
sc.helpers.htmlentities = function(string, quote_style) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: nobbler
// + tweaked by: Jack
// + bugfixed by: Onno Marsman
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// - depends on: get_html_translation_table
// * example 1: htmlentities('Kevin & van Zonneveld');
// * returns 1: 'Kevin & van Zonneveld'
// * example 2: htmlentities("foo'bar","ENT_QUOTES");
// * returns 2: 'foo'bar'
var histogram = {}, symbol = '', tmp_str = '', entity = '';
tmp_str = string.toString();
if (false === (histogram = sc.helpers._get_html_translation_table('HTML_ENTITIES', quote_style))) {
return false;
}
for (symbol in histogram) {
entity = histogram[symbol];
tmp_str = tmp_str.split(symbol).join(entity);
}
return tmp_str;
};
sc.helpers._get_html_translation_table = function(table, quote_style) {
// http://kevin.vanzonneveld.net
// + original by: Philip Peterson
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: noname
// + bugfixed by: Alex
// + bugfixed by: Marco
// + bugfixed by: madipta
// % note: It has been decided that we're not going to add global
// % note: dependencies to php.js. Meaning the constants are not
// % note: real constants, but strings instead. integers are also supported if someone
// % note: chooses to create the constants themselves.
// % note: Table from http://www.the-art-of-web.com/html/character-codes/
// * example 1: get_html_translation_table('HTML_SPECIALCHARS');
// * returns 1: {'"': '"', '&': '&', '<': '<', '>': '>'}
var entities = [], histogram = {}, decimal = 0, symbol = '';
var constMappingTable = {}, constMappingQuoteStyle = {};
var useTable = {}, useQuoteStyle = {};
useTable = (table ? table.toUpperCase() : 'HTML_SPECIALCHARS');
useQuoteStyle = (quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT');
// Translate arguments
constMappingTable[0] = 'HTML_SPECIALCHARS';
constMappingTable[1] = 'HTML_ENTITIES';
constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
constMappingQuoteStyle[2] = 'ENT_COMPAT';
constMappingQuoteStyle[3] = 'ENT_QUOTES';
// Map numbers to strings for compatibilty with PHP constants
if (!isNaN(useTable)) {
useTable = constMappingTable[useTable];
}
if (!isNaN(useQuoteStyle)) {
useQuoteStyle = constMappingQuoteStyle[useQuoteStyle];
}
if (useTable === 'HTML_SPECIALCHARS') {
// ascii decimals for better compatibility
entities.push({'code':38, 'entity':'&'});
if (useQuoteStyle !== 'ENT_NOQUOTES') {
entities.push({'code':34, 'entity':'"'});
}
if (useQuoteStyle === 'ENT_QUOTES') {
entities.push({'code':39, 'entity':'''});
}
entities.push({'code':60, 'entity':'<'});
entities.push({'code':62, 'entity':'>'});
} else if (useTable === 'HTML_ENTITIES') {
// ascii decimals for better compatibility
entities.push({'code':38, 'entity':'&'});
if (useQuoteStyle !== 'ENT_NOQUOTES') {
entities.push({'code':34, 'entity':'"'});
}
if (useQuoteStyle === 'ENT_QUOTES') {
entities.push({'code':39, 'entity':'''});
}
entities.push({'code':60, 'entity':'<'});
entities.push({'code':62, 'entity':'>'});
entities.push({'code':160, 'entity':' '});
entities.push({'code':161, 'entity':'¡'});
entities.push({'code':162, 'entity':'¢'});
entities.push({'code':163, 'entity':'£'});
entities.push({'code':164, 'entity':'¤'});
entities.push({'code':165, 'entity':'¥'});
entities.push({'code':166, 'entity':'¦'});
entities.push({'code':167, 'entity':'§'});
entities.push({'code':168, 'entity':'¨'});
entities.push({'code':169, 'entity':'©'});
entities.push({'code':170, 'entity':'ª'});
entities.push({'code':171, 'entity':'«'});
entities.push({'code':172, 'entity':'¬'});
entities.push({'code':173, 'entity':'­'});
entities.push({'code':174, 'entity':'®'});
entities.push({'code':175, 'entity':'¯'});
entities.push({'code':176, 'entity':'°'});
entities.push({'code':177, 'entity':'±'});
entities.push({'code':178, 'entity':'²'});
entities.push({'code':179, 'entity':'³'});
entities.push({'code':180, 'entity':'´'});
entities.push({'code':181, 'entity':'µ'});
entities.push({'code':182, 'entity':'¶'});
entities.push({'code':183, 'entity':'·'});
entities.push({'code':184, 'entity':'¸'});
entities.push({'code':185, 'entity':'¹'});
entities.push({'code':186, 'entity':'º'});
entities.push({'code':187, 'entity':'»'});
entities.push({'code':188, 'entity':'¼'});
entities.push({'code':189, 'entity':'½'});
entities.push({'code':190, 'entity':'¾'});
entities.push({'code':191, 'entity':'¿'});
entities.push({'code':192, 'entity':'À'});
entities.push({'code':193, 'entity':'Á'});
entities.push({'code':194, 'entity':'Â'});
entities.push({'code':195, 'entity':'Ã'});
entities.push({'code':196, 'entity':'Ä'});
entities.push({'code':197, 'entity':'Å'});
entities.push({'code':198, 'entity':'Æ'});
entities.push({'code':199, 'entity':'Ç'});
entities.push({'code':200, 'entity':'È'});
entities.push({'code':201, 'entity':'É'});
entities.push({'code':202, 'entity':'Ê'});
entities.push({'code':203, 'entity':'Ë'});
entities.push({'code':204, 'entity':'Ì'});
entities.push({'code':205, 'entity':'Í'});
entities.push({'code':206, 'entity':'Î'});
entities.push({'code':207, 'entity':'Ï'});
entities.push({'code':208, 'entity':'Ð'});
entities.push({'code':209, 'entity':'Ñ'});
entities.push({'code':210, 'entity':'Ò'});
entities.push({'code':211, 'entity':'Ó'});
entities.push({'code':212, 'entity':'Ô'});
entities.push({'code':213, 'entity':'Õ'});
entities.push({'code':214, 'entity':'Ö'});
entities.push({'code':215, 'entity':'×'});
entities.push({'code':216, 'entity':'Ø'});
entities.push({'code':217, 'entity':'Ù'});
entities.push({'code':218, 'entity':'Ú'});
entities.push({'code':219, 'entity':'Û'});
entities.push({'code':220, 'entity':'Ü'});
entities.push({'code':221, 'entity':'Ý'});
entities.push({'code':222, 'entity':'Þ'});
entities.push({'code':223, 'entity':'ß'});
entities.push({'code':224, 'entity':'à'});
entities.push({'code':225, 'entity':'á'});
entities.push({'code':226, 'entity':'â'});
entities.push({'code':227, 'entity':'ã'});
entities.push({'code':228, 'entity':'ä'});
entities.push({'code':229, 'entity':'å'});
entities.push({'code':230, 'entity':'æ'});
entities.push({'code':231, 'entity':'ç'});
entities.push({'code':232, 'entity':'è'});
entities.push({'code':233, 'entity':'é'});
entities.push({'code':234, 'entity':'ê'});
entities.push({'code':235, 'entity':'ë'});
entities.push({'code':236, 'entity':'ì'});
entities.push({'code':237, 'entity':'í'});
entities.push({'code':238, 'entity':'î'});
entities.push({'code':239, 'entity':'ï'});
entities.push({'code':240, 'entity':'ð'});
entities.push({'code':241, 'entity':'ñ'});
entities.push({'code':242, 'entity':'ò'});
entities.push({'code':243, 'entity':'ó'});
entities.push({'code':244, 'entity':'ô'});
entities.push({'code':245, 'entity':'õ'});
entities.push({'code':246, 'entity':'ö'});
entities.push({'code':247, 'entity':'÷'});
entities.push({'code':248, 'entity':'ø'});
entities.push({'code':249, 'entity':'ù'});
entities.push({'code':250, 'entity':'ú'});
entities.push({'code':251, 'entity':'û'});
entities.push({'code':252, 'entity':'ü'});
entities.push({'code':253, 'entity':'ý'});
entities.push({'code':254, 'entity':'þ'});
entities.push({'code':255, 'entity':'ÿ'});
} else {
throw Error("Table: "+useTable+' not supported');
}
// ascii decimals to real symbols
for (var i=0; i < entities.length; i++) {
symbol = String.fromCharCode(entities[i].code);
histogram[symbol] = entities[i].entity;
}
return histogram;
};
/**
*
* UTF-8 data encode / decode
* http://www.webtoolkit.info/
* @namespace
**/
sc.helpers.Utf8 = {
/** @function public method for url encoding */
encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
/** @function public method for url decoding */
decode : function (utftext) {
var string = "";
var i = 0;
var c = 0, c1 = 0, c2 = 0, c3 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
};
/**
*
* Javascript trim, ltrim, rtrim
* http://www.webtoolkit.info/
*
**/
sc.helpers.trim = function (str, chars) {
return sc.helpers.ltrim(sc.helpers.rtrim(str, chars), chars);
};
sc.helpers.ltrim = function (str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
};
sc.helpers.rtrim = function (str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
};
/**
* @param {string} input the input string
* @param {number} pad_length the length to pad the string
* @param {string} pad_string the string to pad with
* @param {string} pad_type STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH. Default is STR_PAD_RIGHT
* @member sc.helpers
*/
sc.helpers.pad = function (input, pad_length, pad_string, pad_type) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + namespaced by: Michael White (http://getsprink.com)
// + input by: Marco van Oort
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// * example 1: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT');
// * returns 1: '-=-=-=-=-=-Kevin van Zonneveld'
// * example 2: str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH');
// * returns 2: '------Kevin van Zonneveld-----'
var half = '', pad_to_go;
var str_pad_repeater = function (s, len) {
var collect = '', i;
while (collect.length < len) {collect += s;}
collect = collect.substr(0,len);
return collect;
};
input += '';
pad_string = pad_string !== undefined ? pad_string : ' ';
if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') { pad_type = 'STR_PAD_RIGHT'; }
if ((pad_to_go = pad_length - input.length) > 0) {
if (pad_type == 'STR_PAD_LEFT') { input = str_pad_repeater(pad_string, pad_to_go) + input; }
else if (pad_type == 'STR_PAD_RIGHT') { input = input + str_pad_repeater(pad_string, pad_to_go); }
else if (pad_type == 'STR_PAD_BOTH') {
half = str_pad_repeater(pad_string, Math.ceil(pad_to_go/2));
input = half + input + half;
input = input.substr(0, pad_length);
}
}
return input;
};
/**
* truncate a string to a certain length, if it exceeds that length
*
* @param {string} str
* @param {Number} limit the max length of the string
* @param {string} [suffix] a suffix to append to the string if it is over limit. Does not count against the limit
* @returns {string} the possibly modified string
*/
sc.helpers.truncate = function(str, limit, suffix) {
if (str.length > limit) {
str = str.slice(0, limit);
if (suffix) {
str = str+suffix;
}
}
return str;
};
/**
* @param {string} str the string in which we're converting linebreaks
* @param {string} [breaktag] the tag used to break up lines. defaults to <br>
* @returns {string} the string with linebreaks converted to breaktags
* @member sc.helpers
*/
sc.helpers.nl2br = function(str, breaktag) {
breaktag = breaktag || '<br>';
str = str.replace(/(\r\n|\n\r|\r|\n)/g, breaktag+'$1');
return str;
};