str_shorten
v1.0.18
Published
Shorten string by character length with word boundary supported
Downloads
232
Maintainers
Readme
str_shorten
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"
License
Copyright (c) 2018 Binh Quan
Licensed under the MIT license.