Skip to content

Shorten string by character length with word boundary supported

License

Notifications You must be signed in to change notification settings

binhqd/str_shorten

Repository files navigation

str_shorten

Build Status bitHound Score Coverage Status bitHound Code npm version Downloads

Shorten string by character length with word boundary supported

NPM

Install the module with:

npm install --save str_shorten

Yarn

Install the module with:

yarn add str_shorten

Usage

str_shorten(string, maxChars, options)

Options

name default required type description
wordBoundary true false boolean Indicate whether using word boundary or not.
endSymbols '...' false string Symbols indicate there is more text.

Example

Basic

var strShorten = require("str_shorten")
var str = "We will win if we want";
var shortenStr = strShorten(str, 11);

The return value will be "We will win..."

Customize end symbols

var strShorten = require("str_shorten")
var str = "We will win if we want";
var shortenStr = strShorten(str, 11, {endSymbols: ' -->'});

The return value will be "We will win -->"

Without end symbols

var strShorten = require("str_shorten")
var str = "We will win if we want";
var shortenStr = strShorten(str, 11, {endSymbols: ''});

The return value will be "We will win"

See Demo

License

Copyright (c) 2018 Binh Quan

Licensed under the MIT license.