1.1.8 • Published 8 months ago

truncate-text-between-words v1.1.8

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

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

CI npm npm bundle size Ko-fi

Usage

import {
    truncateText,
    getPositionOfLastSpaceBeforeIndex,
} from 'truncate-text-between-words';

Let's use this text as example:

// Lenght:        5    10   15   20   25
//                ↓    ↓    ↓    ↓    ↓
const TEXT = 'Lorem ipsum dolor sit amet.';

truncateText

Parameters:

type TruncateTextOptions = {
    hideIfNoWords?: boolean;
};

truncateText(text: string, maxLength: number, options?: TruncateTextOptions);
truncateText(TEXT, 3);
// Output: ...

truncateText(TEXT, 3, { hideIfNoWords: true });
// Output:

truncateText(TEXT, 6);
// Output: Lorem...

truncateText(TEXT, 15);
// Output: Lorem ipsum...

truncateText(TEXT, 50);
// Output: Lorem ipsum dolor sit amet.

getPositionOfLastSpaceBeforeIndex

getPositionOfLastSpaceBeforeIndex(text: string, index: number);
getPositionOfLastSpaceBeforeIndex(TEXT, 3);
// Output: -1

getPositionOfLastSpaceBeforeIndex(TEXT, 6);
// Output: 5

getPositionOfLastSpaceBeforeIndex(TEXT, 15);
// Output: 11

getPositionOfLastSpaceBeforeIndex(TEXT, 50);
// Output: 21
1.1.8

8 months ago

1.1.7

8 months ago

1.1.6

8 months ago

1.1.5

2 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago