textsplitter
TypeScript icon, indicating that this package has built-in type declarations

9.6.0 • Public • Published

TextSplitter 🔪

npm version gzip size license dependencies

Description

TextSplitter is a lightweight library designed to split text into individual characters, words, or lines. It also includes a new feature to preserve HTML tags when using the splitLines function.

Installation

You can install TextSplitter using npm or yarn:

npm install textsplitter --save

or

yarn add textsplitter

Usage

Import the required functions from 'textsplitter':

import { splitLetters, splitWords, splitLines } from 'textsplitter';

Use the following functions according to your requirements:

  • splitLetters(container, openingtag, closingtag): Splits individual characters.
  • splitWords(container, openingtag, closingtag): Splits words.
  • splitLines(container, openingtag, closingtag): Splits lines while preserving HTML tags.

React usage example:

useEffect(() => {
  const lines = splitLines(ref.current, openingtag, closingtag);

  // If not calculating correctly, this might be an issue with fonts that are not ready yet
  document.fonts.ready.then(() => {
    splitLines(ref.current, openingtag, closingtag);
  });

  return () => {
    // Destroy lines (return to its initial state)
    lines.destroy();
  };
}, []);

Example

splitLines(document.getElementById("container"), "<span><thisiscoolhuh>", "</thisiscoolhuh></span>");

Output

example.gif

License

MIT © Niels Reijnders


If you're looking for a lightweight library to split text into characters, words, or lines while preserving HTML tags, TextSplitter is the right choice! Easily integrate it into your projects using npm or yarn.

/textsplitter/

    Package Sidebar

    Install

    npm i textsplitter

    Weekly Downloads

    91

    Version

    9.6.0

    License

    MIT

    Unpacked Size

    46 kB

    Total Files

    12

    Last publish

    Collaborators

    • nielsreijnders