This proposal has progressed to the Draft ECMAScript 6 Specification, which is available for review here: specification_drafts. Any new issues relating to them should be filed as bugs at http://bugs.ecmascript.org. The content on this page is for historic record only and may no longer reflect the current state of the feature described within.

String extras

Current spec proposal:

  • Candidate Library Extensions Spec 3/28/2011 doc pdf

String extras

A few basic conveniences that many languages (e.g., Java, C#, Python, Ruby) have by default are missing in the ECMAScript string library.

IMPLEMENTATION NOTE: These code snippets are given as rough descriptions of intended behavior. They are NOT tested or suggested as production-quality polyfills.

NOTE: all the “behaves the same as” is defined in terms of the “original value of” the various methods used.

  • String.prototype.startsWith

Behaves the same as:

String.prototype.startsWith = function(s) {
    return this.indexOf(s) === 0;
};
  • String.prototype.endsWith

Behaves the same as:

String.prototype.endsWith = function(s) {
    var t = String(s);
    var index = this.lastIndexOf(t);
    return index >= 0 && index === this.length - t.length;
};
  • String.prototype.contains

Behaves the same as:

String.prototype.contains = function(s) {
    return this.indexOf(s) !== -1;
};
  • String.prototype.toArray

Behaves the same as:

String.prototype.toArray = function() {
    return this.split('');
};

Dave Herman 2011/03/13 18:54

Questions:

  • Should contains take an optional position parameter, to start the contains check at that position? This would match the indexOf signature. Proposal: Yes.
  • Should startsWith take an optional position parameter, to start the startsWith check at that position? Proposal: Yes.
  • Should endsWith take an optional position parameter, to end the endsWith check at that position? Proposal: Yes.

Luke Hoban 2011/11/15

 
harmony/string_extras.txt · Last modified: 2013/07/12 00:00 by rwaldron
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki