Skip to content

Instantly share code, notes, and snippets.

@jethrolarson
Last active December 22, 2017 22:37
Show Gist options
  • Save jethrolarson/5d5cd6848b2d9595fbda784ab082a965 to your computer and use it in GitHub Desktop.
Save jethrolarson/5d5cd6848b2d9595fbda784ab082a965 to your computer and use it in GitHub Desktop.
// 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);
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']
);
});
});
@bahmutov
Copy link

You know how I would test this? Using data driven snapshots! https://github.com/bahmutov/snap-shot-it#data-driven-testing

it('works', () => {
  snapshot(words, '', 'I told you', 'I.told/you', ...)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment