Skip to content
Permalink
Browse files
Switch to using String.prototype.trim from String.trim as it's more-w…
…idely available.
  • Loading branch information
jeresig committed Mar 11, 2010
1 parent da26d0e commit ba8938d
Showing 1 changed file with 3 additions and 2 deletions.
@@ -53,6 +53,7 @@ var jQuery = function( selector, context ) {
hasOwn = Object.prototype.hasOwnProperty,
push = Array.prototype.push,
slice = Array.prototype.slice,
trim = String.prototype.trim,
indexOf = Array.prototype.indexOf;

jQuery.fn = jQuery.prototype = {
@@ -569,11 +570,11 @@ jQuery.extend({
},

// Use native String.trim function wherever possible
trim: String.trim ?
trim: trim ?
function( text ) {
return text == null ?
"" :
String.trim( text );
trim.call( text );
} :

// Otherwise use our own trimming functionality

11 comments on commit ba8938d

@DBJDBJ
Copy link

@DBJDBJ DBJDBJ commented on ba8938d Mar 12, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't believe how much time we have spent discussing string trim .... And ... I still can't stop ?
What strikes me above is that if String.prototype.trim is available then using the native trim boils down .. to erm, actually using it :
function( text ) {
return text.trim() ;
} :
// Otherwise use our own trimming functionality
Above will also behave properly, and in the case of text , not being a string, the above will/should throw TypeError.
--DBJ

@jeresig
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would really prefer to not throw an exception - we generally try to be forgiving of user input (trying to handle null/undefined gracefully, for example) and I'd like to continue that trend.

@DBJDBJ
Copy link

@DBJDBJ DBJDBJ commented on ba8938d Mar 12, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough.
function( text ) {
return ! text ? "" : text.toString().trim() ;
} :
// Otherwise use our own trimming functionality
-- DBJ

@jerone
Copy link

@jerone jerone commented on ba8938d Mar 20, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:
function( text ) {
return text && text.toString().trim() || "";
} :

@satyr
Copy link

@satyr satyr commented on ba8938d Mar 20, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

text && text.toString().trim() || ""
that's merely an awkward version of:
text ? text.toString().trim() : ""

@jeresig
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course none of those solutions handle trimming false or 0 correctly.

@jerone
Copy link

@jerone jerone commented on ba8938d Mar 21, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok true, but why you want to trim non-strings ?

@satyr
Copy link

@satyr satyr commented on ba8938d Mar 21, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course none of those solutions handle trimming false or 0 correctly.

It didn't before. 1.3.2 had:

return (text || "").replace( ...

If false should be trimmed to "false", why not null/undefined -> "null"/"undefined"?

@jerone
Copy link

@jerone jerone commented on ba8938d Mar 21, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. That's what I was thinking.
Personal I think trim should only be executed on Strings, but EcmaScript says anything can be trimmed (even null).

@jeresig
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bug fix from what was in 1.3.2, we're now implementing string trimming in accordance with the built-in string trimming - and this includes trimming non-strings (which is why we do .toString() in the first place). In the built-in string trimming working against null/undefined is equivalent to trimming the global object which is highly counter-intuitive - so we return an empty string instead (which a much more intuitive result and more inline with the rest of the jQuery API).

@satyr
Copy link

@satyr satyr commented on ba8938d Mar 21, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we return an empty string instead

I prefer "null" and "undefined" which are more informative on debug.

return String(text).trim();

Please sign in to comment.