2010-10-28 14:18:16 +02:00
|
|
|
## util
|
|
|
|
|
|
|
|
These functions are in the module `'util'`. Use `require('util')` to access
|
|
|
|
them.
|
|
|
|
|
|
|
|
|
2011-08-05 16:37:16 +02:00
|
|
|
### util.format()
|
|
|
|
|
|
|
|
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).
|
|
|
|
* `%j` - JSON.
|
|
|
|
* `%%` - 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
|
|
|
|
2011-08-07 08:55:44 +02:00
|
|
|
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
|
|
|
|
converted to strings with `util.inspect()` and these strings are concatenated,
|
|
|
|
delimited by a space.
|
|
|
|
|
|
|
|
util.format('%s:%s', 'foo', 'bar', 'baz'); // 'foo:bar baz'
|
|
|
|
|
|
|
|
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()`.
|
|
|
|
|
|
|
|
util.format(1, 2, 3); // '1 2 3'
|
|
|
|
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
### util.debug(string)
|
|
|
|
|
|
|
|
A synchronous output function. Will block the process and
|
|
|
|
output `string` immediately to `stderr`.
|
|
|
|
|
|
|
|
require('util').debug('message on stderr');
|
|
|
|
|
|
|
|
|
|
|
|
### util.log(string)
|
|
|
|
|
|
|
|
Output with timestamp on `stdout`.
|
|
|
|
|
2011-06-03 13:19:06 +02:00
|
|
|
require('util').log('Timestamped message.');
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
### util.inspect(object, showHidden=false, depth=2)
|
|
|
|
|
|
|
|
Return a string representation of `object`, which is useful for debugging.
|
|
|
|
|
|
|
|
If `showHidden` is `true`, then the object's non-enumerable properties will be
|
|
|
|
shown too.
|
|
|
|
|
|
|
|
If `depth` is provided, it tells `inspect` how many times to recurse while
|
|
|
|
formatting the object. This is useful for inspecting large complicated objects.
|
|
|
|
|
|
|
|
The default is to only recurse twice. To make it recurse indefinitely, pass
|
|
|
|
in `null` for `depth`.
|
|
|
|
|
|
|
|
Example of inspecting all properties of the `util` object:
|
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
console.log(util.inspect(util, true, null));
|
|
|
|
|
|
|
|
|
2011-01-11 19:18:46 +01:00
|
|
|
### util.pump(readableStream, writableStream, [callback])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Experimental
|
|
|
|
|
|
|
|
Read the data from `readableStream` and send it to the `writableStream`.
|
2011-01-11 19:18:46 +01:00
|
|
|
When `writableStream.write(data)` returns `false` `readableStream` will be
|
2010-10-28 14:18:16 +02:00
|
|
|
paused until the `drain` event occurs on the `writableStream`. `callback` gets
|
|
|
|
an error as its only argument and is called when `writableStream` is closed or
|
|
|
|
when an error occurs.
|
2010-11-21 06:28:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
### util.inherits(constructor, superConstructor)
|
|
|
|
|
|
|
|
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 events = require("events");
|
|
|
|
|
|
|
|
function MyStream() {
|
|
|
|
events.EventEmitter.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(MyStream, events.EventEmitter);
|
|
|
|
|
|
|
|
MyStream.prototype.write = function(data) {
|
|
|
|
this.emit("data", data);
|
|
|
|
}
|
|
|
|
|
|
|
|
var stream = new MyStream();
|
|
|
|
|
|
|
|
console.log(stream instanceof events.EventEmitter); // true
|
|
|
|
console.log(MyStream.super_ === events.EventEmitter); // true
|
|
|
|
|
|
|
|
stream.on("data", function(data) {
|
|
|
|
console.log('Received data: "' + data + '"');
|
|
|
|
})
|
|
|
|
stream.write("It works!"); // Received data: "It works!"
|