MCPcopy Create free account
hub / github.com/nodejs/node / format_value

Function format_value

deps/v8/test/wasm-js/third_party/testharness.js:1039–1122  ·  view source on GitHub ↗
(val, seen)

Source from the content-addressed store, hash-verified

1037 * Convert a value to a nice, human-readable string
1038 */
1039 function format_value(val, seen)
1040 {
1041 if (!seen) {
1042 seen = [];
1043 }
1044 if (typeof val === "object" && val !== null) {
1045 if (seen.indexOf(val) >= 0) {
1046 return "[...]";
1047 }
1048 seen.push(val);
1049 }
1050 if (Array.isArray(val)) {
1051 let output = "[";
1052 if (val.beginEllipsis !== undefined) {
1053 output += "…, ";
1054 }
1055 output += val.map(function(x) {return format_value(x, seen);}).join(", ");
1056 if (val.endEllipsis !== undefined) {
1057 output += ", …";
1058 }
1059 return output + "]";
1060 }
1061
1062 switch (typeof val) {
1063 case "string":
1064 val = val.replace(/\\/g, "\\\\");
1065 for (var p in replacements) {
1066 var replace = "\\" + replacements[p];
1067 val = val.replace(RegExp(String.fromCharCode(p), "g"), replace);
1068 }
1069 return '"' + val.replace(/"/g, '\\"') + '"';
1070 case "boolean":
1071 case "undefined":
1072 return String(val);
1073 case "number":
1074 // In JavaScript, -0 === 0 and String(-0) == "0", so we have to
1075 // special-case.
1076 if (val === -0 && 1/val === -Infinity) {
1077 return "-0";
1078 }
1079 return String(val);
1080 case "object":
1081 if (val === null) {
1082 return "null";
1083 }
1084
1085 // Special-case Node objects, since those come up a lot in my tests. I
1086 // ignore namespaces.
1087 if (is_node(val)) {
1088 switch (val.nodeType) {
1089 case Node.ELEMENT_NODE:
1090 var ret = "<" + val.localName;
1091 for (var i = 0; i < val.attributes.length; i++) {
1092 ret += " " + val.attributes[i].name + '="' + val.attributes[i].value + '"';
1093 }
1094 ret += ">" + val.innerHTML + "</" + val.localName + ">";
1095 return "Element node " + truncate(ret, 60);
1096 case Node.TEXT_NODE:

Callers 1

make_messageFunction · 0.70

Calls 7

StringClass · 0.85
is_nodeFunction · 0.70
truncateFunction · 0.70
mapMethod · 0.65
indexOfMethod · 0.45
pushMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected