davepacheco/node-strsplit — pull request #2 by bahamas10 (2012-07-20 22:52:51+00:00)
Title: Default pattern patch 1

I tell the pattern to default to /\s+/ if not specified, to better match how split works in python. I like python's implementation, which is also very simple to bash with `while read field1 field2 field3 ...; do foo; done`, because it will trim the string and split it.

given the string `' field1 field2 field3\tfield4 '` you can see the example below with this patch.

python
``` python
>>> ' field1 field2 field3\tfield4 '.split()
['field1', 'field2', 'field3', 'field4']
>>> ' field1 field2 field3\tfield4 '.split(' ')
['', '', '', 'field1', 'field2', '', '', '', 'field3\tfield4', '', '', '']
>>> ' field1 field2 field3\tfield4 '.split(None, 1)
['field1', 'field2 field3\tfield4 ']
```

node
``` js
> strsplit(' field1 field2 field3\tfield4 ')
[ 'field1',
'field2',
'field3',
'field4' ]
> strsplit(' field1 field2 field3\tfield4 ', ' ')
[ '',
'',
'',
'field1',
'field2',
'',
'',
'',
'field3\tfield4',
'',
'',
'' ]
> strsplit(' field1 field2 field3\tfield4 ', null, 1)
[ 'field1 field2 field3\tfield4' ]
> strsplit(' field1 field2 field3\tfield4 ', null, 2)
[ 'field1', 'field2 field3\tfield4' ]
```

The only difference being that python is 0-indexed when splitting.

This patch will only change the result if the pattern argument is not supplied (or is `null`).