truncate

string

Truncates string if it's longer than the given maximum string length. The last characters of the truncated string are replaced with the omission string which defaults to "...".

Signature

truncate(str: string, options?: { length?: number; omission?: string }): string

Parameters

NameTypeDescription
strstringThe string to truncate
options?{ length?: number; omission?: string }The options object
options.length?numberThe maximum string lengthDefault: 30
options.omission?stringThe string to indicate text is omittedDefault: "..."

Returns

string - Returns the truncated string

Examples

Basic Usage

import { truncate } from 'dashlite'

const result = truncate('hi-diddly-ho there, neighborino')
console.log(result)

Output:

hi-diddly-ho there, neighbo...

Custom Length

import { truncate } from 'dashlite'

const result = truncate('hi-diddly-ho there, neighborino', { 
  length: 24 
})
console.log(result)

Output:

hi-diddly-ho there, n...

Custom Omission

import { truncate } from 'dashlite'

const result = truncate('hi-diddly-ho there, neighborino', { 
  length: 24,
  omission: ' [...]'
})
console.log(result)

Output:

hi-diddly-ho there [...]

Available since version 1.0.0