lib: move format and formatWithOptions into internal/util/inspect.js · nodejs/node@b75af15 (original) (raw)

`@@ -22,7 +22,11 @@

`

22

22

`'use strict';

`

23

23

``

24

24

`const errors = require('internal/errors');

`

25

``

`-

const { inspect } = require('internal/util/inspect');

`

``

25

`+

const {

`

``

26

`+

format,

`

``

27

`+

formatWithOptions,

`

``

28

`+

inspect

`

``

29

`+

} = require('internal/util/inspect');

`

26

30

`const {

`

27

31

`ERR_FALSY_VALUE_REJECTION,

`

28

32

`ERR_INVALID_ARG_TYPE,

`

`@@ -46,137 +50,8 @@ function uncurryThis(func) {

`

46

50

`}

`

47

51

`const objectToString = uncurryThis(Object.prototype.toString);

`

48

52

``

49

``

`-

let CIRCULAR_ERROR_MESSAGE;

`

50

53

`let internalDeepEqual;

`

51

54

``

52

``

`-

function tryStringify(arg) {

`

53

``

`-

try {

`

54

``

`-

return JSON.stringify(arg);

`

55

``

`-

} catch (err) {

`

56

``

`-

// Populate the circular error message lazily

`

57

``

`-

if (!CIRCULAR_ERROR_MESSAGE) {

`

58

``

`-

try {

`

59

``

`-

const a = {}; a.a = a; JSON.stringify(a);

`

60

``

`-

} catch (err) {

`

61

``

`-

CIRCULAR_ERROR_MESSAGE = err.message;

`

62

``

`-

}

`

63

``

`-

}

`

64

``

`-

if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE)

`

65

``

`-

return '[Circular]';

`

66

``

`-

throw err;

`

67

``

`-

}

`

68

``

`-

}

`

69

``

-

70

``

`-

const emptyOptions = {};

`

71

``

`-

function format(...args) {

`

72

``

`-

return formatWithOptions(emptyOptions, ...args);

`

73

``

`-

}

`

74

``

-

75

``

`-

function formatWithOptions(inspectOptions, f) {

`

76

``

`-

let i, tempStr;

`

77

``

`-

if (typeof f !== 'string') {

`

78

``

`-

if (arguments.length === 1) return '';

`

79

``

`-

let res = '';

`

80

``

`-

for (i = 1; i < arguments.length - 1; i++) {

`

81

``

`-

res += inspect(arguments[i], inspectOptions);

`

82

``

`-

res += ' ';

`

83

``

`-

}

`

84

``

`-

res += inspect(arguments[i], inspectOptions);

`

85

``

`-

return res;

`

86

``

`-

}

`

87

``

-

88

``

`-

if (arguments.length === 2) return f;

`

89

``

-

90

``

`-

let str = '';

`

91

``

`-

let a = 2;

`

92

``

`-

let lastPos = 0;

`

93

``

`-

for (i = 0; i < f.length - 1; i++) {

`

94

``

`-

if (f.charCodeAt(i) === 37) { // '%'

`

95

``

`-

const nextChar = f.charCodeAt(++i);

`

96

``

`-

if (a !== arguments.length) {

`

97

``

`-

switch (nextChar) {

`

98

``

`-

case 115: // 's'

`

99

``

`-

tempStr = String(arguments[a++]);

`

100

``

`-

break;

`

101

``

`-

case 106: // 'j'

`

102

``

`-

tempStr = tryStringify(arguments[a++]);

`

103

``

`-

break;

`

104

``

`-

case 100: // 'd'

`

105

``

`-

const tempNum = arguments[a++];

`

106

``

`-

// eslint-disable-next-line valid-typeof

`

107

``

`-

if (typeof tempNum === 'bigint') {

`

108

``

`` -

tempStr = ${tempNum}n;

``

109

``

`-

} else if (typeof tempNum === 'symbol') {

`

110

``

`-

tempStr = 'NaN';

`

111

``

`-

} else {

`

112

``

`` -

tempStr = ${Number(tempNum)};

``

113

``

`-

}

`

114

``

`-

break;

`

115

``

`-

case 79: // 'O'

`

116

``

`-

tempStr = inspect(arguments[a++], inspectOptions);

`

117

``

`-

break;

`

118

``

`-

case 111: // 'o'

`

119

``

`-

{

`

120

``

`-

const opts = {

`

121

``

`-

showHidden: true,

`

122

``

`-

showProxy: true,

`

123

``

`-

depth: 4,

`

124

``

`-

...inspectOptions

`

125

``

`-

};

`

126

``

`-

tempStr = inspect(arguments[a++], opts);

`

127

``

`-

break;

`

128

``

`-

}

`

129

``

`-

case 105: // 'i'

`

130

``

`-

const tempInteger = arguments[a++];

`

131

``

`-

// eslint-disable-next-line valid-typeof

`

132

``

`-

if (typeof tempInteger === 'bigint') {

`

133

``

`` -

tempStr = ${tempInteger}n;

``

134

``

`-

} else if (typeof tempInteger === 'symbol') {

`

135

``

`-

tempStr = 'NaN';

`

136

``

`-

} else {

`

137

``

`` -

tempStr = ${parseInt(tempInteger)};

``

138

``

`-

}

`

139

``

`-

break;

`

140

``

`-

case 102: // 'f'

`

141

``

`-

const tempFloat = arguments[a++];

`

142

``

`-

if (typeof tempFloat === 'symbol') {

`

143

``

`-

tempStr = 'NaN';

`

144

``

`-

} else {

`

145

``

`` -

tempStr = ${parseFloat(tempFloat)};

``

146

``

`-

}

`

147

``

`-

break;

`

148

``

`-

case 37: // '%'

`

149

``

`-

str += f.slice(lastPos, i);

`

150

``

`-

lastPos = i + 1;

`

151

``

`-

continue;

`

152

``

`-

default: // Any other character is not a correct placeholder

`

153

``

`-

continue;

`

154

``

`-

}

`

155

``

`-

if (lastPos !== i - 1)

`

156

``

`-

str += f.slice(lastPos, i - 1);

`

157

``

`-

str += tempStr;

`

158

``

`-

lastPos = i + 1;

`

159

``

`-

} else if (nextChar === 37) {

`

160

``

`-

str += f.slice(lastPos, i);

`

161

``

`-

lastPos = i + 1;

`

162

``

`-

}

`

163

``

`-

}

`

164

``

`-

}

`

165

``

`-

if (lastPos === 0)

`

166

``

`-

str = f;

`

167

``

`-

else if (lastPos < f.length)

`

168

``

`-

str += f.slice(lastPos);

`

169

``

`-

while (a < arguments.length) {

`

170

``

`-

const x = arguments[a++];

`

171

``

`-

if ((typeof x !== 'object' && typeof x !== 'symbol') || x === null) {

`

172

``

`` -

str += ${x};

``

173

``

`-

} else {

`

174

``

`` -

str += ${inspect(x, inspectOptions)};

``

175

``

`-

}

`

176

``

`-

}

`

177

``

`-

return str;

`

178

``

`-

}

`

179

``

-

180

55

`const debugs = {};

`

181

56

`let debugEnvRegex = /^$/;

`

182

57

`if (process.env.NODE_DEBUG) {

`