🔪
TextSplitter
Tiny library to split each characters, words or lines! New feature for splitlines function to preserve HTML tags.
Install
npm install textsplitter --save
Usage
import { splitLetters, splitWords, splitLines } from 'textsplitter';
// HTML ELEMENT, STRING, STRING
splitLetters(container, openingtag, closingtag);
// HTML ELEMENT, STRING, STRING
splitWords(container, openingtag, closingtag);
// HTML ELEMENT, STRING, STRING
splitLines(container, openingtag, closingtag);
// React usage
useEffect(() => {
// It is not specific made for React but it works fine
splitLines(ref.current, openingtag, closingtag);
// If not calulating correctly this might be an issue with the fonts which are not ready yet
document.fonts.ready.then(() => {
splitLines(ref.current, openingtag, closingtag);
});
// When running React in strict mode you might want to add an if statement to check if the function already fired
if (!ref.current.querySelector('.lines')) {
splitLines(ref.current, '<span class="lines">', '</span>');
}
}, [])
Example
splitLines(document.getElementById("container"), "<span><thisiscoolhuh>", "</thisiscoolhuh></span>");