-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add _.startsWith and _.endsWith #668
Conversation
The coercion issue is the same as here: #667 – and the two should probably be consistent. |
Should these accept an arbitrary number of arguments? For example: _.startsWith(url, 'http:', 'https:') Equivalent to: url.indexOf('http:') == 0 || url.indexOf('https:') == 0 What about regular expressions? _.startsWith(url, /https?:/) Equivalent to: /^https?:/.test(url) If we ignore regular expressions, this evaluates as true: _.startsWith('/@+/123', /@+/)
Related: Underscore.string |
In my opinion it should do only es6 stuff and fallback to native version. |
I don't disagree. I'm suggesting that ES6, too, treat regular expressions as patterns in this context. (I should bring this up in a more appropriate forum.) |
Ouch, coercing a regex to a string leads to something quite unexpected it seems. It would seem unusual to reject a regex entirely, which would make me inclined to think it should handle a regex intelligently. In Python you can do I looked at the Underscore.string implementations but did not find them particularly inspiring. |
These String extensions may be worth a look, Ian. |
For what it's worth, our general policy has always been to leave string methods out of Underscore, as there are a large number of useful ones, and folks rarely agree on which. See many previous tickets. I'd bet if you worked on making a complete "string function" library using your best judgement, you might end up with something that approaches the size of Underscore in the first place. That's one fine way to go... ... I think the only circumstance under which we should merge string functions in would be:
|
The reason I chose these two functions (and extending
Instead of proposing a complete list of functions I thought instead I'd start with the approach of proposing what I think are the least controversial functions and see how far it goes. If I was making a longer list I'd probably add:
At least that's what I get from looking at Underscore.string, the CoffeeScript helpers that were linked to, MooTools String. But a selection depends some on the criteria – for instance, I am personally biased against language-related functions like capitalize or camelCase. |
RegExp issues noted here: https://bugs.ecmascript.org/show_bug.cgi?id=498 |
I think we're going to stick with tradition here, and leave string helpers to the domain of other libraries, or Underscore plugins. I don't feel like just adding:
... would make for a very satisfactory set of functionality. |
This adds
_.startsWith
and_.endsWith
. These work with both strings and arrays.Both methods are proposed on strings in Harmony: http://wiki.ecmascript.org/doku.php?id=harmony:string_extras – extending them to also work on arrays seems natural.
This makes specific tests for arrays, because it only makes sense for ordered collections.
_.isArray
seems crude and maybe wrong (e.g., why shouldn't it work with childNodes or something array-like), but I'm not sure how better to detect indexable ordered collections.Also if the types don't work out it returns false. Should it throw an error? And there's implicit string coercion, which maybe is okay or maybe not.