Skip to content
This repository has been archived by the owner on Jul 5, 2019. It is now read-only.

Implemented keepDelimiter - keeps delimiter characters at end of each… #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function split (matcher, mapper, options) {
var decoder = new Decoder()
var soFar = ''
var maxLength = options && options.maxLength;
var keepDelimiter = options && options.keepDelimiter === true ? true : false
var trailing = options && options.trailing === false ? false : true
if('function' === typeof matcher)
mapper = matcher, matcher = null
Expand Down Expand Up @@ -44,6 +45,14 @@ function split (matcher, mapper, options) {
if (maxLength && soFar.length > maxLength)
return stream.emit('error', new Error('maximum buffer reached'))

if(keepDelimiter) {
pieces.length % 2 && pieces.push('') // Add trailing empty string if odd elements
for (var i = 0; i < pieces.length; i+=2) {
var piece = pieces[i] + pieces[i+1]
emit(stream,piece)
}
return
}
for (var i = 0; i < pieces.length; i++) {
var piece = pieces[i]
emit(stream, piece)
Expand Down
109 changes: 55 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ Valid options:
split(JSON.parse, null, { trailing: false })
```

* keepDelimiter - By default, delimiters are thrown away. Set this property `true` to keep the trailing delimiter characters at the end of the line. You will also need to keep matched splitter as instructed as follows.

``` js
split(/(\r?\n)/, { keepDelimiter: true }) // Must also use matching group in RE
// Example output: line\n
```


## keep matched splitter

As with `String#split`, if you split by a regular expression with a matching group,
Expand Down