Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danielvigaru/truncate-text-between-words
https://github.com/danielvigaru/truncate-text-between-words
javascript node nodejs npm typescript
Last synced: 8 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/danielvigaru/truncate-text-between-words
- Owner: danielvigaru
- License: isc
- Created: 2022-07-10T09:26:01.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-09-16T16:18:35.000Z (2 months ago)
- Last Synced: 2024-09-16T19:53:10.343Z (2 months ago)
- Topics: javascript, node, nodejs, npm, typescript
- Language: TypeScript
- Homepage: https://npmjs.com/package/truncate-text-between-words
- Size: 81.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Truncates the text at the last space found before the given length. Adds "..." to indicate that the text is truncated.
[![CI](https://github.com/danielvigaru/truncate-text-between-words/actions/workflows/node.js.yml/badge.svg)](https://github.com/danielvigaru/truncate-text-between-words/actions/workflows/node.js.yml)
[![npm](https://img.shields.io/badge/npm-FFF?style=flat&logo=npm&logoColor=fff&color=CB3837)](https://www.npmjs.com/package/truncate-text-between-words)
[![npm bundle size](https://img.shields.io/bundlephobia/min/truncate-text-between-words)](https://bundlephobia.com/package/truncate-text-between-words)
[![Ko-fi](https://img.shields.io/badge/Ko--fi-FF5E5B?style=flat&logo=ko-fi&logoColor=white&color=D34F4C)](https://ko-fi.com/Y8Y1DZBZU)## Usage
```javascript
import {
truncateText,
getPositionOfLastSpaceBeforeIndex,
} from 'truncate-text-between-words';
```### Let's use this text as example:
```javascript
// Lenght: 5 10 15 20 25
// ↓ ↓ ↓ ↓ ↓
const TEXT = 'Lorem ipsum dolor sit amet.';
```## truncateText
Parameters:
```typescript
type TruncateTextOptions = {
hideIfNoWords?: boolean;
};truncateText(text: string, maxLength: number, options?: TruncateTextOptions);
``````javascript
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
```typescript
getPositionOfLastSpaceBeforeIndex(text: string, index: number);```
```javascript
getPositionOfLastSpaceBeforeIndex(TEXT, 3);
// Output: -1getPositionOfLastSpaceBeforeIndex(TEXT, 6);
// Output: 5getPositionOfLastSpaceBeforeIndex(TEXT, 15);
// Output: 11getPositionOfLastSpaceBeforeIndex(TEXT, 50);
// Output: 21
```