github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/lib/1.0.0/util.js (about) 1 // Copyright Joyent, Inc. and other Node contributors. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a 4 // copy of this software and associated documentation files (the 5 // "Software"), to deal in the Software without restriction, including 6 // without limitation the rights to use, copy, modify, merge, publish, 7 // distribute, sublicense, and/or sell copies of the Software, and to permit 8 // persons to whom the Software is furnished to do so, subject to the 9 // following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included 12 // in all copies or substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22 var formatRegExp = /%[sdj%]/g; 23 exports.format = function (f) { 24 if (!isString(f)) { 25 var objects = []; 26 for (var i = 0; i < arguments.length; i++) { 27 objects.push(inspect(arguments[i])); 28 } 29 return objects.join(' '); 30 } 31 32 var i = 1; 33 var args = arguments; 34 var len = args.length; 35 var str = String(f).replace(formatRegExp, function (x) { 36 if (x === '%%') return '%'; 37 if (i >= len) return x; 38 switch (x) { 39 case '%s': 40 return String(args[i++]); 41 case '%d': 42 return Number(args[i++]); 43 case '%j': 44 try { 45 return JSON.stringify(args[i++]); 46 } catch (_) { 47 return '[Circular]'; 48 } 49 default: 50 return x; 51 } 52 }); 53 for (var x = args[i]; i < len; x = args[++i]) { 54 if (isNull(x) || !isObject(x)) { 55 str += ' ' + x; 56 } else { 57 str += ' ' + inspect(x); 58 } 59 } 60 return str; 61 }; 62 63 64 // Mark that a method should not be used. 65 // Returns a modified function which warns once by default. 66 // If --no-deprecation is set, then it is a no-op. 67 exports.deprecate = function (fn, msg) { 68 // Allow for deprecating things in the process of starting up. 69 if (isUndefined(global.process)) { 70 return function () { 71 return exports.deprecate(fn, msg).apply(this, arguments); 72 }; 73 } 74 75 if (process.noDeprecation === true) { 76 return fn; 77 } 78 79 var warned = false; 80 81 function deprecated() { 82 if (!warned) { 83 if (process.throwDeprecation) { 84 throw new Error(msg); 85 } else if (process.traceDeprecation) { 86 console.trace(msg); 87 } else { 88 console.error(msg); 89 } 90 warned = true; 91 } 92 return fn.apply(this, arguments); 93 } 94 95 return deprecated; 96 }; 97 98 99 var debugs = {}; 100 var debugEnviron; 101 exports.debuglog = function (set) { 102 if (isUndefined(debugEnviron)) 103 debugEnviron = process.env.NODE_DEBUG || ''; 104 set = set.toUpperCase(); 105 if (!debugs[set]) { 106 if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) { 107 var pid = process.pid; 108 debugs[set] = function () { 109 var msg = exports.format.apply(exports, arguments); 110 console.error('%s %d: %s', set, pid, msg); 111 }; 112 } else { 113 debugs[set] = function () {}; 114 } 115 } 116 return debugs[set]; 117 }; 118 119 120 /** 121 * Echos the value of a value. Trys to print the value out 122 * in the best way possible given the different types. 123 * 124 * @param {Object} obj The object to print out. 125 * @param {Object} opts Optional options object that alters the output. 126 */ 127 /* legacy: obj, showHidden, depth, colors*/ 128 function inspect(obj, opts) { 129 // default options 130 var ctx = { 131 seen: [], 132 stylize: stylizeNoColor 133 }; 134 // legacy... 135 if (arguments.length >= 3) ctx.depth = arguments[2]; 136 if (arguments.length >= 4) ctx.colors = arguments[3]; 137 if (isBoolean(opts)) { 138 // legacy... 139 ctx.showHidden = opts; 140 } else if (opts) { 141 // got an "options" object 142 exports._extend(ctx, opts); 143 } 144 // set default options 145 if (isUndefined(ctx.showHidden)) ctx.showHidden = false; 146 if (isUndefined(ctx.depth)) ctx.depth = 2; 147 if (isUndefined(ctx.colors)) ctx.colors = false; 148 if (isUndefined(ctx.customInspect)) ctx.customInspect = true; 149 if (ctx.colors) ctx.stylize = stylizeWithColor; 150 return formatValue(ctx, obj, ctx.depth); 151 } 152 exports.inspect = inspect; 153 154 155 // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics 156 inspect.colors = { 157 'bold': [1, 22], 158 'italic': [3, 23], 159 'underline': [4, 24], 160 'inverse': [7, 27], 161 'white': [37, 39], 162 'grey': [90, 39], 163 'black': [30, 39], 164 'blue': [34, 39], 165 'cyan': [36, 39], 166 'green': [32, 39], 167 'magenta': [35, 39], 168 'red': [31, 39], 169 'yellow': [33, 39] 170 }; 171 172 // Don't use 'blue' not visible on cmd.exe 173 inspect.styles = { 174 'special': 'cyan', 175 'number': 'yellow', 176 'boolean': 'yellow', 177 'undefined': 'grey', 178 'null': 'bold', 179 'string': 'green', 180 'date': 'magenta', 181 // "name": intentionally not styling 182 'regexp': 'red' 183 }; 184 185 186 function stylizeWithColor(str, styleType) { 187 var style = inspect.styles[styleType]; 188 189 if (style) { 190 return '\u001b[' + inspect.colors[style][0] + 'm' + str + 191 '\u001b[' + inspect.colors[style][1] + 'm'; 192 } else { 193 return str; 194 } 195 } 196 197 198 function stylizeNoColor(str, styleType) { 199 return str; 200 } 201 202 203 function arrayToHash(array) { 204 var hash = {}; 205 206 array.forEach(function (val, idx) { 207 hash[val] = true; 208 }); 209 210 return hash; 211 } 212 213 214 function formatValue(ctx, value, recurseTimes) { 215 // Provide a hook for user-specified inspect functions. 216 // Check that value is an object with an inspect function on it 217 if (ctx.customInspect && 218 value && 219 isFunction(value.inspect) && 220 // Filter out the util module, it's inspect function is special 221 value.inspect !== exports.inspect && 222 // Also filter out any prototype objects using the circular check. 223 !(value.constructor && value.constructor.prototype === value)) { 224 var ret = value.inspect(recurseTimes, ctx); 225 if (!isString(ret)) { 226 ret = formatValue(ctx, ret, recurseTimes); 227 } 228 return ret; 229 } 230 231 // Primitive types cannot have properties 232 var primitive = formatPrimitive(ctx, value); 233 if (primitive) { 234 return primitive; 235 } 236 237 // Look up the keys of the object. 238 var keys = Object.keys(value); 239 var visibleKeys = arrayToHash(keys); 240 241 if (ctx.showHidden) { 242 keys = Object.getOwnPropertyNames(value); 243 } 244 245 // IE doesn't make error fields non-enumerable 246 // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx 247 if (isError(value) && 248 (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) { 249 return formatError(value); 250 } 251 252 // Some type of object without properties can be shortcutted. 253 if (keys.length === 0) { 254 if (isFunction(value)) { 255 var name = value.name ? ': ' + value.name : ''; 256 return ctx.stylize('[Function' + name + ']', 'special'); 257 } 258 if (isRegExp(value)) { 259 return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); 260 } 261 if (isDate(value)) { 262 return ctx.stylize(Date.prototype.toString.call(value), 'date'); 263 } 264 if (isError(value)) { 265 return formatError(value); 266 } 267 } 268 269 var base = '', 270 array = false, 271 braces = ['{', '}']; 272 273 // Make Array say that they are Array 274 if (isArray(value)) { 275 array = true; 276 braces = ['[', ']']; 277 } 278 279 // Make functions say that they are functions 280 if (isFunction(value)) { 281 var n = value.name ? ': ' + value.name : ''; 282 base = ' [Function' + n + ']'; 283 } 284 285 // Make RegExps say that they are RegExps 286 if (isRegExp(value)) { 287 base = ' ' + RegExp.prototype.toString.call(value); 288 } 289 290 // Make dates with properties first say the date 291 if (isDate(value)) { 292 base = ' ' + Date.prototype.toUTCString.call(value); 293 } 294 295 // Make error with message first say the error 296 if (isError(value)) { 297 base = ' ' + formatError(value); 298 } 299 300 if (keys.length === 0 && (!array || value.length == 0)) { 301 return braces[0] + base + braces[1]; 302 } 303 304 if (recurseTimes < 0) { 305 if (isRegExp(value)) { 306 return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); 307 } else { 308 return ctx.stylize('[Object]', 'special'); 309 } 310 } 311 312 ctx.seen.push(value); 313 314 var output; 315 if (array) { 316 output = formatArray(ctx, value, recurseTimes, visibleKeys, keys); 317 } else { 318 output = keys.map(function (key) { 319 return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array); 320 }); 321 } 322 323 ctx.seen.pop(); 324 325 return reduceToSingleString(output, base, braces); 326 } 327 328 329 function formatPrimitive(ctx, value) { 330 if (isUndefined(value)) 331 return ctx.stylize('undefined', 'undefined'); 332 if (isString(value)) { 333 var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') 334 .replace(/'/g, "\\'") 335 .replace(/\\"/g, '"') + '\''; 336 return ctx.stylize(simple, 'string'); 337 } 338 if (isNumber(value)) 339 return ctx.stylize('' + value, 'number'); 340 if (isBoolean(value)) 341 return ctx.stylize('' + value, 'boolean'); 342 // For some reason typeof null is "object", so special case here. 343 if (isNull(value)) 344 return ctx.stylize('null', 'null'); 345 } 346 347 348 function formatError(value) { 349 return '[' + Error.prototype.toString.call(value) + ']'; 350 } 351 352 353 function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { 354 var output = []; 355 for (var i = 0, l = value.length; i < l; ++i) { 356 if (hasOwnProperty(value, String(i))) { 357 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, 358 String(i), true)); 359 } else { 360 output.push(''); 361 } 362 } 363 keys.forEach(function (key) { 364 if (!key.match(/^\d+$/)) { 365 output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, 366 key, true)); 367 } 368 }); 369 return output; 370 } 371 372 373 function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { 374 var name, str, desc; 375 desc = Object.getOwnPropertyDescriptor(value, key) || { 376 value: value[key] 377 }; 378 if (desc.get) { 379 if (desc.set) { 380 str = ctx.stylize('[Getter/Setter]', 'special'); 381 } else { 382 str = ctx.stylize('[Getter]', 'special'); 383 } 384 } else { 385 if (desc.set) { 386 str = ctx.stylize('[Setter]', 'special'); 387 } 388 } 389 if (!hasOwnProperty(visibleKeys, key)) { 390 name = '[' + key + ']'; 391 } 392 if (!str) { 393 if (ctx.seen.indexOf(desc.value) < 0) { 394 if (isNull(recurseTimes)) { 395 str = formatValue(ctx, desc.value, null); 396 } else { 397 str = formatValue(ctx, desc.value, recurseTimes - 1); 398 } 399 if (str.indexOf('\n') > -1) { 400 if (array) { 401 str = str.split('\n').map(function (line) { 402 return ' ' + line; 403 }).join('\n').substr(2); 404 } else { 405 str = '\n' + str.split('\n').map(function (line) { 406 return ' ' + line; 407 }).join('\n'); 408 } 409 } 410 } else { 411 str = ctx.stylize('[Circular]', 'special'); 412 } 413 } 414 if (isUndefined(name)) { 415 if (array && key.match(/^\d+$/)) { 416 return str; 417 } 418 name = JSON.stringify('' + key); 419 if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { 420 name = name.substr(1, name.length - 2); 421 name = ctx.stylize(name, 'name'); 422 } else { 423 name = name.replace(/'/g, "\\'") 424 .replace(/\\"/g, '"') 425 .replace(/(^"|"$)/g, "'"); 426 name = ctx.stylize(name, 'string'); 427 } 428 } 429 430 return name + ': ' + str; 431 } 432 433 434 function reduceToSingleString(output, base, braces) { 435 var numLinesEst = 0; 436 var length = output.reduce(function (prev, cur) { 437 numLinesEst++; 438 if (cur.indexOf('\n') >= 0) numLinesEst++; 439 return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1; 440 }, 0); 441 442 if (length > 60) { 443 return braces[0] + 444 (base === '' ? '' : base + '\n ') + 445 ' ' + 446 output.join(',\n ') + 447 ' ' + 448 braces[1]; 449 } 450 451 return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; 452 } 453 454 455 // NOTE: These type checking functions intentionally don't use `instanceof` 456 // because it is fragile and can be easily faked with `Object.create()`. 457 function isArray(ar) { 458 return Array.isArray(ar); 459 } 460 exports.isArray = isArray; 461 462 function isBoolean(arg) { 463 return typeof arg === 'boolean'; 464 } 465 exports.isBoolean = isBoolean; 466 467 function isNull(arg) { 468 return arg === null; 469 } 470 exports.isNull = isNull; 471 472 function isNullOrUndefined(arg) { 473 return arg == null; 474 } 475 exports.isNullOrUndefined = isNullOrUndefined; 476 477 function isNumber(arg) { 478 return typeof arg === 'number'; 479 } 480 exports.isNumber = isNumber; 481 482 function isString(arg) { 483 return typeof arg === 'string'; 484 } 485 exports.isString = isString; 486 487 function isSymbol(arg) { 488 return typeof arg === 'symbol'; 489 } 490 exports.isSymbol = isSymbol; 491 492 function isUndefined(arg) { 493 return arg === void 0; 494 } 495 exports.isUndefined = isUndefined; 496 497 function isRegExp(re) { 498 return isObject(re) && objectToString(re) === '[object RegExp]'; 499 } 500 exports.isRegExp = isRegExp; 501 502 function isObject(arg) { 503 return typeof arg === 'object' && arg !== null; 504 } 505 exports.isObject = isObject; 506 507 function isDate(d) { 508 return isObject(d) && objectToString(d) === '[object Date]'; 509 } 510 exports.isDate = isDate; 511 512 function isError(e) { 513 return isObject(e) && 514 (objectToString(e) === '[object Error]' || e instanceof Error); 515 } 516 exports.isError = isError; 517 518 function isFunction(arg) { 519 return typeof arg === 'function'; 520 } 521 exports.isFunction = isFunction; 522 523 function isPrimitive(arg) { 524 return arg === null || 525 typeof arg === 'boolean' || 526 typeof arg === 'number' || 527 typeof arg === 'string' || 528 typeof arg === 'symbol' || // ES6 symbol 529 typeof arg === 'undefined'; 530 } 531 exports.isPrimitive = isPrimitive; 532 533 exports.isBuffer = function (arg) { 534 return false; 535 }; 536 537 function objectToString(o) { 538 return Object.prototype.toString.call(o); 539 } 540 541 542 function pad(n) { 543 return n < 10 ? '0' + n.toString(10) : n.toString(10); 544 } 545 546 547 var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 548 'Oct', 'Nov', 'Dec' 549 ]; 550 551 // 26 Feb 16:19:34 552 function timestamp() { 553 var d = new Date(); 554 var time = [pad(d.getHours()), 555 pad(d.getMinutes()), 556 pad(d.getSeconds()) 557 ].join(':'); 558 return [d.getDate(), months[d.getMonth()], time].join(' '); 559 } 560 561 562 // log is just a thin wrapper to console.log that prepends a timestamp 563 exports.log = function () { 564 console.log(timestamp() + " - " + exports.format.apply(exports, arguments)); 565 }; 566 567 568 /** 569 * Inherit the prototype methods from one constructor into another. 570 * 571 * The Function.prototype.inherits from lang.js rewritten as a standalone 572 * function (not on Function.prototype). NOTE: If this file is to be loaded 573 * during bootstrapping this function needs to be rewritten using some native 574 * functions as prototype setup using normal JavaScript does not work as 575 * expected during bootstrapping (see mirror.js in r114903). 576 * 577 * @param {function} ctor Constructor function which needs to inherit the 578 * prototype. 579 * @param {function} superCtor Constructor function to inherit prototype from. 580 */ 581 exports.inherits = function (ctor, superCtor) { 582 ctor.super_ = superCtor 583 ctor.prototype = Object.create(superCtor.prototype, { 584 constructor: { 585 value: ctor, 586 enumerable: false, 587 writable: true, 588 configurable: true 589 } 590 }); 591 }; 592 593 exports._extend = function (origin, add) { 594 // Don't do anything if add isn't an object 595 if (!add || !isObject(add)) return origin; 596 597 var keys = Object.keys(add); 598 var i = keys.length; 599 while (i--) { 600 origin[keys[i]] = add[keys[i]]; 601 } 602 return origin; 603 }; 604 605 function hasOwnProperty(obj, prop) { 606 return Object.prototype.hasOwnProperty.call(obj, prop); 607 }