#345 ✓fixed_in_master_branch
Wilson Lee

Make String.strip() use native String.prototype.trim() if possible

Reported by Wilson Lee | September 16th, 2008 @ 09:21 AM | in 1.6.1

Currently, Prototype's String.strip function is implemented by stripping whitespaces on both ends of the string with regular expressions. The trim() function was recently implemented natively in Firefox (https://bugzilla.mozilla.org/sho...), and the trim() function is also in the ES3.1 specs draft, so I expect to see more browsers follow suit.

I haven't done any benchmarks yet, but a native trim() function should be way faster than Prototype's strip() implementation, so this patch for strip() uses the native trim() function if possible.

Comments and changes to this ticket

  • Juriy Zaytsev

    Juriy Zaytsev September 16th, 2008 @ 03:08 PM

    • Milestone set to 1.7
    • Assigned user set to “Juriy Zaytsev”
    • State changed from “new” to “enhancement”
    • Tag changed from enhancement to enhancement, needs_patch, needs_tests

    Wilson, there's really no need to execute Object.isFunction(String.prototype.trim) every time strip runs ;)

    I would suggest something like this:

    
    ...
    strip: (function(){
      // if trim exists
      if (String.prototype.trim) {
        // check if it really does what it should do
        try {
          if (' _ '.trim().length === 1)
            // return reference to native `trim`
            return String.prototype.trim;
        } catch(e) { }
      }
      // fallback
      return function() {
        this.replace(/^\s+/, '').replace(/\s+$/, '')
      }
    })(),
    ...
    
  • Juriy Zaytsev

    Juriy Zaytsev September 16th, 2008 @ 05:19 PM

    String.prototype.trim conforms (or at least it should) to ES3.1 specs:

    
    15.5.4.20 String.prototype.trim ( )
    
    If this object is not already a string, it is converted to a string. The result is a copy of the string with
    both leading and trailing white space removed. The definition of white space is the union of
    WhiteSpace and LineTerminator. The result is a string value, not a String object.
    
    NOTE
    The trim function is intentionally generic; it does not require that its this value be a String object.
    Therefore, it can be transferred to other kinds of objects for use as a method.
    
  • Phred

    Phred December 18th, 2008 @ 04:09 PM

    Just a side note: Where did String#strip come from? Trim seems to be the conventional name for the function. Is strip used in some language I am unfamiliar with?

  • Juriy Zaytsev

    Juriy Zaytsev December 18th, 2008 @ 05:13 PM

    @fearphage

    That would be ruby : )

  • kazuyoshi tlacaelel
  • Wilson Lee

    Wilson Lee December 19th, 2008 @ 09:44 AM

    Python also calls it strip. I will soon get my lazy bum up and reroll this patch.

  • Tobie Langel

    Tobie Langel July 23rd, 2009 @ 02:44 AM

    • State changed from “enhancement” to “fixed_in_trunk”
    • Milestone changed from 1.7 to 1.6.1
  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:26 AM

    • Tag changed from enhancement, needs_patch, needs_tests to missing:tests, needs_patch

    [not-tagged:"needs_tests" tagged:"missing:tests" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:28 AM

    • Tag changed from missing:tests, needs_patch to missing:patch, missing:tests

    [not-tagged:"needs_patch" tagged:"missing:patch" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:36 AM

    • Tag changed from missing:patch, missing:tests to missing:patch, needs:tests

    [not-tagged:"missing:tests" tagged:"needs:tests" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:37 AM

    • Tag changed from missing:patch, needs:tests to needs:patch, needs:tests

    [not-tagged:"missing:patch" tagged:"needs:patch" bulk edit command]

  • Tobie Langel

    Tobie Langel March 1st, 2010 @ 12:15 AM

    • State changed from “fixed_in_trunk” to “fixed_in_master_branch”

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

Attachments

Pages