Created
March 4, 2010 22:21
-
-
Save dieseltravis/322184 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* jQuery fasterTrim Plugin | |
* version: 1.0.1 | |
* @requires jQuery v1.0.x or later | |
* | |
* licensed under the MIT license: | |
* http://www.opensource.org/licenses/mit-license.php | |
* | |
* @version $Id: jquery.fastertrim.js 2 2010-03-04 12:00:00Z $ | |
* @author Travis Hardiman https://forum.jquery.com/user/travis.hardiman http://travis.servebeer.com | |
* 2010-02-22 - initial creation | |
* 2010-03-04 - perf improvements, formatting | |
* | |
*/ | |
/** | |
* Trims strings faster than jQuery.trim() demo: http://jsbin.com/izici3/25 | |
* Usage: | |
* | |
* $(element).fasterTrim(options); // returns jQuery object | |
* $.fasterTrim.trim(" string ", options); // returns trimmed string | |
* | |
* @param String | |
* text value to trim | |
* @param Object | |
* (using custom options will slow the trim down, see jQuery.fasterTrim.defaultOptions object below for parameter info) | |
*/ | |
(function( jQuery ) { | |
jQuery.fasterTrim = function( el, options ) { | |
var base = this; | |
base.$el = jQuery( el ); | |
base.el = el; | |
// Add a reverse reference to the DOM object | |
base.$el.data( "fasterTrim", base ); | |
base.init = function() { | |
base.options = jQuery.extend( {}, jQuery.fasterTrim.defaultOptions, options ); | |
}; | |
// Run initializer | |
base.init(); | |
// trim element text or html | |
if ( !base.options.trimHtmlInstead ) { | |
base.$el.text( jQuery.fasterTrim.trim( base.$el.text(), base.options ) ); | |
} else { | |
base.$el.html( jQuery.fasterTrim.trim( base.$el.html(), base.options ) ); | |
} | |
}; | |
// simple values to cache | |
var empty = ""; | |
var hasNbspBug = ( ("\u00a0").replace( /\s/, empty ).length > 0); | |
jQuery.fasterTrim.hasNbspBug = hasNbspBug; | |
// faster trim | |
jQuery.fasterTrim.trim = function( text, options ) { | |
if ( text ) { | |
if ( options ) { | |
options = jQuery.extend( {}, jQuery.fasterTrim.defaultOptions, options ); | |
if ( options.useNative && typeof String.trim === "function" ) { | |
return text.trim(); | |
} else if ( !options.useLevithan ) { | |
return text.replace( options.trimLeft, options.empty ).replace( options.trimRight, options.empty ); | |
} else { | |
// Levithan function | |
var str = text.replace( options.trimLeft, options.empty ), | |
i = str.length; | |
while ( options.whitespace.test( str.charAt(--i) ) ){} | |
return str.slice( 0, i + 1 ); | |
} | |
} else { | |
// don't test options | |
if ( typeof String.trim === "function" ) { | |
return text.trim(); | |
} else { | |
return text.replace( jQuery.fasterTrim.defaultOptions.trimLeft, empty ).replace( jQuery.fasterTrim.defaultOptions.trimRight, empty ); | |
} | |
} | |
} else { | |
return empty; | |
} | |
}; | |
jQuery.fasterTrim.defaultOptions = { | |
// use native String.Trim() if available | |
useNative: true, | |
// better for larger strings, but slower for smaller ones | |
useLevithan: false, | |
// if IE isn't required or text is guaranteed to never start/end with a NBSP (\xA0) then the "[\s\xA0]" in the following options can safely be changed to "\s" for an even greater speed boost | |
// default regex for trimming beginning of string | |
trimLeft: ( hasNbspBug ) ? /^[\s\xA0]+/ : /^\s+/, | |
// default regex for trimming end of string | |
trimRight: ( hasNbspBug ) ? /[\s\xA0]+$/ : /\s+$/, | |
// default regex for matching whitespace | |
whitespace: ( hasNbspBug ) ? /[\s\xA0]/ : /\s/, | |
// an $element's .text() will be trimmed by default, enable this to trim its .html() (this is only used on a jQuery object) | |
trimHtmlInstead: false, | |
// an empty string, used for testing and replacements, stored here for caching purposes, not intended to be overwritten unless required for crazy hacks | |
empty: "" | |
}; | |
jQuery.fn.fasterTrim = function( options ) { | |
return this.each( function() { | |
( new jQuery.fasterTrim( this, options ) ); | |
}); | |
}; | |
// returns the FasterTrim if it has been attached to the object. | |
jQuery.fn.getFasterTrim = function() { | |
this.data( "fasterTrim" ); | |
}; | |
})( jQuery ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment