Comments (12)
I would say I'm not really in favour of it, personally. We're not trying to replicate the JS API here, and it already has stripPrefix
and stripSuffix
which can be used for this purpose - generally these functions are more useful too, as often a startsWith would be followed by chopping the prefix off too. Not having the test-only versions hopefully means people will stumble over these more useful functions, rather than being stuck in their JS ways.
That's just my opinion though 🙂
from purescript-strings.
To be clear, startsWith prefix
is isJust <<< stripPrefix prefix
.
from purescript-strings.
I think a backend which uses a mutable type for Strings is probably incorrect; if you want to write a PureScript backend for a language which does not provide an immutable String type, then you should probably map PureScript strings to some other type in your backend which is an immutable string. We should really write a spec for what is expected of a backend, and specifically how the primitive types work. There are also problems around the encoding of a String as well (whether invalid UTF-16 is allowed, for instance).
from purescript-strings.
@mhmdanas I'm just throwing in a point-of-view that startsWith/endsWith are not always functions you don't want. I've written lots of code that checks a prefix to classify something, but still needs to retain the prefix for convenience. I'm not really fighting for their inclusion, other than saying that it's expected functionality, and I think it's fine if we did include them while also suggesting that stripPrefix/stripSuffix may be more appropriate.
from purescript-strings.
@garyb you do make a good point. I just think that the discoverability of this is low for new people. Should I add an example to the cookbook?
from purescript-strings.
Sure! That sounds like a good idea.
from purescript-strings.
I actually ran into this issue a week ago. I ended up using stringutils
because my goal was to solve my problem (does this string start with "X"?), not think about how I could achieve my goal via some other way. For some reason, stripPrefix
never stood out to me as being helpful.
So, I would agree that discoverability is the issue here.
from purescript-strings.
FWIW I've somewhat often wanted startsWith/endsWith. I would not be opposed to adding them with docs that refer to stripPrefix/stripSuffix.
from purescript-strings.
I'm not sure what to do here. Should I open a PR here and add startsWith
and endsWith
like @natefaubion is suggesting, or should I add examples to the cookbook?
from purescript-strings.
What about performance, is it not a consideration? I mean if this is a standard functions of String in JS.
from purescript-strings.
Stripping a string prefix is cheap (i.e. O(1)) in JS because JS strings are immutable. We should avoid unnecessary FFI here because that creates problems for alternative backends.
from purescript-strings.
Stripping a string prefix is cheap (i.e. O(1)) in JS because JS strings are immutable.
We can't be sure that is the case on other backends (it might as well be for at least most; I'm not sure).
We should avoid unnecessary FFI here because that creates problems for alternative backends.
I don't think it should be much of a problem because it's just two extra functions, but yeah, it's still two more functions to implement in the FFI.
from purescript-strings.
Related Issues (20)
- Data.String.CodePoints.uncons probably isn't constant-time HOT 4
- stripPrefix performs full linear search in order to check if index is 0
- Docs are not up to date on Pursuit HOT 5
- Data.String.Unsafe.char is not Unicode-aware HOT 1
- Suggestion: allow `slice (length s) (length s)` on Strings HOT 15
- Add trimStart/trimEnd?
- Add support for matchAll? HOT 1
- Add support for dotAll flag? HOT 5
- Allow slicing from index to end of string HOT 1
- `split` should return a NonEmptyArray? HOT 3
- fromCharCode BMP HOT 5
- CodePoints.uncons performance optimization? HOT 3
- Efficient CodePoint indexing function? HOT 11
- Add a `isWellFormed` function? HOT 2
- Reuse `Replacement` in `Regex.replace` HOT 2
- genUnicodeString generates invalid unicode HOT 4
- Should `localeCompare` return in `Effect`? HOT 7
- Add takeEnd & dropEnd
- Remove bounds check and exception from Data.String.Unsafe foreign code? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from purescript-strings.