2012-02-27 20:09:35 +01:00
|
|
|
# util
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-02-25 01:15:26 +01:00
|
|
|
Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2013-05-22 00:22:05 +02:00
|
|
|
These functions are in the module `'util'`. Use `require('util')` to
|
|
|
|
access them.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-07 20:20:50 +01:00
|
|
|
The `util` module is primarily designed to support the needs of Node.js's
|
2013-05-22 00:22:05 +02:00
|
|
|
internal APIs. Many of these utilities are useful for your own
|
|
|
|
programs. If you find that these functions are lacking for your
|
|
|
|
purposes, however, you are encouraged to write your own utilities. We
|
|
|
|
are not interested in any future additions to the `util` module that
|
2015-12-07 20:20:50 +01:00
|
|
|
are unnecessary for Node.js's internal functionality.
|
2013-05-22 00:22:05 +02:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
## util.debug(string)
|
|
|
|
|
2016-01-27 14:49:05 +01:00
|
|
|
Stability: 0 - Deprecated: Use [`console.error()`][] instead.
|
2015-11-05 16:28:34 +01:00
|
|
|
|
|
|
|
Deprecated predecessor of `console.error`.
|
|
|
|
|
2013-05-22 00:22:05 +02:00
|
|
|
## util.debuglog(section)
|
|
|
|
|
|
|
|
* `section` {String} The section of the program to be debugged
|
|
|
|
* Returns: {Function} The logging function
|
|
|
|
|
|
|
|
This is used to create a function which conditionally writes to stderr
|
|
|
|
based on the existence of a `NODE_DEBUG` environment variable. If the
|
|
|
|
`section` name appears in that environment variable, then the returned
|
|
|
|
function will be similar to `console.error()`. If not, then the
|
|
|
|
returned function is a no-op.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2013-05-22 00:22:05 +02:00
|
|
|
var debuglog = util.debuglog('foo');
|
|
|
|
|
|
|
|
var bar = 123;
|
|
|
|
debuglog('hello from foo [%d]', bar);
|
|
|
|
```
|
|
|
|
|
|
|
|
If this program is run with `NODE_DEBUG=foo` in the environment, then
|
|
|
|
it will output something like:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
|
|
|
FOO 3245: hello from foo [123]
|
|
|
|
```
|
2013-05-22 00:22:05 +02:00
|
|
|
|
|
|
|
where `3245` is the process id. If it is not run with that
|
|
|
|
environment variable set, then it will not print anything.
|
|
|
|
|
|
|
|
You may separate multiple `NODE_DEBUG` environment variables with a
|
|
|
|
comma. For example, `NODE_DEBUG=fs,net,tls`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
## util.deprecate(function, string)
|
|
|
|
|
|
|
|
Marks that a method should not be used any more.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2015-11-05 16:28:34 +01:00
|
|
|
|
2016-01-24 10:15:51 +01:00
|
|
|
exports.puts = util.deprecate(() => {
|
2016-01-17 18:39:07 +01:00
|
|
|
for (var i = 0, len = arguments.length; i < len; ++i) {
|
|
|
|
process.stdout.write(arguments[i] + '\n');
|
|
|
|
}
|
|
|
|
}, 'util.puts: Use console.log instead');
|
|
|
|
```
|
2015-11-05 16:28:34 +01:00
|
|
|
|
|
|
|
It returns a modified function which warns once by default.
|
|
|
|
|
|
|
|
If `--no-deprecation` is set then this function is a NO-OP. Configurable
|
|
|
|
at run-time through the `process.noDeprecation` boolean (only effective
|
|
|
|
when set before a module is loaded.)
|
|
|
|
|
|
|
|
If `--trace-deprecation` is set, a warning and a stack trace are logged
|
|
|
|
to the console the first time the deprecated API is used. Configurable
|
|
|
|
at run-time through the `process.traceDeprecation` boolean.
|
|
|
|
|
|
|
|
If `--throw-deprecation` is set then the application throws an exception
|
|
|
|
when the deprecated API is used. Configurable at run-time through the
|
|
|
|
`process.throwDeprecation` boolean.
|
|
|
|
|
|
|
|
`process.throwDeprecation` takes precedence over `process.traceDeprecation`.
|
|
|
|
|
|
|
|
## util.error([...])
|
|
|
|
|
2016-01-27 14:49:05 +01:00
|
|
|
Stability: 0 - Deprecated: Use [`console.error()`][] instead.
|
2015-11-05 16:28:34 +01:00
|
|
|
|
|
|
|
Deprecated predecessor of `console.error`.
|
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## util.format(format[, ...])
|
2011-08-05 16:37:16 +02:00
|
|
|
|
|
|
|
Returns a formatted string using the first argument as a `printf`-like format.
|
|
|
|
|
|
|
|
The first argument is a string that contains zero or more *placeholders*.
|
|
|
|
Each placeholder is replaced with the converted value from its corresponding
|
|
|
|
argument. Supported placeholders are:
|
|
|
|
|
|
|
|
* `%s` - String.
|
|
|
|
* `%d` - Number (both integer and float).
|
2013-07-29 21:00:33 +02:00
|
|
|
* `%j` - JSON. Replaced with the string `'[Circular]'` if the argument
|
2015-03-08 07:26:15 +01:00
|
|
|
contains circular references.
|
2011-08-05 16:37:16 +02:00
|
|
|
* `%%` - single percent sign (`'%'`). This does not consume an argument.
|
|
|
|
|
2011-08-07 08:55:44 +02:00
|
|
|
If the placeholder does not have a corresponding argument, the placeholder is
|
|
|
|
not replaced.
|
2011-08-05 16:37:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
util.format('%s:%s', 'foo'); // 'foo:%s'
|
|
|
|
```
|
2011-08-05 16:37:16 +02:00
|
|
|
|
|
|
|
If there are more arguments than placeholders, the extra arguments are
|
2015-03-25 06:44:08 +01:00
|
|
|
coerced to strings (for objects and symbols, `util.inspect()` is used)
|
|
|
|
and then concatenated, delimited by a space.
|
2011-08-05 16:37:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
util.format('%s:%s', 'foo', 'bar', 'baz'); // 'foo:bar baz'
|
|
|
|
```
|
2011-08-05 16:37:16 +02:00
|
|
|
|
|
|
|
If the first argument is not a format string then `util.format()` returns
|
|
|
|
a string that is the concatenation of all its arguments separated by spaces.
|
|
|
|
Each argument is converted to a string with `util.inspect()`.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
util.format(1, 2, 3); // '1 2 3'
|
|
|
|
```
|
2011-08-05 16:37:16 +02:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
## util.inherits(constructor, superConstructor)
|
2011-08-05 16:37:16 +02:00
|
|
|
|
2016-05-02 07:03:23 +02:00
|
|
|
_Note: usage of util.inherits() is discouraged. Please use the ES6 `class` and
|
|
|
|
`extends` keywords to get language level inheritance support. Also note that
|
|
|
|
the two styles are [semantically incompatible][]._
|
|
|
|
|
2015-11-14 04:21:49 +01:00
|
|
|
Inherit the prototype methods from one [constructor][] into another. The
|
|
|
|
prototype of `constructor` will be set to a new object created from
|
|
|
|
`superConstructor`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
As an additional convenience, `superConstructor` will be accessible
|
|
|
|
through the `constructor.super_` property.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
|
|
|
const EventEmitter = require('events');
|
2015-11-05 16:28:34 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
function MyStream() {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
}
|
2015-11-05 16:28:34 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
util.inherits(MyStream, EventEmitter);
|
2015-11-05 16:28:34 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
MyStream.prototype.write = function(data) {
|
|
|
|
this.emit('data', data);
|
|
|
|
}
|
2015-11-05 16:28:34 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
var stream = new MyStream();
|
2015-11-05 16:28:34 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log(stream instanceof EventEmitter); // true
|
|
|
|
console.log(MyStream.super_ === EventEmitter); // true
|
2015-11-05 16:28:34 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
stream.on('data', (data) => {
|
|
|
|
console.log(`Received data: "${data}"`);
|
|
|
|
})
|
|
|
|
stream.write('It works!'); // Received data: "It works!"
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## util.inspect(object[, options])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Return a string representation of `object`, which is useful for debugging.
|
|
|
|
|
2012-10-10 03:47:08 +02:00
|
|
|
An optional *options* object may be passed that alters certain aspects of the
|
|
|
|
formatted string:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-01-07 22:54:25 +01:00
|
|
|
- `showHidden` - if `true` then the object's non-enumerable and symbol
|
|
|
|
properties will be shown too. Defaults to `false`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-10-10 03:47:08 +02:00
|
|
|
- `depth` - tells `inspect` how many times to recurse while formatting the
|
|
|
|
object. This is useful for inspecting large complicated objects. Defaults to
|
|
|
|
`2`. To make it recurse indefinitely pass `null`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-10-10 03:47:08 +02:00
|
|
|
- `colors` - if `true`, then the output will be styled with ANSI color codes.
|
2016-01-05 11:49:54 +01:00
|
|
|
Defaults to `false`. Colors are customizable, see [Customizing
|
|
|
|
`util.inspect` colors][].
|
2011-12-08 02:15:40 +01:00
|
|
|
|
2013-08-22 03:38:23 +02:00
|
|
|
- `customInspect` - if `false`, then custom `inspect(depth, opts)` functions
|
|
|
|
defined on the objects being inspected won't be called. Defaults to `true`.
|
2012-10-10 22:52:56 +02:00
|
|
|
|
2016-04-29 10:06:48 +02:00
|
|
|
- `showProxy` - if `true`, then objects and functions that are Proxy objects
|
|
|
|
will be introspected to show their `target` and `hander` objects. Defaults to
|
|
|
|
`false`.
|
|
|
|
|
2016-04-21 19:29:24 +02:00
|
|
|
- `maxArrayLength` - specifies the maximum number of Array and TypedArray
|
|
|
|
elements to include when formatting. Defaults to `100`. Set to `null` to
|
|
|
|
show all array elements. Set to `0` or negative to show no array elements.
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
Example of inspecting all properties of the `util` object:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log(util.inspect(util, { showHidden: true, depth: null }));
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-08-22 03:38:23 +02:00
|
|
|
Values may supply their own custom `inspect(depth, opts)` functions, when
|
|
|
|
called they receive the current depth in the recursive inspection, as well as
|
|
|
|
the options object passed to `util.inspect()`.
|
|
|
|
|
2012-07-16 23:50:15 +02:00
|
|
|
### Customizing `util.inspect` colors
|
|
|
|
|
2013-05-22 00:22:05 +02:00
|
|
|
<!-- type=misc -->
|
|
|
|
|
2012-07-16 23:50:15 +02:00
|
|
|
Color output (if enabled) of `util.inspect` is customizable globally
|
|
|
|
via `util.inspect.styles` and `util.inspect.colors` objects.
|
|
|
|
|
|
|
|
`util.inspect.styles` is a map assigning each style a color
|
|
|
|
from `util.inspect.colors`.
|
|
|
|
Highlighted styles and their default values are:
|
|
|
|
* `number` (yellow)
|
|
|
|
* `boolean` (yellow)
|
|
|
|
* `string` (green)
|
|
|
|
* `date` (magenta)
|
|
|
|
* `regexp` (red)
|
|
|
|
* `null` (bold)
|
|
|
|
* `undefined` (grey)
|
|
|
|
* `special` - only function at this time (cyan)
|
|
|
|
* `name` (intentionally no styling)
|
|
|
|
|
2015-08-13 18:14:34 +02:00
|
|
|
Predefined color codes are: `white`, `grey`, `black`, `blue`, `cyan`,
|
2012-07-16 23:50:15 +02:00
|
|
|
`green`, `magenta`, `red` and `yellow`.
|
|
|
|
There are also `bold`, `italic`, `underline` and `inverse` codes.
|
|
|
|
|
2013-03-12 23:58:27 +01:00
|
|
|
### Custom `inspect()` function on Objects
|
2013-01-30 04:06:32 +01:00
|
|
|
|
2013-05-22 00:22:05 +02:00
|
|
|
<!-- type=misc -->
|
|
|
|
|
2012-10-06 01:48:13 +02:00
|
|
|
Objects also may define their own `inspect(depth)` function which `util.inspect()`
|
|
|
|
will invoke and use the result of when inspecting the object:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2012-10-06 01:48:13 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
var obj = { name: 'nate' };
|
|
|
|
obj.inspect = function(depth) {
|
|
|
|
return `{${this.name}}`;
|
|
|
|
};
|
2012-10-06 01:48:13 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
util.inspect(obj);
|
|
|
|
// "{nate}"
|
|
|
|
```
|
2012-10-06 01:48:13 +02:00
|
|
|
|
2013-01-30 04:06:32 +01:00
|
|
|
You may also return another Object entirely, and the returned String will be
|
|
|
|
formatted according to the returned Object. This is similar to how
|
|
|
|
`JSON.stringify()` works:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
var obj = { foo: 'this will not show up in the inspect() output' };
|
|
|
|
obj.inspect = function(depth) {
|
|
|
|
return { bar: 'baz' };
|
|
|
|
};
|
2013-01-30 04:06:32 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
util.inspect(obj);
|
|
|
|
// "{ bar: 'baz' }"
|
|
|
|
```
|
2013-01-30 04:06:32 +01:00
|
|
|
|
2012-02-27 20:09:35 +01:00
|
|
|
## util.isArray(object)
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2015-08-19 22:39:47 +02:00
|
|
|
Stability: 0 - Deprecated
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
Internal alias for [`Array.isArray`][].
|
2014-04-02 02:04:15 +02:00
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is an `Array`. Otherwise, returns `false`.
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
util.isArray([])
|
|
|
|
// true
|
|
|
|
util.isArray(new Array)
|
|
|
|
// true
|
|
|
|
util.isArray({})
|
|
|
|
// false
|
|
|
|
```
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
## util.isBoolean(object)
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2015-08-19 22:39:47 +02:00
|
|
|
Stability: 0 - Deprecated
|
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is a `Boolean`. Otherwise, returns `false`.
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
util.isBoolean(1)
|
|
|
|
// false
|
|
|
|
util.isBoolean(0)
|
|
|
|
// false
|
|
|
|
util.isBoolean(false)
|
|
|
|
// true
|
|
|
|
```
|
2015-12-07 20:20:50 +01:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
## util.isBuffer(object)
|
|
|
|
|
2016-01-27 14:49:05 +01:00
|
|
|
Stability: 0 - Deprecated: Use [`Buffer.isBuffer()`][] instead.
|
2015-11-05 16:28:34 +01:00
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is a `Buffer`. Otherwise, returns `false`.
|
2015-11-05 16:28:34 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2015-11-05 16:28:34 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
util.isBuffer({ length: 0 })
|
|
|
|
// false
|
|
|
|
util.isBuffer([])
|
|
|
|
// false
|
2016-04-25 04:36:57 +02:00
|
|
|
util.isBuffer(Buffer.from('hello world'))
|
2016-01-17 18:39:07 +01:00
|
|
|
// true
|
|
|
|
```
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2012-02-27 20:09:35 +01:00
|
|
|
## util.isDate(object)
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2015-08-19 22:39:47 +02:00
|
|
|
Stability: 0 - Deprecated
|
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is a `Date`. Otherwise, returns `false`.
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
util.isDate(new Date())
|
|
|
|
// true
|
|
|
|
util.isDate(Date())
|
|
|
|
// false (without 'new' returns a String)
|
|
|
|
util.isDate({})
|
|
|
|
// false
|
|
|
|
```
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2012-02-27 20:09:35 +01:00
|
|
|
## util.isError(object)
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2015-08-19 22:39:47 +02:00
|
|
|
Stability: 0 - Deprecated
|
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is an [`Error`][]. Otherwise, returns
|
|
|
|
`false`.
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
util.isError(new Error())
|
|
|
|
// true
|
|
|
|
util.isError(new TypeError())
|
|
|
|
// true
|
|
|
|
util.isError({ name: 'Error', message: 'an error occurred' })
|
|
|
|
// false
|
|
|
|
```
|
2011-10-26 20:10:23 +02:00
|
|
|
|
2016-02-24 18:00:23 +01:00
|
|
|
Note that this method relies on `Object.prototype.toString()` behavior. It is
|
|
|
|
possible to obtain an incorrect result when the `object` argument manipulates
|
|
|
|
`@@toStringTag`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const util = require('util');
|
|
|
|
const obj = { name: 'Error', message: 'an error occurred' };
|
|
|
|
|
|
|
|
util.isError(obj);
|
|
|
|
// false
|
|
|
|
obj[Symbol.toStringTag] = 'Error';
|
|
|
|
util.isError(obj);
|
|
|
|
// true
|
|
|
|
```
|
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
## util.isFunction(object)
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2015-08-19 22:39:47 +02:00
|
|
|
Stability: 0 - Deprecated
|
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is a `Function`. Otherwise, returns
|
|
|
|
`false`.
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
function Foo() {}
|
|
|
|
var Bar = function() {};
|
2015-11-05 16:28:34 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
util.isFunction({})
|
|
|
|
// false
|
|
|
|
util.isFunction(Foo)
|
|
|
|
// true
|
|
|
|
util.isFunction(Bar)
|
|
|
|
// true
|
|
|
|
```
|
2015-01-14 02:19:26 +01:00
|
|
|
|
|
|
|
## util.isNull(object)
|
|
|
|
|
2015-08-19 22:39:47 +02:00
|
|
|
Stability: 0 - Deprecated
|
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is strictly `null`. Otherwise, returns
|
|
|
|
`false`.
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
util.isNull(0)
|
|
|
|
// false
|
|
|
|
util.isNull(undefined)
|
|
|
|
// false
|
|
|
|
util.isNull(null)
|
|
|
|
// true
|
|
|
|
```
|
2015-01-14 02:19:26 +01:00
|
|
|
|
|
|
|
## util.isNullOrUndefined(object)
|
|
|
|
|
2015-08-19 22:39:47 +02:00
|
|
|
Stability: 0 - Deprecated
|
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is `null` or `undefined`. Otherwise,
|
|
|
|
returns `false`.
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
util.isNullOrUndefined(0)
|
|
|
|
// false
|
|
|
|
util.isNullOrUndefined(undefined)
|
|
|
|
// true
|
|
|
|
util.isNullOrUndefined(null)
|
|
|
|
// true
|
|
|
|
```
|
2015-01-14 02:19:26 +01:00
|
|
|
|
|
|
|
## util.isNumber(object)
|
|
|
|
|
2015-08-19 22:39:47 +02:00
|
|
|
Stability: 0 - Deprecated
|
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is a `Number`. Otherwise, returns `false`.
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
|
|
|
|
|
|
|
util.isNumber(false)
|
|
|
|
// false
|
|
|
|
util.isNumber(Infinity)
|
|
|
|
// true
|
|
|
|
util.isNumber(0)
|
|
|
|
// true
|
|
|
|
util.isNumber(NaN)
|
|
|
|
// true
|
|
|
|
```
|
2015-01-14 02:19:26 +01:00
|
|
|
|
|
|
|
## util.isObject(object)
|
|
|
|
|
2015-08-19 22:39:47 +02:00
|
|
|
Stability: 0 - Deprecated
|
|
|
|
|
2015-03-30 20:34:24 +02:00
|
|
|
Returns `true` if the given "object" is strictly an `Object` __and__ not a
|
2016-02-24 00:05:26 +01:00
|
|
|
`Function`. Otherwise, returns `false`.
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
|
|
|
|
|
|
|
util.isObject(5)
|
|
|
|
// false
|
|
|
|
util.isObject(null)
|
|
|
|
// false
|
|
|
|
util.isObject({})
|
|
|
|
// true
|
|
|
|
util.isObject(function(){})
|
|
|
|
// false
|
|
|
|
```
|
2015-01-14 02:19:26 +01:00
|
|
|
|
|
|
|
## util.isPrimitive(object)
|
|
|
|
|
2015-08-19 22:39:47 +02:00
|
|
|
Stability: 0 - Deprecated
|
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is a primitive type. Otherwise, returns
|
|
|
|
`false`.
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
|
|
|
|
|
|
|
util.isPrimitive(5)
|
|
|
|
// true
|
|
|
|
util.isPrimitive('foo')
|
|
|
|
// true
|
|
|
|
util.isPrimitive(false)
|
|
|
|
// true
|
|
|
|
util.isPrimitive(null)
|
|
|
|
// true
|
|
|
|
util.isPrimitive(undefined)
|
|
|
|
// true
|
|
|
|
util.isPrimitive({})
|
|
|
|
// false
|
|
|
|
util.isPrimitive(function() {})
|
|
|
|
// false
|
|
|
|
util.isPrimitive(/^$/)
|
|
|
|
// false
|
|
|
|
util.isPrimitive(new Date())
|
|
|
|
// false
|
|
|
|
```
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
## util.isRegExp(object)
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2015-08-19 22:39:47 +02:00
|
|
|
Stability: 0 - Deprecated
|
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is a `RegExp`. Otherwise, returns `false`.
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
util.isRegExp(/some regexp/)
|
|
|
|
// true
|
|
|
|
util.isRegExp(new RegExp('another regexp'))
|
|
|
|
// true
|
|
|
|
util.isRegExp({})
|
|
|
|
// false
|
|
|
|
```
|
2015-01-14 02:19:26 +01:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
## util.isString(object)
|
2013-05-22 00:22:05 +02:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
Stability: 0 - Deprecated
|
2014-05-07 11:48:55 +02:00
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is a `String`. Otherwise, returns `false`.
|
2014-05-07 11:48:55 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
|
|
|
|
|
|
|
util.isString('')
|
|
|
|
// true
|
|
|
|
util.isString('foo')
|
|
|
|
// true
|
|
|
|
util.isString(String('foo'))
|
|
|
|
// true
|
|
|
|
util.isString(5)
|
|
|
|
// false
|
|
|
|
```
|
2014-12-21 02:06:19 +01:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
## util.isSymbol(object)
|
2014-12-21 02:06:19 +01:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
Stability: 0 - Deprecated
|
2014-12-21 02:06:19 +01:00
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is a `Symbol`. Otherwise, returns `false`.
|
2014-12-21 02:06:19 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2014-05-07 11:48:55 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
util.isSymbol(5)
|
|
|
|
// false
|
|
|
|
util.isSymbol('foo')
|
|
|
|
// false
|
|
|
|
util.isSymbol(Symbol('foo'))
|
|
|
|
// true
|
|
|
|
```
|
2013-05-22 00:22:05 +02:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
## util.isUndefined(object)
|
2013-05-22 00:22:05 +02:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
Stability: 0 - Deprecated
|
2013-05-22 00:22:05 +02:00
|
|
|
|
2016-02-24 00:05:26 +01:00
|
|
|
Returns `true` if the given "object" is `undefined`. Otherwise, returns `false`.
|
2013-05-22 00:22:05 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
2013-05-22 00:22:05 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
var foo;
|
|
|
|
util.isUndefined(5)
|
|
|
|
// false
|
|
|
|
util.isUndefined(foo)
|
|
|
|
// true
|
|
|
|
util.isUndefined(null)
|
|
|
|
// false
|
|
|
|
```
|
2013-05-22 00:22:05 +02:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
## util.log(string)
|
2013-05-22 00:22:05 +02:00
|
|
|
|
2016-04-12 08:01:31 +02:00
|
|
|
Stability: 0 - Deprecated: Use a third party module instead.
|
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
Output with timestamp on `stdout`.
|
2013-05-22 00:22:05 +02:00
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
require('util').log('Timestamped message.');
|
2013-05-22 00:22:05 +02:00
|
|
|
|
|
|
|
## util.print([...])
|
|
|
|
|
2016-01-27 14:49:05 +01:00
|
|
|
Stability: 0 - Deprecated: Use [`console.log()`][] instead.
|
2013-05-22 00:22:05 +02:00
|
|
|
|
|
|
|
Deprecated predecessor of `console.log`.
|
|
|
|
|
2015-11-05 16:28:34 +01:00
|
|
|
## util.puts([...])
|
|
|
|
|
2016-01-27 14:49:05 +01:00
|
|
|
Stability: 0 - Deprecated: Use [`console.log()`][] instead.
|
2015-11-05 16:28:34 +01:00
|
|
|
|
|
|
|
Deprecated predecessor of `console.log`.
|
2015-11-14 04:21:49 +01:00
|
|
|
|
2016-01-27 14:05:43 +01:00
|
|
|
## util._extend(obj)
|
|
|
|
|
|
|
|
Stability: 0 - Deprecated: Use Object.assign() instead.
|
|
|
|
|
|
|
|
`_extend` was never intended to be used outside of internal NodeJS modules. The
|
|
|
|
community found and used it anyway.
|
|
|
|
|
|
|
|
It is deprecated and should not be used in new code. JavaScript comes with very
|
|
|
|
similar built-in functionality through `Object.assign`.
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
[`Array.isArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
|
2015-11-14 04:21:49 +01:00
|
|
|
[constructor]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/constructor
|
2016-05-02 07:03:23 +02:00
|
|
|
[semantically incompatible]: https://github.com/nodejs/node/issues/4179
|
2016-01-05 11:49:54 +01:00
|
|
|
[Customizing `util.inspect` colors]: #util_customizing_util_inspect_colors
|
|
|
|
[here]: #util_customizing_util_inspect_colors
|
2015-11-28 00:30:32 +01:00
|
|
|
[`Error`]: errors.html#errors_class_error
|
2016-01-27 14:49:05 +01:00
|
|
|
[`console.log()`]: console.html#console_console_log_data
|
|
|
|
[`console.error()`]: console.html#console_console_error_data
|
|
|
|
[`Buffer.isBuffer()`]: buffer.html#buffer_class_method_buffer_isbuffer_obj
|