shorten
The shorten()
function returns a shortened version of a string if its length is greater than the specified limit..
Parameters
Parameter | Type | Description |
---|---|---|
text | String | The text to shorten. |
len | Number | The maximum length of the shortened text, in chars. |
Returns
String
: The shortened text with ellipsis added to the end
Throws
Error
: If thetext
argument is not a string, or thelen
argument is not a positive integer
Example
const utils = require('utils-core.js');
const text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
const shortenedText = utils.format.shorten(text, 20);
console.log(shortenedText);
// output: "Lorem ipsum dolor si..."
info
if the passed text is shorter than the lengththe function will return the text with no modifications:
const utils = require('utils-core.js');
const text = "This is a short text.";
const shortenedText = utils.format.shorten(text, 50);
console.log(shortenedText);
// output: "This is a short text."