-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility function to split strings & preserve the delimiter
- Loading branch information
Richard Palmer
committed
Jan 31, 2017
1 parent
ed35dee
commit f06a736
Showing
2 changed files
with
13 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const splitPreservingDelimiter = (string, delimiter) => string | ||
.replace(new RegExp(delimiter, 'gi'), `,${delimiter},`) | ||
.split(','); | ||
|
||
export default splitPreservingDelimiter; |
8 changes: 8 additions & 0 deletions
8
utils/splitPreservingDelimiter/splitPreservingDelimiter.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import splitPreservingDelimiter from './splitPreservingDelimiter'; | ||
|
||
it('splits a string, preserving the delimiter string', () => { | ||
const string = 'somefancystring'; | ||
const delimiter = 'fancy'; | ||
|
||
expect(splitPreservingDelimiter(string, delimiter)).toEqual(['some', 'fancy', 'string']); | ||
}); |