Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS
vanillajs-helpers > “eachWord”
► eachWord(phrase: string
, cb: IndexLoopCallback, separator?: RegExp
⎮string
): number
Defined in eachWord.ts:27
Iterate over each word in a phrase (or return word count if no callback is given)
// Default seperator
const count = eachWord('Hello JS-World', word => console.log(word));
// Hello ... JS ... World
// count === 3
// With specified separator
const count = eachWord('Hello JS World-Championship', word => console.log(word), ' ');
// Hello ... JS-World
// count === 2
Parameters:
Param | Type | Default value | Description |
---|---|---|---|
phrase | string |
- | Phrase to loop over |
cb | IndexLoopCallback | - | Callback function to call on every iteration |
separator | RegExp ⎮string |
/[- _,]+/ | - |
Returns: number