Skip to content

Instantly share code, notes, and snippets.

@Freezystem
Created July 9, 2017 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Freezystem/7a9434fd224bef1b625dc691b7138240 to your computer and use it in GitHub Desktop.
Save Freezystem/7a9434fd224bef1b625dc691b7138240 to your computer and use it in GitHub Desktop.
Source for getting started with TDD article
const countWords = text => typeof text === 'string' && text.trim() ? text.match(/\S+\s{0,1}/g).length : 0;
const countWords = text => typeof text === 'string' && text.trim() ? text.match(/\S+\s{0,1}/g).length : 0;
describe('countWords()', () => {
it('should handle empty text', () => {
expect(countWords('')).toBe(0);
});
it('should handle one word text', () => {
expect(countWords('nope')).toBe(1);
});
it('should handle n words text', () => {
expect(countWords('tdd is fun bro')).toBe(4);
});
it('should handle text with trailing whitespaces', () => {
expect(countWords(' so is skateboarding ')).toBe(3);
});
it('should handle text with uneven whitespaces separator', () => {
expect(countWords(` I'm 28, I love $#@! and multi-spaces `)).toBe(7);
});
});
console.assert(countWords('') === 0, 'test 0: text is empty');
const countWords = () => {};
console.assert(countWords('') === 0, 'test 0: text is empty');
const countWords = text => text || 0;
console.assert(countWords('') === 0, 'test 0: text is empty');
const countWords = text => text || 0;
console.assert(countWords('') === 0, 'test 0: text is empty');
console.assert(countWords('nope') === 1, 'test 1: text contains 1 word');
const countWords = text => text ? text.split(' ').length : 0;
console.assert(countWords('') === 0, 'test 0: text is empty');
console.assert(countWords('nope') === 1, 'test 1: text contains 1 word');
const countWords = text => text ? text.split(' ').length : 0;
console.assert(countWords('') === 0, 'test 0: text is empty');
console.assert(countWords('nope') === 1, 'test 1: text contains 1 word');
console.assert(countWords('tdd is fun bro') === 4, 'test 2: text contains 4 words');
const countWords = text => text ? text.split(' ').length : 0;
console.assert(countWords('') === 0, 'test 0: text is empty');
console.assert(countWords('nope') === 1, 'test 1: text contains 1 word');
console.assert(countWords('tdd is fun bro') === 4, 'test 2: text contains 4 words');
console.assert(countWords(' so is skateboarding ') === 3, 'test 3: text contains 3 words');
const countWords = text => text ? text.trim().split(' ').length : 0;
console.assert(countWords('') === 0, 'test 0: text is empty');
console.assert(countWords('nope') === 1, 'test 1: text contains 1 word');
console.assert(countWords('tdd is fun bro') === 4, 'test 2: text contains 4 words');
console.assert(countWords(' so is skateboarding ') === 3, 'test 3: text contains 3 words');
const countWords = text => text ? text.trim().split(' ').length : 0;
console.assert(countWords('') === 0, 'test 0: text is empty');
console.assert(countWords('nope') === 1, 'test 1: text contains 1 word');
console.assert(countWords('tdd is fun bro') === 4, 'test 2: text contains 4 words');
console.assert(countWords(' so is skateboarding ') === 3, 'test 3: text contains 3 words');
console.assert(countWords(` I'm 28, I love $#@! and multi-spaces `) === 7, 'test 4: text contains 7 words');
const countWords = text => text ? text.trim().replace(/\s+/g, ' ').split(' ').length : 0;
console.assert(countWords('') === 0, 'test 0: text is empty');
console.assert(countWords('nope') === 1, 'test 1: text contains 1 word');
console.assert(countWords('tdd is fun bro') === 4, 'test 2: text contains 4 words');
console.assert(countWords(' so is skateboarding ') === 3, 'test 3: text contains 3 words');
console.assert(countWords(` I'm 28, I love $#@! and multi-spaces `) === 7, 'test 4: text contains 7 words');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment