Home

function: truncate


truncate(string: string, options: Object): 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 "...".

Example(s):

truncate('hi-diddly-ho there, neighborino')
// => 'hi-diddly-ho there, neighbo...'

truncate('hi-diddly-ho there, neighborino', {
  'length': 24,
  'separator': ' '
})
// => 'hi-diddly-ho there,...'

truncate('hi-diddly-ho there, neighborino', {
  'length': 24,
  'separator': /,? +/
})
// => 'hi-diddly-ho there...'

truncate('hi-diddly-ho there, neighborino', {
  'omission': ' [...]'
})
// => 'hi-diddly-ho there, neig [...]'

Params:

Name Type Attribute Description
string The string to truncate.
options The options object.
options.length The maximum string length.
options.omission The string to indicate text is omitted.
options.separator The separator pattern to truncate to.

Returns:

string

  {
    "comment": "/**\r\n * Truncates `string` if it's longer than the given maximum string length.\r\n * The last characters of the truncated string are replaced with the omission\r\n * string which defaults to \"...\".\r\n *\r\n * @since 4.0.0\r\n * @category String\r\n * @param {string} [string=''] The string to truncate.\r\n * @param {Object} [options={}] The options object.\r\n * @param {number} [options.length=30] The maximum string length.\r\n * @param {string} [options.omission='...'] The string to indicate text is omitted.\r\n * @param {RegExp|string} [options.separator] The separator pattern to truncate to.\r\n * @returns {string} Returns the truncated string.\r\n * @see replace\r\n * @example\r\n *\r\n * truncate('hi-diddly-ho there, neighborino')\r\n * // => 'hi-diddly-ho there, neighbo...'\r\n *\r\n * truncate('hi-diddly-ho there, neighborino', {\r\n *   'length': 24,\r\n *   'separator': ' '\r\n * })\r\n * // => 'hi-diddly-ho there,...'\r\n *\r\n * truncate('hi-diddly-ho there, neighborino', {\r\n *   'length': 24,\r\n *   'separator': /,? +/\r\n * })\r\n * // => 'hi-diddly-ho there...'\r\n *\r\n * truncate('hi-diddly-ho there, neighborino', {\r\n *   'omission': ' [...]'\r\n * })\r\n * // => 'hi-diddly-ho there, neig [...]'\r\n */",
    "meta": {
        "range": [
            1741,
            3349
        ],
        "filename": "truncate.js",
        "lineno": 52,
        "path": "C:\\Users\\beaujeup\\projects\\jsdoc-template\\examples\\lodash\\lodash-repo",
        "code": {
            "id": "astnode100010749",
            "name": "truncate",
            "type": "FunctionDeclaration",
            "paramnames": [
                "string",
                "options"
            ]
        },
        "vars": {
            "separator": "truncate~separator",
            "length": "truncate~length",
            "omission": "truncate~omission",
            "strSymbols": "truncate~strSymbols",
            "strLength": "truncate~strLength",
            "end": "truncate~end",
            "result": "truncate~result",
            "match": "truncate~match",
            "newEnd": "truncate~newEnd",
            "substring": "truncate~substring",
            "separator.lastIndex": "truncate~separator.lastIndex",
            "index": "truncate~index"
        }
    },
    "description": "Truncates `string` if it's longer than the given maximum string length.\rThe last characters of the truncated string are replaced with the omission\rstring which defaults to \"...\".",
    "since": "4.0.0",
    "tags": [
        {
            "originalTitle": "category",
            "title": "category",
            "text": "String",
            "value": "String"
        }
    ],
    "params": [
        {
            "type": {
                "names": [
                    "string"
                ]
            },
            "optional": true,
            "defaultvalue": "''",
            "description": "The string to truncate.",
            "name": "string"
        },
        {
            "type": {
                "names": [
                    "Object"
                ]
            },
            "optional": true,
            "defaultvalue": "{}",
            "description": "The options object.",
            "name": "options"
        },
        {
            "type": {
                "names": [
                    "number"
                ]
            },
            "optional": true,
            "defaultvalue": 30,
            "description": "The maximum string length.",
            "name": "options.length"
        },
        {
            "type": {
                "names": [
                    "string"
                ]
            },
            "optional": true,
            "defaultvalue": "'...'",
            "description": "The string to indicate text is omitted.",
            "name": "options.omission"
        },
        {
            "type": {
                "names": [
                    "RegExp",
                    "string"
                ]
            },
            "optional": true,
            "description": "The separator pattern to truncate to.",
            "name": "options.separator"
        }
    ],
    "returns": [
        {
            "type": {
                "names": [
                    "string"
                ]
            },
            "description": "Returns the truncated string."
        }
    ],
    "see": [
        "replace"
    ],
    "examples": [
        "truncate('hi-diddly-ho there, neighborino')\r// => 'hi-diddly-ho there, neighbo...'\r\rtruncate('hi-diddly-ho there, neighborino', {\r  'length': 24,\r  'separator': ' '\r})\r// => 'hi-diddly-ho there,...'\r\rtruncate('hi-diddly-ho there, neighborino', {\r  'length': 24,\r  'separator': /,? +/\r})\r// => 'hi-diddly-ho there...'\r\rtruncate('hi-diddly-ho there, neighborino', {\r  'omission': ' [...]'\r})\r// => 'hi-diddly-ho there, neig [...]'"
    ],
    "name": "truncate",
    "longname": "truncate",
    "kind": "function",
    "scope": "global",
    "___id": "T000002R000972",
    "___s": true,
    "filepath": "truncate.js"
}