Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created April 16, 2014 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaldron/10891790 to your computer and use it in GitHub Desktop.
Save rwaldron/10891790 to your computer and use it in GitHub Desktop.
ES7 Proposal: String.prototype.trimLeft(), String.prototype.trimRight()

String.prototype.trimRight()

This function interprets a string value as a sequence of code points, as described in 6.1.4.

The following steps are taken:

  1. Let O be CheckObjectCoercible(this value).
  2. Let S be ToString(O).
  3. ReturnIfAbrupt(S).
  4. Let T be a String value that is a copy of S with trailing white space removed. The definition of white space is the union of WhiteSpace and )LineTerminator. When determining whether a Unicode code point is in Unicode general category “Zs”, code unit sequences are interpreted as UTF-16 encoded code point sequences as specified in 6.1.4.
  5. Return T.

NOTE The trimRight 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.

String.prototype.trimLeft()

This function interprets a string value as a sequence of code points, as described in 6.1.4.

The following steps are taken:

  1. Let O be CheckObjectCoercible(this value).
  2. Let S be ToString(O).
  3. ReturnIfAbrupt(S).
  4. Let T be a String value that is a copy of S with leading white space removed. The definition of white space is the union of WhiteSpace and )LineTerminator. When determining whether a Unicode code point is in Unicode general category “Zs”, code unit sequences are interpreted as UTF-16 encoded code point sequences as specified in 6.1.4.
  5. Return T.

NOTE The trimRight 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.

@bterlson
Copy link

👍

@ianwjhalliday
Copy link

Typo ")LineTerminator"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment