Skip to content

Instantly share code, notes, and snippets.

@dcneiner
Forked from dieseltravis/jquery.fastertrim.js
Created March 4, 2010 22:26
Show Gist options
  • Save dcneiner/322189 to your computer and use it in GitHub Desktop.
Save dcneiner/322189 to your computer and use it in GitHub Desktop.
/*
* 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 ) {
// simple values to cache
var empty = "",
hasNbspBug = ( ("\u00a0").replace( /\s/, empty ).length > 0);
jQuery.fasterTrim = {
hasNbspBug: hasNbspBug,
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;
}
},
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() {
var $el = $(this);
if ( options && options.trimHtmlInstead ) {
$el.html( jQuery.fasterTrim.trim( $el.html(), options ) );
} else {
$el.text( jQuery.fasterTrim.trim( $el.text(), options ) );
}
});
};
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment