truncate

truncate is a utility to truncate a long string.

Import#

import { truncate } from '@dwarvesf/react-utils'

Return value#

This method returns a truncated string with a ... middle or ending.

Usage#

function Example() {
return (
<Stack>
<Box>{truncate('Lorem ipsum dolor sit amet', 20)}</Box>
<Box>{truncate('Lorem ipsum dolor sit amet', 10, true)}</Box>
</Stack>
)
}

Masking character#

Alternatively, you can use other character instead of ".", for example "*":

function Example() {
return (
<Stack>
<Box>{truncate('Lorem ipsum dolor sit amet', 10, true, "*")}</Box>
</Stack>
)
}

Parameters#

The truncate method accepts an list of following paramesters:

truncate(str, max, middle)
NameTypeDefaultDescription
strstring_The string to truncate.
maxnumber_The maximum string length.
middlebooleanfalseWhether to put ... in the middle or at the end of the truncated string.
maskCharstring"."What to use as the masking character when truncating.
Edit this page

Made with ❤️ by Dwarves Foundation