Capitalize!
Sentence capitalization functions
by SirKoik
HTML
<div id="samples">
</div>
<textarea id="caps-input">this is some sample text.</textarea>
<div class="opt-container">
<label>Format
<select id="opt-selector">
<option>Capitalize first letter</option>
<option>Capitalize All First Letters</option>
<option>Title Style</option>
<option>Original style</option>
</select></label><br/>
<label><input type="checkbox" id="preserve-acronyms" checked />Preserve acronym capitalization</label><br/>
<label><input type="checkbox" id="preserve-capitalized" checked />Preserve word capitalization</label><br/>
<button id="format">Format capital letters</button>
</div>
<br/>
<div class="message" style="border-color: orange">
<div style="background-color: orange">Need to know more</div>
<div>
More text here...
</div>
<div>
</div>
<div>
</div>
</div>
<div id="outputs">
<div id="output-container" class="message">
<div class="output-container-title">Output</div>
<div class="caps-output">
Output...
</div>
</div>
</div>
CSS
textarea {
width: 100%;
height: 200px;
}
div.opt-container {
width: 300px;
padding: 0 auto;
}
input, select {
margin: 10px;
}
div#output-container {
display: none;
margin: 5px;
}
div.message {
border: 1px solid #000;
background-color: #fff;
display: none;
}
div.message div:first-child {
padding: 5px;
color: #fff;
background-color: #000;
}
div.message div:nth-child(2) {
padding: 10px;
}
JavaScript
var samples = [
'THIS IS CAPITALIZED TEXT.',
'this is not capitalized text.',
'tHIs iS inCORRecTLY CAPITALIZeD texT.',
'IBM and SAP are global companies.', 'Information Technology is a big field. It has a well-known acronym: IT.',
'AJAX is a very powerful tool in JavaScript.',
'the USA, UK, and UAE are all countries. India, Denmark, and Sweden are countries as well.',
"Titles aren't meant to be too long, and a title isn't meant to be elaborate in detail.",
"The Yarkovsky-O'Keefe–Radzievskii–Paddack effect is caused by sunlight pushing on an asteroid."
];
var formats = [
'Capitalize first letter',
'Capitalize all first letters',
'Title style',
'Original style'
];
/*
* @style first letter, all first letters, title
*/
function capitals(input, outFormat) {
if (outFormat == undefined || outFormat == 3) return input;
this.preserveAcronyms = $('#preserve-acronyms').is(':checked');
this.preserveCapitalized = $('#preserve-capitalized').is(':checked');
this.init = function() {
var textArr = input.split(/(\s|\-|\–)/);
var textArrNew = [];
var str = '';
var titleLowerCase = ['a', 'an', 'the', 'and', 'or', 'nor', 'but', 'as', 'at', 'by', 'for', 'in', 'of', 'per', 'to', 'is', 'isn\'t', 'are', 'aren\'t', 'was', 'be', 'its'];
var newSentence = false;
for (var x = 0; x < textArr.length; x++) {
var t = textArr[x];
if (t == ' ' || t == '-') {
textArrNew.push(t);
continue;
}
if (outFormat === 1 || ((outFormat == 0 || outFormat == 2) && (x == 0 || newSentence))) {
if (this.preserveAcronyms && this.isAcronym(t)) {
str = t;
} else {
str = this.firstLetter(t);
}
} else {
if (outFormat == 2) {
if (this.preserveAcronyms && this.isAcronym(t)) {
str = t;
} else {
if (this.preserveCapitalized && t[0] === t[0].toUpperCase()) {
str = t;
} else {
str =...