Skip to content

Instantly share code, notes, and snippets.

@unRob
Created September 11, 2014 08:58
Show Gist options
  • Save unRob/3e3f7f05550791d52ada to your computer and use it in GitHub Desktop.
Save unRob/3e3f7f05550791d52ada to your computer and use it in GitHub Desktop.
SI TE CAGA QUE ESCRIBAN ASÍ, este plugin es pa ti
String.prototype.toTitleCase = function(){
// Usaría Array.prototype.map, pero ie :/
return jQuery.map(this.toLocaleLowerCase().split(/\s/), function(word){
return word[0].toLocaleUpperCase()+word.slice(1);
}).join(' ');
};
(function(){
var callback = function(hardcore){
return function(evt){
if (!this.value.match(/[a-z]/i)) {
return true;
}
if (this.value === this.value.toLocaleUpperCase()) {
var titlecase = this.value.toTitleCase();
var mensaje = 'Parece que has escrito TODO EN MAYÚSCULAS, tal bestia primitiva. ¿Deseas que lo cambie por "'+titlecase+'"?';
if (confirm(mensaje)){
this.value = titlecase;
} else {
hardcore && this.focus();
}
}
};
};
jQuery.fn.desburocratiza = function(hardcore) {
// hardcore=true: no permite blur de campos en mayúsculas
hardcore = hardcore || true;
this.on('change blur', callback(hardcore));
};
})(jQuery);
// Usage Desburocratiza(document.querySelectorAll('input'))
String.prototype.toTitleCase = function(){
return this.toLocaleLowerCase().split(/\s/).map(function(word){
return word[0].toLocaleUpperCase()+word.slice(1);
}).join(' ');
};
(function(w){
var callback = function(hardcore){
return function(evt){
if (!this.value.match(/[a-z]/i)) {
return true;
}
if (this.value === this.value.toLocaleUpperCase()) {
var titlecase = this.value.toTitleCase();
var mensaje = 'Parece que has escrito TODO EN MAYÚSCULAS, tal bestia primitiva. ¿Deseas que lo cambie por "'+titlecase+'"?';
if (confirm(mensaje)){
this.value = titlecase;
} else {
hardcore && this.focus();
}
}
};
};
w.Desburocratiza = function(els, hardcore) {
hardcore = hardcore || true;
[].forEach.call(els, function(el){
el.addEventListener('change', callback(hardcore));
});
};
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment