/
jquery.shhh.js
71 lines (56 loc) · 1.72 KB
/
jquery.shhh.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
(function( $ ){
/**
* Shhh jQuery plugin
*
* https://github.com/spangborn/Shhh
*/
// Default values
var defaults = {
punctuation : ['.', '!', '?'],
ignore : ["I ", "I'm", "I'll", "I've", "I'd"]
};
// Methods
var methods = {
init : function( target, options ) {
// Extend the options
options = $.extend( defaults, options );
// Call the function on each element in the selection
target.each( function (i, item) {
methods.transform( item, options );
});
return target;
},
transform : function ( target, options ) {
var text = $(target).text();
var pString = methods.punctuationString(options.punctuation);
// Make everything lowercase
text = text.toLowerCase();
// Change back the stuff that's on the "ignore" list
$(options.ignore).each(function ( i , value) {
var ignoreRegexp = new RegExp("(^|\\s+)" + value + "(\\s+|" + pString + ")", 'igm' );
text = text.replace( ignoreRegexp, '$1' + value + '$2' );
});
// Make the start of the string(s) uppercase
// text = text.replace( options.startRegex, methods.upper );
// Make the start of each sentence uppercase
var sentenceBegin = new RegExp('(^|' + pString+')([a-z])', 'igm');
text = text.replace( sentenceBegin , function(str) {
return String(str).toUpperCase();
});
$(target).text(text);
},
punctuationString : function(pArray) {
var str = '';
var nArray = [];
// /((\.|\!|\?)\s+)([a-z])/
$(pArray).each(function(index, punc) {
nArray.push( '\\' + punc);
})
return '((' + nArray.join('|') + ')(\\s+|$))'
}
};
// The actual function definition
$.fn.shhh = function ( options ) {
return methods.init(this, options);
};
})( jQuery );