@ -12,6 +12,12 @@ purposes, however, you are encouraged to write your own utilities. We
are not interested in any future additions to the `util` module that
are not interested in any future additions to the `util` module that
are unnecessary for node.js's internal functionality.
are unnecessary for node.js's internal functionality.
## util.debug(string)
Stability: 0 - Deprecated: use console.error() instead.
Deprecated predecessor of `console.error` .
## util.debuglog(section)
## util.debuglog(section)
* `section` {String} The section of the program to be debugged
* `section` {String} The section of the program to be debugged
@ -43,6 +49,40 @@ environment variable set, then it will not print anything.
You may separate multiple `NODE_DEBUG` environment variables with a
You may separate multiple `NODE_DEBUG` environment variables with a
comma. For example, `NODE_DEBUG=fs,net,tls` .
comma. For example, `NODE_DEBUG=fs,net,tls` .
## util.deprecate(function, string)
Marks that a method should not be used any more.
var util = require('util');
exports.puts = util.deprecate(function() {
for (var i = 0, len = arguments.length; i < len ; + + i ) {
process.stdout.write(arguments[i] + '\n');
}
}, 'util.puts: Use console.log instead');
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([...])
Stability: 0 - Deprecated: Use console.error() instead.
Deprecated predecessor of `console.error` .
## util.format(format[, ...])
## util.format(format[, ...])
Returns a formatted string using the first argument as a `printf` -like format.
Returns a formatted string using the first argument as a `printf` -like format.
@ -74,12 +114,38 @@ Each argument is converted to a string with `util.inspect()`.
util.format(1, 2, 3); // '1 2 3'
util.format(1, 2, 3); // '1 2 3'
## util.inherits(constructor, superConstructor)
## util.log(string)
Inherit the prototype methods from one
[constructor ](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/constructor )
into another. The prototype of `constructor` will be set to a new
object created from `superConstructor` .
Output with timestamp on `stdout` .
As an additional convenience, `superConstructor` will be accessible
through the `constructor.super_` property.
require('util').log('Timestamped message.');
var util = require("util");
var EventEmitter = require("events");
function MyStream() {
EventEmitter.call(this);
}
util.inherits(MyStream, EventEmitter);
MyStream.prototype.write = function(data) {
this.emit("data", data);
}
var stream = new MyStream();
console.log(stream instanceof EventEmitter); // true
console.log(MyStream.super_ === EventEmitter); // true
stream.on("data", function(data) {
console.log('Received data: "' + data + '"');
})
stream.write("It works!"); // Received data: "It works!"
## util.inspect(object[, options])
## util.inspect(object[, options])
@ -164,7 +230,6 @@ formatted according to the returned Object. This is similar to how
util.inspect(obj);
util.inspect(obj);
// "{ bar: 'baz' }"
// "{ bar: 'baz' }"
## util.isArray(object)
## util.isArray(object)
Stability: 0 - Deprecated
Stability: 0 - Deprecated
@ -182,20 +247,37 @@ Returns `true` if the given "object" is an `Array`. `false` otherwise.
util.isArray({})
util.isArray({})
// false
// false
## util.isRegExp (object)
## util.isBoolean (object)
Stability: 0 - Deprecated
Stability: 0 - Deprecated
Returns `true` if the given "object" is a `RegExp ` . `false` otherwise.
Returns `true` if the given "object" is a `Boolean ` . `false` otherwise.
var util = require('util');
var util = require('util');
util.isRegExp(/some regexp/)
util.isBoolean(1)
// true
// false
util.isRegExp(new RegExp('another regexp'))
util.isBoolean(0)
// false
util.isBoolean(false)
// true
// true
util.isRegExp({})
## util.isBuffer(object)
Stability: 0 - Deprecated
Use `Buffer.isBuffer()` instead.
Returns `true` if the given "object" is a `Buffer` . `false` otherwise.
var util = require('util');
util.isBuffer({ length: 0 })
// false
// false
util.isBuffer([])
// false
util.isBuffer(new Buffer('hello world'))
// true
## util.isDate(object)
## util.isDate(object)
@ -227,19 +309,22 @@ Returns `true` if the given "object" is an `Error`. `false` otherwise.
util.isError({ name: 'Error', message: 'an error occurred' })
util.isError({ name: 'Error', message: 'an error occurred' })
// false
// false
## util.isBoolea n(object)
## util.isFunctio n(object)
Stability: 0 - Deprecated
Stability: 0 - Deprecated
Returns `true` if the given "object" is a `Boolea n` . `false` otherwise.
Returns `true` if the given "object" is a `Functio n` . `false` otherwise.
var util = require('util');
var util = require('util');
util.isBoolean(1)
function Foo() {}
// false
var Bar = function() {};
util.isBoolean(0)
util.isFunction({})
// false
// false
util.isBoolean(false)
util.isFunction(Foo)
// true
util.isFunction(Bar)
// true
// true
## util.isNull(object)
## util.isNull(object)
@ -289,54 +374,6 @@ Returns `true` if the given "object" is a `Number`. `false` otherwise.
util.isNumber(NaN)
util.isNumber(NaN)
// true
// true
## util.isString(object)
Stability: 0 - Deprecated
Returns `true` if the given "object" is a `String` . `false` otherwise.
var util = require('util');
util.isString('')
// true
util.isString('foo')
// true
util.isString(String('foo'))
// true
util.isString(5)
// false
## util.isSymbol(object)
Stability: 0 - Deprecated
Returns `true` if the given "object" is a `Symbol` . `false` otherwise.
var util = require('util');
util.isSymbol(5)
// false
util.isSymbol('foo')
// false
util.isSymbol(Symbol('foo'))
// true
## util.isUndefined(object)
Stability: 0 - Deprecated
Returns `true` if the given "object" is `undefined` . `false` otherwise.
var util = require('util');
var foo;
util.isUndefined(5)
// false
util.isUndefined(foo)
// true
util.isUndefined(null)
// false
## util.isObject(object)
## util.isObject(object)
Stability: 0 - Deprecated
Stability: 0 - Deprecated
@ -355,24 +392,6 @@ Returns `true` if the given "object" is strictly an `Object` __and__ not a
util.isObject(function(){})
util.isObject(function(){})
// false
// false
## util.isFunction(object)
Stability: 0 - Deprecated
Returns `true` if the given "object" is a `Function` . `false` otherwise.
var util = require('util');
function Foo() {}
var Bar = function() {};
util.isFunction({})
// false
util.isFunction(Foo)
// true
util.isFunction(Bar)
// true
## util.isPrimitive(object)
## util.isPrimitive(object)
Stability: 0 - Deprecated
Stability: 0 - Deprecated
@ -400,102 +419,74 @@ Returns `true` if the given "object" is a primitive type. `false` otherwise.
util.isPrimitive(new Date())
util.isPrimitive(new Date())
// false
// false
## util.isBuffer (object)
## util.isRegExp (object)
Stability: 0 - Deprecated
Stability: 0 - Deprecated
Use `Buffer.isBuffer()` instead.
Returns `true` if the given "object" is a `RegExp` . `false` otherwise.
Returns `true` if the given "object" is a `Buffer` . `false` otherwise.
var util = require('util');
var util = require('util');
util.isBuffer({ length: 0 })
util.isRegExp(/some regexp/)
// false
util.isBuffer([])
// false
util.isBuffer(new Buffer('hello world'))
// true
// true
util.isRegExp(new RegExp('another regexp'))
// true
util.isRegExp({})
// false
## util.inherits(constructor, superConstructor)
## util.isString(object)
Inherit the prototype methods from one
[constructor ](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/constructor )
into another. The prototype of `constructor` will be set to a new
object created from `superConstructor` .
As an additional convenience, `superConstructor` will be accessible
through the `constructor.super_` property.
var util = require("util");
var EventEmitter = require("events");
function MyStream() {
EventEmitter.call(this);
}
util.inherits(MyStream, EventEmitter);
MyStream.prototype.write = function(data) {
this.emit("data", data);
}
var stream = new MyStream();
console.log(stream instanceof EventEmitter); // true
console.log(MyStream.super_ === EventEmitter); // true
stream.on("data", function(data) {
console.log('Received data: "' + data + '"');
})
stream.write("It works!"); // Received data: "It works!"
## util.deprecate(function, string)
Stability: 0 - Deprecated
Marks that a method should not be used any mor e.
Returns `true` if the given "object" is a `String` . `false` otherwise.
var util = require('util');
var util = require('util');
exports.puts = util.deprecate(function() {
util.isString('')
for (var i = 0, len = arguments.length; i < len ; + + i ) {
// true
process.stdout.write(arguments[i] + '\n');
util.isString('foo')
}
// true
}, 'util.puts: Use console.log instead');
util.isString(String('foo'))
// true
It returns a modified function which warns once by default.
util.isString(5)
// false
If `--no-deprecation` is set then this function is a NO-OP. Configurable
## util.isSymbol(object)
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
Stability: 0 - Deprecated
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
Returns `true` if the given "object" is a `Symbol` . `false` otherwise.
when the deprecated API is used. Configurable at run-time through the
`process.throwDeprecation` boolean.
`process.throwDeprecation` takes precedence over `process.traceDeprecation` .
var util = require('util');
## util.debug(string)
util.isSymbol(5)
// false
util.isSymbol('foo')
// false
util.isSymbol(Symbol('foo'))
// true
Stability: 0 - Deprecated: use console.error() instead.
## util.isUndefined(object)
Deprecated predecessor of `console.error` .
Stability: 0 - Deprecated
## util.error([...])
Returns `true` if the given "object" is `undefined` . `false` otherwise.
Stability: 0 - Deprecated: Use console.error() instead.
var util = require('util');
Deprecated predecessor of `console.error` .
var foo;
util.isUndefined(5)
// false
util.isUndefined(foo)
// true
util.isUndefined(null)
// false
## util.puts([...])
## util.log(string )
Stability: 0 - Deprecated: Use console.log() instead.
Output with timestamp on `stdout` .
Deprecated predecessor of `console.log` .
require('util').log('Timestamped message.');
## util.print([...])
## util.print([...])
@ -503,9 +494,14 @@ Deprecated predecessor of `console.log`.
Deprecated predecessor of `console.log` .
Deprecated predecessor of `console.log` .
## util.pump(readableStream, writableStream[, callback])
## util.pump(readableStream, writableStream[, callback])
Stability: 0 - Deprecated: Use readableStream.pipe(writableStream)
Stability: 0 - Deprecated: Use readableStream.pipe(writableStream)
Deprecated predecessor of `stream.pipe()` .
Deprecated predecessor of `stream.pipe()` .
## util.puts([...])
Stability: 0 - Deprecated: Use console.log() instead.
Deprecated predecessor of `console.log` .