JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1309
  • Score
    100M100P100Q112457F
  • License ISC

Truncates the text at the last space before the given length.

Package Exports

  • truncate-text-between-words
  • truncate-text-between-words/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (truncate-text-between-words) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Truncates the text at the last space before the given length. Adds "..." to indicate that the text is truncated.

Installation

npm i truncate-text-between-words

Usage

import truncateText from "truncate-text-between-words";
import { truncateText, getPositionOfLastSpaceBeforeIndex } from "truncate-text-between-words";
const truncateText = require("truncate-text-between-words").default;
const { truncateText, getPositionOfLastSpaceBeforeIndex } = require("truncate-text-between-words");
                  5    10   15   20   25
                  ↓    ↓    ↓    ↓    ↓
const TEXT = "Lorem ipsum dolor sit amet.";

truncateText

truncateText( text, maxLength [, { hideIfNoWords }] );

console.log(truncateText(TEXT, 3));
// ...

console.log(truncateText(TEXT, 3, { hideIfNoWords: true }));
//

console.log(truncateText(TEXT, 6));
// Lorem...

console.log(truncateText(TEXT, 15));
// Lorem ipsum...

console.log(truncateText(TEXT, 50));
// Lorem ipsum dolor sit amet.

getPositionOfLastSpaceBeforeIndex

getPositionOfLastSpaceBeforeIndex(text, index);

console.log(getPositionOfLastSpaceBeforeIndex(TEXT, 3));
// -1

console.log(getPositionOfLastSpaceBeforeIndex(TEXT, 6));
// 5

console.log(getPositionOfLastSpaceBeforeIndex(TEXT, 15));
// 11

console.log(getPositionOfLastSpaceBeforeIndex(TEXT, 50));
// 21