Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
Last active August 29, 2015 14:17

ES7 String trim functions

String.prototype.trim ( )

Return result of StringTrim abstract operation passing this value as thisArg, and TrimBoth as the type.

String.prototype.trimRight ( )

Return result of StringTrim abstract operation passing this value as thisArg, and TrimRight as the type.

String.prototype.trimLeft ( )

Return result of StringTrim abstract operation passing this value as thisArg, and TrimLeft as the type.

NOTE: The trim, trimLeft, and trimRight functions are intentionally generic; they do not require that their this value be a String object. Therefore, they can be transferred to other kinds of objects for use as a method.

Helper abstract operations

StringTrim(thisArg, type) abstract operation

This function interprets a string value as a sequence of UTF-16 encoded code points, as described in <...>.

The following steps are taken:

  1. Let O be RequireObjectCoercible(thisArg).
  2. Let S be ToString(O).
  3. ReturnIfAbrupt(S).
  4. Let T be a String value that is a copy of S with:
  • if type is TrimBoth, then with both leading and trailing white space removed
  • else if type is TrimRight, then with trailing white space removed
  • else if type is TrimgLeft, then both leading white space removed
  • NOTE: 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 <...>.
  1. Return T.
@zloirock
Copy link

@DmitrySoshnikov any plans about adding it to https://github.com/tc39/ecma262 or present it tc39?

@DmitrySoshnikov
Copy link
Author

@zloirock, yeah, exactly wanted to discuss it in the nearest future meeting. I guess we already agreed on having these two methods, we just need to confirm it on a meeting.

@DmitrySoshnikov
Copy link
Author

@zloirock, OK, this was accepted on the latest meeting.

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