chore: move utils to package

This commit is contained in:
2023-02-10 17:52:50 +01:00
parent 0c829c88c5
commit 3727b6bb2b
26 changed files with 170 additions and 160 deletions

24
packages/utils/text.ts Normal file
View File

@@ -0,0 +1,24 @@
/**
* Split text
* @description Split a string into words or characters
* @returns string[]
*/
export const splitText = (text: string, mode = '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
}
return []
}