question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 2 years ago
  • Reactions:5
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
enricopolanskicommented, Dec 1, 2021
  1. Because that’s the behaviour of String.prototype.split.
  2. pipe('ab', string.split('')) returns ['a', 'b'], which suggests that a '' separator will split each character, but if '' is the input, there are no characters to split

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found