Add SplitText component

This commit is contained in:
2021-10-18 16:18:16 +02:00
parent 7f8dc8cec6
commit 691e499e9e
3 changed files with 65 additions and 5 deletions

View File

@@ -24,6 +24,30 @@ export const debounce = (func: Function, timeout: number) => {
}
/**
* Split text
* @description Split a string into words or characters
* @returns string[]
*/
export const splitText = (text: string, mode: string = 'words'): string[] => {
// Split by words
if (mode === 'words') {
const words = text
.replace(/\\n/g, '\n')
.replace(/\s+/g, m => m.includes('\n') ? '\n ' : ' ')
.trim()
.split(' ')
return words
}
// Split by chars
else if (mode === 'chars') {
const chars = Array.from(text).map(char => char === ' ' ? '\xa0' : char)
return chars
}
}
/**
* Linear Interpolation
*/