Last active
May 6, 2018 20:46
-
-
Save IgorMing/c079260cedb24cbfdf5605b9c70bacad to your computer and use it in GitHub Desktop.
Almost like my wordWrapper function, but a function to create custom words (I've created it to work with React Native)
This file contains hidden or 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
function customWords(text, customStyles, ...wordsToWrap) { | |
// Pattern to be considered to split every word | |
const regexp = /([\A-zÀ-ÿ]+|([!.,:?]))/gi; | |
// Separate the phrase with all the words and characters "!.,:?" | |
const splittedText = text.match(regexp); | |
// Convert everything for lower case, to compare it correctly | |
// and breaking words to wrap even if it contains some compound word | |
const splittedWordsToWrap = wordsToWrap.reduce((acc, curr) => { | |
const each = curr.match(regexp); | |
each.forEach(eachChild => { | |
acc.push(eachChild.toLowerCase()); | |
}); | |
return acc; | |
}, []); | |
// Wrap every word to an object, exposing which words should be customized | |
const wrappedSplittedText = splittedText.reduce((acc, curr) => { | |
acc.push({ | |
customize: splittedWordsToWrap.includes(curr.toLowerCase()), | |
text: curr | |
}); | |
return acc; | |
}, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment