heyujie
2021-06-07 8f8155aa4c83f7c2577ac123add550766b6a7ce3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"use strict";
 
var safeToString = require("./safe-to-string");
 
var reNewLine = /[\n\r\u2028\u2029]/g;
 
module.exports = function (value) {
    var string = safeToString(value);
    if (string === null) return "<Non-coercible to string value>";
    // Trim if too long
    if (string.length > 100) string = string.slice(0, 99) + "…";
    // Replace eventual new lines
    string = string.replace(reNewLine, function (char) {
        switch (char) {
            case "\n":
                return "\\n";
            case "\r":
                return "\\r";
            case "\u2028":
                return "\\u2028";
            case "\u2029":
                return "\\u2029";
            /* istanbul ignore next */
            default:
                throw new Error("Unexpected character");
        }
    });
    return string;
};