Strings.trunc()

If the string is longer than the given maximum length, chop off characters from the end.

By default, this machine will reduce the string to a maximum of 30 characters, including a 3-character elipses ("...") added to the end. An additional default behavior of this machine (which can be disabled by disabling the `pretty` option) is to omit trailing spaces and punctuation, and also avoid chopping words in half.

Usage

Hover over inputs/exits for more info.

var Strings = require('machinepack-strings');


// If the string is longer than the given maximum length, chop off characters from the end.

Strings.trunc({

string: 'Christian van der Henst',

maxLength: 15,

omission: '...',

pretty: true,

}).exec({

// An unexpected error occurred.

error: function (err) {

 

},

// OK.

success: function (result) {

 

},

});

Inputs

string required

The string to truncate.

'Christian van der Henst'
maxLength

The maximum number of characters (including the "...")

15
omission

The substring to append to our string indicating characters were omitted.

'...'
pretty

Whether or not to attempt to make the truncated string look more natural.

true

Exits

error

An unexpected error occurred.

success

OK.

'Christian van...'