Skip to content

Trims text to a certain number of characters. You can define either to break word to maintain the number of characters limit or characters limit may decreases to avoid breaking words.

License

Notifications You must be signed in to change notification settings

samiahmedsiddiqui/trim-characters

Repository files navigation

trim-characters

NPM version Downloads Build Status AppVeyor Build Status Dependency Status

Trims text to a certain number of characters. You can define either to break-word to maintain the number of characters limit or characters limit may decrease to avoid breaking words.

Install

Via npm

npm install trim-characters

Via Yarn

yarn add trim-characters

Usage

With CommonJS

const trimCharacters = require('trim-characters');

With React

import trimCharacters from 'trim-characters';

Example

Trims text to a certain number of characters in Node.js

const trimCharacters = require('trim-characters');

const text = '<h1>What is Geostatistics?</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tincidunt quam ut ligula ullamcorper interdum. Nulla malesuada purus tristique justo tristique, id posuere purus tristique. Pellentesque non magna ut libero elementum interdum vel vitae ante. Sed porta auctor urna eget venenatis. Mauris nec convallis metus. Sed at dui elit. Donec rhoncus justo neque, finibus commodo dui posuere ut. Maecenas in mi enim. Quisque maximus enim nunc.</p> <p>Donec eu ultricies ipsum. Fusce eget pellentesque urna. Vestibulum lacinia laoreet mi nec posuere. Duis vel elit elementum, scelerisque eros a, sodales eros. Praesent hendrerit neque velit, nec pretium ipsum finibus facilisis. Proin ultricies sem in sapien consectetur dictum.</p>';

const trimmedChar1 = trimCharacters(text, 70, false, '...');
const trimmedChar2 = trimCharacters(text, 70, true, '...');

console.log('Trim Characters without breaking words: ', trimmedChar1);
console.log('Trim Characters with breaking words: ', trimmedChar2);

Trims text to a certain number of characters in React

import trimCharacters from 'trim-characters';

const text = '<h1>What is Geostatistics?</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tincidunt quam ut ligula ullamcorper interdum. Nulla malesuada purus tristique justo tristique, id posuere purus tristique. Pellentesque non magna ut libero elementum interdum vel vitae ante. Sed porta auctor urna eget venenatis. Mauris nec convallis metus. Sed at dui elit. Donec rhoncus justo neque, finibus commodo dui posuere ut. Maecenas in mi enim. Quisque maximus enim nunc.</p> <p>Donec eu ultricies ipsum. Fusce eget pellentesque urna. Vestibulum lacinia laoreet mi nec posuere. Duis vel elit elementum, scelerisque eros a, sodales eros. Praesent hendrerit neque velit, nec pretium ipsum finibus facilisis. Proin ultricies sem in sapien consectetur dictum.</p>';

const trimmedChar1 = trimCharacters(text, 70, false, '...');
const trimmedChar2 = trimCharacters(text, 70, true, '...');

console.log('Trim Characters without breaking words: ', trimmedChar1);
console.log('Trim Characters with breaking words: ', trimmedChar2);

Parameters

Attributes Type Required Default Description
text String Yes Text to trim.
charLength Integer No 155 Number of characters.
breakWord Boolean No true Either to break words or not.

If it is set to false then charLength may decrease to AVOID breaking words.
more String No &hellip; What to append if the text needs to be trimmed.

Return

Trimmed text.

Tested

This package is tested with the Node.js and React Application.

About

Trims text to a certain number of characters. You can define either to break word to maintain the number of characters limit or characters limit may decreases to avoid breaking words.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published