Skip to content

Instantly share code, notes, and snippets.

@dieseltravis
Forked from dcneiner/jquery.fastertrim.js
Created March 4, 2010 22:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dieseltravis/322211 to your computer and use it in GitHub Desktop.
Save dieseltravis/322211 to your computer and use it in GitHub Desktop.
Doug saves the day with a more streamlined plugin and feature suggestions
/*
* jQuery fasterTrim Plugin
* version: 1.0.5
* @requires jQuery v1.0.x or later
*
* dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/gpl-2.0.php
*
* @version $Id: jquery.fastertrim.js 5 2010-03-12 12:00:00Z $
* @author Travis Hardiman https://forum.jquery.com/user/travis.hardiman http://travis.servebeer.com
* with code/help from: Steven Levithan, Ralph Whitbeck, D. Jovanovic, John Resig, Doug Neiner, Dave Methvin
*
* 2010-02-22 - initial creation
* 2010-03-04 - perf improvements, formatting
* 2010-03-04 - Doug's help & suggestions: http://gist.github.com/gists/322211
* 2010-03-08 - Dave's suggestions on separate functions, added GPL, added .trimAttr()
* 2010-03-11 - suggestions from DBJ, John's code from core, added .trimLeft() and .trimRight()
* 2010-03-12 - more of John's code from core, .unicodeTrim()
*
*/
/**
* Trims strings faster than default jQuery.trim() (up until 1.4.3) demo: http://jsbin.com/izici3/56
* Usage:
*
* // these now use the fasterTrim functions
* $.trim( " string " ); // === "string"
* $.trimLeft( " string " ); // === "string "
* $.trimRight( " string " ); // === " string"
*
* // this uses the Levithan trim function, better for larger strings
* // (see http://blog.stevenlevithan.com/archives/faster-trim-javascript )
* $.levithanTrim( " string " );
*
* OR:
*
* $( element ).trimHtml(); // returns jQuery object with HTML trimmed
* $( element ).trimText(); // returns jQuery object with text trimmed
* $( element ).trimAttr( attrToTrim ); // returns jQuery object with the attribute attrToTrim trimmed
*
*/
(function( jQuery ) {
// simple values to cache
var empty = "",
// test for NBSP bug in IE
//TODO: test for other \s bugs as well? http://xregexp.com/tests/unicode.html
hasNbspBug = ( !/\s/.test( "\xA0" ) ),
// do you have native trim? http://jsbin.com/unaqa3/8
hasNativeTrim = jQuery.isFunction( String.prototype.trim ),
hasNativeTrimLeft = jQuery.isFunction( String.prototype.trimLeft ),
hasNativeTrimRight = jQuery.isFunction( String.prototype.trimRight ),
// default regex for trimming beginning of string
rtrimLeft = /^\s+/,
// default regex for trimming end of string
rtrimRight = /\s+$/,
// default regex for matching whitespace
whitespace = /\s/,
trimLeft = ( hasNativeTrimLeft ) ? function( text ) {
return ( text == null ) ?
empty :
String.prototype.trimLeft.call( text );
} : function( text ) {
return ( text == null ) ?
empty :
text.toString().replace( rtrimLeft, empty );
},
trimRight = ( hasNativeTrimRight ) ? function( text ) {
return ( text == null ) ?
empty :
String.prototype.trimRight.call( text );
} : function( text ) {
return ( text == null ) ?
empty :
text.toString().replace( rtrimRight, empty );
},
trim = ( hasNativeTrim ) ? function( text ) {
return ( text == null ) ?
empty :
String.prototype.trim.call( text );
} : function( text ) {
return ( text == null ) ?
empty :
text.toString().replace( rtrimLeft, empty ).replace( rtrimRight, empty );
},
// pass through to native trim since that's still faster
levithanTrim = ( hasNativeTrim ) ? trim : function ( text ) {
if ( text == null ) {
return empty;
} else {
var str = text.toString().replace( rtrimLeft, empty ),
i = str.length;
while ( whitespace.test( str.charAt( --i ) ) ) { }
return str.slice( 0, i + 1 );
}
},
// focus on trimming all visual white-space instead of performance (native trim cannot be relied upon, since it may have bugs)
unicodeWhitespace = ["\u1680" , "\u180e" , "\u2000" , "\u2001" , "\u2002" ,"\u2003", "\u2004", "\u2005", "\u2006", "\u2007", "\u2008", "\u2009", "\u200a", "\u200b", "\u2028", "\u2029", "\u202F", "\u205f", "\u3000"],
unmatchedUnicode = (function () {
// return string of escaped unicode chars not matched by \s
var unmatched = ( hasNbspBug ) ? "\\xA0" : empty;
for ( var i = unicodeWhitespace.length; i--; ) {
if ( !/\s/.test( unicodeWhitespace[i] ) ) {
unmatched += "\\u" + unicodeWhitespace[i].charCodeAt(0).toString(16);
}
}
return unmatched;
})(),
runicodeTrimLeft = new RegExp("^[\\s" + unmatchedUnicode + "]+"),
runicodeTrimRight = new RegExp("[\\s" + unmatchedUnicode + "]+$"),
unicodeTrim = function ( text ) {
return ( text == null ) ?
empty :
text.toString().replace( runicodeTrimLeft, empty ).replace( runicodeTrimRight, empty );
};
if ( hasNbspBug ) {
rtrimLeft = /^[\s\xA0]+/;
rtrimRight = /[\s\xA0]+$/;
whitespace = /[\s\xA0]/;
}
jQuery.fasterTrim = {
hasNbspBug: hasNbspBug,
hasNativeTrim: hasNativeTrim,
rtrimLeft: rtrimLeft,
rtrimRight: rtrimRight,
whitespace: whitespace,
trimLeft: trimLeft,
trimRight: trimRight,
trim: trim,
levithanTrim: levithanTrim,
unmatchedUnicode: unmatchedUnicode,
runicodeTrimLeft: runicodeTrimLeft,
runicodeTrimRight: runicodeTrimRight,
unicodeTrim: unicodeTrim
};
jQuery.fn.trimHtml = function() {
return this.each( function() {
var $el = jQuery( this );
$el.html( jQuery.fasterTrim.trim( $el.html() ) );
});
};
jQuery.fn.trimText = function() {
return this.each( function() {
var $el = jQuery( this );
$el.text( jQuery.fasterTrim.trim( $el.text() ) );
});
};
jQuery.fn.trimAttr = function( attrToTrim ) {
return this.each( function() {
var $el = jQuery( this );
$el.attr( attrToTrim, jQuery.fasterTrim.trim( $el.attr( attrToTrim ) ) );
});
};
// overwrite jQuery's default trim after backing it up in _trim
jQuery.fasterTrim._trim = jQuery.trim;
jQuery.trim = jQuery.fasterTrim.trim;
// add new functions
jQuery.trimLeft = jQuery.fasterTrim.trimLeft;
jQuery.trimRight = jQuery.fasterTrim.trimRight;
jQuery.levithanTrim = jQuery.fasterTrim.levithanTrim;
jQuery.unicodeTrim = jQuery.fasterTrim.unicodeTrim;
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment