{ "source": "doc/api/util.markdown", "modules": [ { "textRaw": "util", "name": "util", "stability": 4, "stabilityText": "API Frozen", "desc": "
These functions are in the module 'util'
. Use require('util')
to access\nthem.\n\n\n
Returns a formatted string using the first argument as a printf
-like format.\n\n
The first argument is a string that contains zero or more placeholders.\nEach placeholder is replaced with the converted value from its corresponding\nargument. Supported placeholders are:\n\n
\n%s
- String.%d
- Number (both integer and float).%j
- JSON.%
- single percent sign ('%'
). This does not consume an argument.If the placeholder does not have a corresponding argument, the placeholder is\nnot replaced.\n\n
\nutil.format('%s:%s', 'foo'); // 'foo:%s'
\nIf there are more arguments than placeholders, the extra arguments are\nconverted to strings with util.inspect()
and these strings are concatenated,\ndelimited by a space.\n\n
util.format('%s:%s', 'foo', 'bar', 'baz'); // 'foo:bar baz'
\nIf the first argument is not a format string then util.format()
returns\na string that is the concatenation of all its arguments separated by spaces.\nEach argument is converted to a string with util.inspect()
.\n\n
util.format(1, 2, 3); // '1 2 3'
\n",
"signatures": [
{
"params": [
{
"name": "format"
},
{
"name": "...",
"optional": true
}
]
}
]
},
{
"textRaw": "util.debug(string)",
"type": "method",
"name": "debug",
"desc": "A synchronous output function. Will block the process and\noutput string
immediately to stderr
.\n\n
require('util').debug('message on stderr');
\n",
"signatures": [
{
"params": [
{
"name": "string"
}
]
}
]
},
{
"textRaw": "util.error([...])",
"type": "method",
"name": "error",
"desc": "Same as util.debug()
except this will output all arguments immediately to\nstderr
.\n\n
A synchronous output function. Will block the process and output all arguments\nto stdout
with newlines after each argument.\n\n
A synchronous output function. Will block the process, cast each argument to a\nstring then output to stdout
. Does not place newlines after each argument.\n\n
Output with timestamp on stdout
.\n\n
require('util').log('Timestamped message.');
\n",
"signatures": [
{
"params": [
{
"name": "string"
}
]
}
]
},
{
"textRaw": "util.inspect(object, [options])",
"type": "method",
"name": "inspect",
"desc": "Return a string representation of object
, which is useful for debugging.\n\n
An optional options object may be passed that alters certain aspects of the\nformatted string:\n\n
\nshowHidden
- if true
then the object's non-enumerable properties will be\nshown too. Defaults to false
.
depth
- tells inspect
how many times to recurse while formatting the\nobject. This is useful for inspecting large complicated objects. Defaults to\n2
. To make it recurse indefinitely pass null
.
colors
- if true
, then the output will be styled with ANSI color codes.\nDefaults to false
. Colors are customizable, see below.
customInspect
- if false
, then custom inspect()
functions defined on the\nobjects being inspected won't be called. Defaults to true
.
Example of inspecting all properties of the util
object:\n\n
var util = require('util');\n\nconsole.log(util.inspect(util, { showHidden: true, depth: null }));
\n",
"modules": [
{
"textRaw": "Customizing `util.inspect` colors",
"name": "customizing_`util.inspect`_colors",
"desc": "Color output (if enabled) of util.inspect
is customizable globally\nvia util.inspect.styles
and util.inspect.colors
objects.\n\n
util.inspect.styles
is a map assigning each style a color\nfrom util.inspect.colors
.\nHighlighted styles and their default values are:\n number
(yellow)\n boolean
(yellow)\n string
(green)\n date
(magenta)\n regexp
(red)\n null
(bold)\n undefined
(grey)\n special
- only function at this time (cyan)\n * name
(intentionally no styling)\n\n
Predefined color codes are: white
, grey
, black
, blue
, cyan
, \ngreen
, magenta
, red
and yellow
.\nThere are also bold
, italic
, underline
and inverse
codes.\n\n
Objects also may define their own inspect(depth)
function which util.inspect()
\nwill invoke and use the result of when inspecting the object:\n\n
var util = require('util');\n\nvar obj = { name: 'nate' };\nobj.inspect = function(depth) {\n return '{' + this.name + '}';\n};\n\nutil.inspect(obj);\n // "{nate}"
\n",
"type": "module",
"displayName": "Customizing `util.inspect` colors"
}
],
"signatures": [
{
"params": [
{
"name": "object"
},
{
"name": "options",
"optional": true
}
]
}
]
},
{
"textRaw": "util.isArray(object)",
"type": "method",
"name": "isArray",
"desc": "Returns true
if the given "object" is an Array
. false
otherwise.\n\n
var util = require('util');\n\nutil.isArray([])\n // true\nutil.isArray(new Array)\n // true\nutil.isArray({})\n // false
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isRegExp(object)",
"type": "method",
"name": "isRegExp",
"desc": "Returns true
if the given "object" is a RegExp
. false
otherwise.\n\n
var util = require('util');\n\nutil.isRegExp(/some regexp/)\n // true\nutil.isRegExp(new RegExp('another regexp'))\n // true\nutil.isRegExp({})\n // false
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isDate(object)",
"type": "method",
"name": "isDate",
"desc": "Returns true
if the given "object" is a Date
. false
otherwise.\n\n
var util = require('util');\n\nutil.isDate(new Date())\n // true\nutil.isDate(Date())\n // false (without 'new' returns a String)\nutil.isDate({})\n // false
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isError(object)",
"type": "method",
"name": "isError",
"desc": "Returns true
if the given "object" is an Error
. false
otherwise.\n\n
var util = require('util');\n\nutil.isError(new Error())\n // true\nutil.isError(new TypeError())\n // true\nutil.isError({ name: 'Error', message: 'an error occurred' })\n // false
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.pump(readableStream, writableStream, [callback])",
"type": "method",
"name": "pump",
"stability": 0,
"stabilityText": "Deprecated: Use readableStream.pipe(writableStream)",
"desc": "Read the data from readableStream
and send it to the writableStream
.\nWhen writableStream.write(data)
returns false
readableStream
will be\npaused until the drain
event occurs on the writableStream
. callback
gets\nan error as its only argument and is called when writableStream
is closed or\nwhen an error occurs.\n\n\n
Inherit the prototype methods from one\nconstructor\ninto another. The prototype of constructor
will be set to a new\nobject created from superConstructor
.\n\n
As an additional convenience, superConstructor
will be accessible\nthrough the constructor.super_
property.\n\n
var util = require("util");\nvar events = require("events");\n\nfunction MyStream() {\n events.EventEmitter.call(this);\n}\n\nutil.inherits(MyStream, events.EventEmitter);\n\nMyStream.prototype.write = function(data) {\n this.emit("data", data);\n}\n\nvar stream = new MyStream();\n\nconsole.log(stream instanceof events.EventEmitter); // true\nconsole.log(MyStream.super_ === events.EventEmitter); // true\n\nstream.on("data", function(data) {\n console.log('Received data: "' + data + '"');\n})\nstream.write("It works!"); // Received data: "It works!"
\n",
"signatures": [
{
"params": [
{
"name": "constructor"
},
{
"name": "superConstructor"
}
]
}
]
}
],
"type": "module",
"displayName": "util"
}
]
}