Last active
December 22, 2017 22:37
-
-
Save jethrolarson/5d5cd6848b2d9595fbda784ab082a965 to your computer and use it in GitHub Desktop.
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
// Take string and get words as an array. Valid split characters are `,` `.` `\s` `/` `\n` | |
// words :: String -> [String] | |
export const words = str => | |
(str ? str.split(/[\s\n.,/]+/) : []) // split on whitespace | |
.filter(Boolean); |
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
describe('words', () => { | |
it('returns empty for empty', () => { | |
expect(words('')).to.eql([]); | |
}); | |
it('turns space separated words into array', () => { | |
expect(words(`I told you`)).to.eql(['I', 'told', 'you']); | |
}); | |
it('splits on path-like separators', () => { | |
expect(words(`I.told/you`)).to.eql(['I', 'told', 'you']); | |
}); | |
it('splits list-like separators', () => { | |
expect(words(`I, told,you`)).to.eql(['I', 'told', 'you']); | |
}); | |
it('ignores extra whitespace', () => { | |
expect(words(`I told you `)).to.eql(['I', 'told', 'you']); | |
}); | |
it('allows new lines', () => { | |
expect(words(` | |
a | |
bunch | |
of | |
stuff | |
`)).to.eql(['a', 'bunch', 'of', 'stuff']); | |
}); | |
it('ignores extra whitespace', () => { | |
expect(words(`interpolation ${'still'}-works well`)).to.eql( | |
['interpolation', 'still-works', 'well'] | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You know how I would test this? Using data driven snapshots! https://github.com/bahmutov/snap-shot-it#data-driven-testing