Skip to content

Commit

Permalink
Add utility function to split strings & preserve the delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Palmer committed Jan 31, 2017
1 parent ed35dee commit f06a736
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions utils/splitPreservingDelimiter/splitPreservingDelimiter.js
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;
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']);
});

0 comments on commit f06a736

Please sign in to comment.