Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #7369 > unrolled thread

FAQ Topic - How do I trim whitespace? (2011-10-14)

Started by"FAQ server" <javascript@dotinternet.be>
First post2011-10-13 23:00 +0000
Last post2011-10-15 19:17 +0100
Articles 4 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  FAQ Topic - How do I trim whitespace? (2011-10-14) "FAQ server" <javascript@dotinternet.be> - 2011-10-13 23:00 +0000
    Re: FAQ Topic - How do I trim whitespace? (2011-10-14) "J.R." <groups_jr-1@yahoo.com.br> - 2011-10-13 22:09 -0300
      Re: FAQ Topic - How do I trim whitespace? (2011-10-14) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-14 20:54 +0200
      Re: FAQ Topic - How do I trim whitespace? (2011-10-14) Dr J R Stockton <reply1141@merlyn.demon.co.uk> - 2011-10-15 19:17 +0100

#7369 — FAQ Topic - How do I trim whitespace? (2011-10-14)

From"FAQ server" <javascript@dotinternet.be>
Date2011-10-13 23:00 +0000
SubjectFAQ Topic - How do I trim whitespace? (2011-10-14)
Message-ID<4e976d84$0$289$14726298@news.sunsite.dk>
-----------------------------------------------------------------------
FAQ Topic - How do I trim whitespace?
-----------------------------------------------------------------------

ECMAScript 5 defines `String.prototype.trim`. Where not supported,
it can be added as a function that uses a regular expression:

if(!String.prototype.trim) {
String.prototype.trim = function() {
return String(this).replace(/^\s+|\s+$/g, "");
};
}

Implementations are inconsistent with `\s`. For example,
some implementations, notably JScript 5.8 and Safari 2, do not match `\xA0`
(no-break space), among others.

A more consistent approach would be to create a character class
that defines the characters to trim.

<URL: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp>
<URL: http://thinkweb2.com/projects/prototype/whitespace-deviations/>
<URL: https://developer.mozilla.org/en/Firefox_3.1_for_developers>
<URL: http://docs.sun.com/source/816-6408-10/regexp.htm>
<URL: http://msdn.microsoft.com/en-us/library/6wzad2b2%28VS.85%29.aspx>
<URL: http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/c7010139217600c3/31092c5eb99625d0?#31092c5eb99625d0>
<URL: http://unicode.org/Public/UNIDATA/PropList.txt>


The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/

-- 

The sendings of these daily posts are proficiently hosted
by http://www.pair.com.

[toc] | [next] | [standalone]


#7372

From"J.R." <groups_jr-1@yahoo.com.br>
Date2011-10-13 22:09 -0300
Message-ID<j7824g$6lf$1@speranza.aioe.org>
In reply to#7369
On 13/10/2011 20:00, FAQ server wrote:
> -----------------------------------------------------------------------
> FAQ Topic - How do I trim whitespace?
> -----------------------------------------------------------------------
>
> ECMAScript 5 defines `String.prototype.trim`. Where not supported,
> it can be added as a function that uses a regular expression:
>
> if(!String.prototype.trim) {
> String.prototype.trim = function() {
> return String(this).replace(/^\s+|\s+$/g, "");
> };
> }

> The complete comp.lang.javascript FAQ is at
> http://jibbering.com/faq/
>

A few months ago, there was an article at Jibbering,com with a different 
version of this code:

   /** @see http://jibbering.com/faq/#trimString */
   if (typeof String.prototype.trim !== "function") {
     String.prototype.trim = function () {
       // return String(this).replace(/^\s+|\s+$/g, "");
       return this.replace(/^\s+|\s+$/g, "");
     };
   }

I think that is more consistent with ECMA-262 5th ed., 15.5.4.20.

-- 
Joao Rodrigues (J.R.)

[toc] | [prev] | [next] | [standalone]


#7398

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2011-10-14 20:54 +0200
Message-ID<1543388.qVoOGUtdWV@PointedEars.de>
In reply to#7372
J.R. wrote:

> FAQ server wrote:
>> -----------------------------------------------------------------------
>> FAQ Topic - How do I trim whitespace?
>> -----------------------------------------------------------------------
>>
>> ECMAScript 5 defines `String.prototype.trim`. Where not supported,
>> it can be added as a function that uses a regular expression:
>>
>> if(!String.prototype.trim) {
>> String.prototype.trim = function() {
>> return String(this).replace(/^\s+|\s+$/g, "");
>> };
>> }
> 
>> The complete comp.lang.javascript FAQ is at
>> http://jibbering.com/faq/
>>
> 
> A few months ago, there was an article at Jibbering,com with a different
> version of this code:
> 
>    /** @see http://jibbering.com/faq/#trimString */
>    if (typeof String.prototype.trim !== "function") {
>      String.prototype.trim = function () {
>        // return String(this).replace(/^\s+|\s+$/g, "");
>        return this.replace(/^\s+|\s+$/g, "");
>      };
>    }
> 
> I think that is more consistent with ECMA-262 5th ed., 15.5.4.20.

`!x' requires

  RETURN Not(ToBoolean(GetValue(x)));

`typeof x !== y' requires
 
  lref := FUNCTION(x) {
    val := GetValue(x);
    IF Type(val) = Reference
    {
      IF IsUnresolvableReference(v)
      {
        RETURN "undefined";
      }
      val := GetValue(val);
    }
    RETURN GetResultByType(Type(val));
  }(x);
  lval := GetValue(lref);
  rref := Evaluate(y);
  rval := GetValue(rref);
  r := (rval === lval);  /* 11.9.6 */
  RETURN Not(r);

Implicit type conversion may suffice, especially iff it is more efficient 
than `typeof' (benchmarks anyone?); it is not logical to assume that the 
property would be assigned a non-function true-value, or a host object 
reference.

Regardless, strict comparison is pointless here.  `typeof' is not going to 
result in a non-string.

Also, we are at ES 5.1 (2011-06) by now, and section 15.5.4.20 says 
basically what the line does that is commented out here (there are 
implementations that do not implement whitespace as specified in ECMAScript, 
but it appears prudent that whitespace be handled either implementation-
independent or implementation-dependent in all places in a script library).


PointedEars
-- 
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
  -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

[toc] | [prev] | [next] | [standalone]


#7466

FromDr J R Stockton <reply1141@merlyn.demon.co.uk>
Date2011-10-15 19:17 +0100
Message-ID<a8cp2hIS5cmOFwS5@invalid.uk.co.demon.merlyn.invalid>
In reply to#7372
In comp.lang.javascript message <j7824g$6lf$1@speranza.aioe.org>, Thu,
13 Oct 2011 22:09:37, J.R. <groups_jr-1@yahoo.com.br> posted:

>On 13/10/2011 20:00, FAQ server wrote:
>> -----------------------------------------------------------------------
>> FAQ Topic - How do I trim whitespace?
>> -----------------------------------------------------------------------
>>
>> ECMAScript 5 defines `String.prototype.trim`. Where not supported,
>> it can be added as a function that uses a regular expression:
>>
>> if(!String.prototype.trim) {
>> String.prototype.trim = function() {
>> return String(this).replace(/^\s+|\s+$/g, "");
>> };
>> }
>
>> The complete comp.lang.javascript FAQ is at
>> http://jibbering.com/faq/
>>
>
>A few months ago, there was an article at Jibbering,com with a
>different version of this code:
>
>  /** @see http://jibbering.com/faq/#trimString */
>  if (typeof String.prototype.trim !== "function") {
>    String.prototype.trim = function () {
>      // return String(this).replace(/^\s+|\s+$/g, "");
>      return this.replace(/^\s+|\s+$/g, "");
>    };
>  }
>
>I think that is more consistent with ECMA-262 5th ed., 15.5.4.20.


If, before that latter code is executed, String.prototype.trim exists
and is not a function, then it would be better to leave it as it is; a
subsequent attempt to use it as a function will then give an error
message in testing.

In the former code, it would be better to explicitly test against
"undefined".  The cost in execution time will be unimportant; the saving
in time taken to understand may be significant, remembering that the
true expert will not use the FAQ code, having already written what he
needs.

-- 
 (c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   MIME.
   Web  <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
 Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
 Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web