String.prototype.until
Hello all, I recently ran into a situation where I would like to obtain a substring from the beginning until the first encounter with another substring. This promoted me to write a simple function, called until and I wondered if it would be something to add with the other string extras for ES.next. It could be defined as acting the same way as the following code: String.prototype.until = function (needle) { return this.substr(0, this.indexOf(needle)); } -- Adam Shannon Web Developer University of Northern Iowa Sophomore -- Computer Science B.S. Mathematics http://ashannon.us ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: String.prototype.until
Semantically, calling it until makes me think that if the needle isn't found, it should return the entire haystack. Your example implementation would return an empty string in that case. Also, to keep consistency with other string methods like substr, shouldn't we allow the developer to decide the starting index? String.prototype.until = function (start, needle) { return + (this.substr(start, this.indexOf(needle)) || this); } (The [ +] part is probably not necessary, but it makes it easier to see the implementation work in the console.) Michael A. Smith Web Developer True Action Network (an eBay Company) On Mon, Jan 2, 2012 at 12:03 PM, Adam Shannon a...@ashannon.us wrote: Hello all, I recently ran into a situation where I would like to obtain a substring from the beginning until the first encounter with another substring. This promoted me to write a simple function, called until and I wondered if it would be something to add with the other string extras for ES.next. It could be defined as acting the same way as the following code: String.prototype.until = function (needle) { return this.substr(0, this.indexOf(needle)); } -- Adam Shannon Web Developer University of Northern Iowa Sophomore -- Computer Science B.S. Mathematics http://ashannon.us ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: String.prototype.until
Isn’t that usually better handled via a regular expression? One of the use cases for quasis [1][2] is to make it easy to insert literal text into a regular expression. That seems pertinent here. Example: re`\d+(${localeSpecificDecimalPoint}\d+)?` The text in the variable localeSpecificDecimalPoint is matched literally by the regular expression produced by re``. [1] http://wiki.ecmascript.org/doku.php?id=harmony:quasis [2] http://www.2ality.com/2011/09/quasi-literals.html On Jan 2, 2012, at 18:03 , Adam Shannon wrote: Hello all, I recently ran into a situation where I would like to obtain a substring from the beginning until the first encounter with another substring. This promoted me to write a simple function, called until and I wondered if it would be something to add with the other string extras for ES.next. It could be defined as acting the same way as the following code: String.prototype.until = function (needle) { return this.substr(0, this.indexOf(needle)); } -- Dr. Axel Rauschmayer a...@rauschma.de home: rauschma.de twitter: twitter.com/rauschma blog: 2ality.com ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: String.prototype.until
Yes, I see the use for returning the entire string if the needle isn't found. I was also thinking about a dynamic start position, which is why I'd favor something like this. String.prototype.until = function (needle, start) { start ?? 0; return this.substr(start, this.indexOf(needle)) || this } It seems weird to call something like str.until(bc, 2), but the other option would be to check if arguments[0] is a number, and if so to set start = arguments[0] and needle arguments[1]. On Mon, Jan 2, 2012 at 11:48, Michael A. Smith mich...@smith-li.com wrote: Semantically, calling it until makes me think that if the needle isn't found, it should return the entire haystack. Your example implementation would return an empty string in that case. Also, to keep consistency with other string methods like substr, shouldn't we allow the developer to decide the starting index? String.prototype.until = function (start, needle) { return + (this.substr(start, this.indexOf(needle)) || this); } (The [ +] part is probably not necessary, but it makes it easier to see the implementation work in the console.) Michael A. Smith Web Developer True Action Network (an eBay Company) On Mon, Jan 2, 2012 at 12:03 PM, Adam Shannon a...@ashannon.us wrote: Hello all, I recently ran into a situation where I would like to obtain a substring from the beginning until the first encounter with another substring. This promoted me to write a simple function, called until and I wondered if it would be something to add with the other string extras for ES.next. It could be defined as acting the same way as the following code: String.prototype.until = function (needle) { return this.substr(0, this.indexOf(needle)); } -- Adam Shannon Web Developer University of Northern Iowa Sophomore -- Computer Science B.S. Mathematics http://ashannon.us ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss -- Adam Shannon Web Developer University of Northern Iowa Sophomore -- Computer Science B.S. Mathematics http://ashannon.us ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: String.prototype.until
Alex, I'm confused as to what regular expressions would help with in this case. (Over .indexOf) The idea of .util() would be to return a new string which is just a substring, but provided as an ease of use to the developer. The case where I wrote .util() was in parsing out two comma separated values. I only needed the first, as could also be seen with trying to pull the first name (assuming just a first and last name), you could easily call name.until(' ') and get that back. On Mon, Jan 2, 2012 at 11:55, Axel Rauschmayer a...@rauschma.de wrote: Isn’t that usually better handled via a regular expression? One of the use cases for quasis [1][2] is to make it easy to insert literal text into a regular expression. That seems pertinent here. Example: re`\d+(${localeSpecificDecimalPoint}\d+)?` The text in the variable localeSpecificDecimalPoint is matched literally by the regular expression produced by re``. [1] http://wiki.ecmascript.org/doku.php?id=harmony:quasis [2] http://www.2ality.com/2011/09/quasi-literals.html On Jan 2, 2012, at 18:03 , Adam Shannon wrote: Hello all, I recently ran into a situation where I would like to obtain a substring from the beginning until the first encounter with another substring. This promoted me to write a simple function, called until and I wondered if it would be something to add with the other string extras for ES.next. It could be defined as acting the same way as the following code: String.prototype.until = function (needle) { return this.substr(0, this.indexOf(needle)); } -- Dr. Axel Rauschmayer a...@rauschma.de home: rauschma.de twitter: twitter.com/rauschma blog: 2ality.com -- Adam Shannon Web Developer University of Northern Iowa Sophomore -- Computer Science B.S. Mathematics http://ashannon.us ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: String.prototype.until
/^(.*?)needle.*$/.exec(foobar_needle)[1] 'foobar_' On Jan 2, 2012, at 19:04 , Adam Shannon wrote: Alex, I'm confused as to what regular expressions would help with in this case. (Over .indexOf) The idea of .util() would be to return a new string which is just a substring, but provided as an ease of use to the developer. The case where I wrote .util() was in parsing out two comma separated values. I only needed the first, as could also be seen with trying to pull the first name (assuming just a first and last name), you could easily call name.until(' ') and get that back. On Mon, Jan 2, 2012 at 11:55, Axel Rauschmayer a...@rauschma.de wrote: Isn’t that usually better handled via a regular expression? One of the use cases for quasis [1][2] is to make it easy to insert literal text into a regular expression. That seems pertinent here. Example: re`\d+(${localeSpecificDecimalPoint}\d+)?` The text in the variable localeSpecificDecimalPoint is matched literally by the regular expression produced by re``. [1] http://wiki.ecmascript.org/doku.php?id=harmony:quasis [2] http://www.2ality.com/2011/09/quasi-literals.html On Jan 2, 2012, at 18:03 , Adam Shannon wrote: Hello all, I recently ran into a situation where I would like to obtain a substring from the beginning until the first encounter with another substring. This promoted me to write a simple function, called until and I wondered if it would be something to add with the other string extras for ES.next. It could be defined as acting the same way as the following code: String.prototype.until = function (needle) { return this.substr(0, this.indexOf(needle)); } -- Dr. Axel Rauschmayer a...@rauschma.de home: rauschma.de twitter: twitter.com/rauschma blog: 2ality.com -- Adam Shannon Web Developer University of Northern Iowa Sophomore -- Computer Science B.S. Mathematics http://ashannon.us -- Dr. Axel Rauschmayer a...@rauschma.de home: rauschma.de twitter: twitter.com/rauschma blog: 2ality.com ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss