Why does `string.split` return non-empty array when given an empty string?
See original GitHub issue🐛 Bug report
Current Behavior
String.prototype.split
:
''.split('') // => []
fp-ts
:
pipe('', string.split('')) // => ['']
Expected behavior
I would expect []
from string.split
, so it matches String.prototype.split
but also because I think that behaviour makes more sense.
It seems like this was done intentionally: https://github.com/gcanti/fp-ts/commit/7d1887de8b704b6dc86a29964c33f6ca8decb41a#diff-bbade37cef19cf470364082a01329d9fbf208a96862818d50ea97602767d7a48R130
What was the reasoning for this change?
Reproducible example
See above.
Suggested solution(s)
Additional context
Your environment
Which versions of fp-ts are affected by this issue? Did this work in previous versions of fp-ts?
Software | Version(s) |
---|---|
fp-ts | |
TypeScript |
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Why does "split" on an empty string return a non-empty array?
First, split the string by delimiter. The natural consequence is that if the string does not contain the delimiter, a singleton array containing...
Read more >String.split option to ignore empty parts? - Google Groups
Basically String.split works great if your data is 'A,B,C' and you do .split(','), but it doesn't behave as desired if your data is...
Read more >String.Split Method (System) - Microsoft Learn
Returns a string array that contains the substrings in this instance that are ... Split a string delimited by characters and return all...
Read more >String.prototype.split() - JavaScript - MDN Web Docs
When the string is empty and no separator is specified, split() returns an array containing one empty string, rather than an empty array....
Read more >Test for Empty Strings and Missing Values - MATLAB & Simulink
Empty strings contain zero characters and display as double quotes with nothing between them ( "" ). You can determine if a string...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
:trollface: https://github.com/denysdovhan/wtfjs#split-a-string-by-a-space
I don’t see why is that relevant what the native api does. 🤷
I think we’re reframing the classical question of “does the empty set contains itself”. I think both of the choices are okay and it’s more of a design question than a bug.