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 ba8938d444b9a49bdfb27213826ba108145c2e50
Showing with 3 additions and 2 deletions.
  1. +3 −2 src/core.js
@@ -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

This comment has been minimized.

Copy link

DBJDBJ replied Mar 12, 2010

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

This comment has been minimized.

Copy link
Member Author

jeresig replied Mar 12, 2010

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

This comment has been minimized.

Copy link

DBJDBJ replied Mar 12, 2010

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

@jerone

This comment has been minimized.

Copy link

jerone replied Mar 20, 2010

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

@satyr

This comment has been minimized.

Copy link

satyr replied Mar 20, 2010

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

@jeresig

This comment has been minimized.

Copy link
Member Author

jeresig replied Mar 21, 2010

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

@jerone

This comment has been minimized.

Copy link

jerone replied Mar 21, 2010

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

@satyr

This comment has been minimized.

Copy link

satyr replied Mar 21, 2010

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

This comment has been minimized.

Copy link

jerone replied Mar 21, 2010

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

This comment has been minimized.

Copy link
Member Author

jeresig replied Mar 21, 2010

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

This comment has been minimized.

Copy link

satyr replied Mar 21, 2010

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.
You can’t perform that action at this time.