node. js Api--util (Tools)

Source: Internet
Author: User

//Descriptionthe Node API version is v0.10.31.
Chinese Reference: Http://nodeapi.ucdok.com/#/api/,http://blog.sina.com.cn/oleoneoythis Keweibo main note. Directory Tools
0 Util.format (format, [...])
0 util.debug (string) 0 Util.error ([...]) 0 util.puts ([...]) 0 Util.print ([...]) 0 Util.log (string) 0 Util.inspect (object, [options]) customizing Util.inspect colors 0 Util.isarray (object) 0 Util.isregexp (object) 0 util.isdate (object) 0 Util.iserror (object) 0 util.pump (readablestream, Writablestream, [callback]) 0 Util.inherits (constructor, superconstructor) Tools
Stability: 4-api freeze
These functions are in the module ' util ' . Use require (' util ') to access them. Util.format (format, [...])returns a formatted string, with the first argument used as the format of the printf form. The first argument is a string that contains 0 or more placeholders . Each placeholder is replaced by a value that is converted by its corresponding argument. Supported placeholders:%s-String. %d-numeric value (including integers and floating-point numbers). %j-json. %-a single percent sign ('% '). This does not consume a parameter. if the placeholder does not have a corresponding parameter, it is not replaced.
1 // ' foo:%s '

If the argument is more than a placeholder, the extra arguments are converted to strings using util.inspect () , and the resulting string is concatenated using a space separator.

1 // ' Foo:bar Baz '

If the first argument is not a formatted string , then Util.format () returns a string that separates all parameters with spaces. Each parameter is converted to a string using util.inspect () .

1 // ' 1 2 3 '
Util.debug (String)a synchronous output function. The process is blocked and the stringis output to stderr immediately.
1 require (' util '). Debug (' Message on stderr ');
Util.error ([...])As with Util.debug () , except that it will immediately output all parameters to stderr. Util.puts ([...])a synchronous output function. Blocks the process and outputs all parameters to stdoutat the end of the line break. Util.print ([...])a synchronous output function. will block the process, concatenate all parameters into strings and output to stdout. No line break is added after each parameter. Util.log (String)output a timestamp to the stdout .
1 require (' util '). log (' timestamped message. ');
Util.inspect (object, [options])returns a String that represents an object , which is useful for debugging. You can pass an optional options object to change some aspects of the formatted string. ShowHidden -if true, the object's non-enumerable properties will also be displayed. The default is false. Depth -tells inspect how many times it is recursive when the object is formatted. This is useful when looking at very complex objects. The default is 2. You want it to pass nullin infinite recursion. Colors -if true, then the output will use the ANSI color code style. The default is false. Color can be customized, see below. custominspect -if false, then the custom inspect () function defined on the object being viewed will not be called. The default is true. See an example of all the properties of a util object:
1 var util = require (' util '); 2 true null}));
customizing Util.inspect ColorsThe output coloring to the util.inspect (if activated) is globally customizable, as long as the util.inspect.styles and util.inspect.colors objects are passed. util.inspect.styles is a map that assigns a color to each style in a util.iinspect.colors . The highlight styles and their default values are: numeric value (yellow), boolean (Yellow), string (green), date (magenta), Regular expression (red), Null(bold),undefined(gray), special case --this time only the function (cyan) * attribute name (intentionally without style)Predefined color codes: white , grey , black , blue , cyan , Green , magenta and yellow . There are also bold , italic , underline , and reversed-phase codes. objects can also define their own inspect (depth) function to let util.inspect () call and use it to return results when viewing an object.
 1  var  util = require (' Util ' ); 2  var  obj = {name: ' Nate ' };  4  obj.inspect =  (depth) { 5  return  ' {' + this . Name + '} ' ;  6  };  7   Util.inspect (obj);  9  " { Nate} " 
Util.isarray (object)returns trueif the given "object" is an array . Otherwise, falseis returned.
1 var util = require (' util '); 2 3 Util.isarray ([]) 4   // true 5 Util.isarray (new  Array)6   //  true7  Util.isarray ({})8   //  false
Util.isregexp (object)    If the given object is a returns the true . Otherwise, return false .
1 var util = require (' util '); 2 3 util.isregexp (/some regexp/)4   //  true5 util.isregexp (new RegExp (' another RegExp ')) 6   // true 7 util.isregexp ({}) 8   // false
Util.isdate (object)    If the given object is a date returns true . Otherwise, return false .
1 var util = require (' util '); 2 3 util.isdate (new  Date ())4   //  true5  Util.isdate (Date ())6   //  false (without ' new ' returns a String)7  util.isdate ({})8   //  false
Util.iserror (object)    If the given object is a error returns true . Otherwise, return false .
 1  var  util = require (' Util ' ); 2  new   Error ())  4   True  5  util.iserror (new   TypeError ())  6  //  true  7  util.iserror ({name: ' Error ', message: ' An error occurred ' })  8  //  
Util.pump (Readablestream, Writablestream, [callback])
Stability: 0-Objection: Use Readablestream.pipe (writablestream)
reads data from Readablestream and sends it to Writablestream. When writablestream.write (data) returns false,readablestream pauses until writablestream occurs Drain event. Callback has an error as its only parameter and is called when the Writablestream is closed or when an error occurs. Util.inherits (constructor, Superconstructor)inherit a method from a prototype to another constructor from one of the constructor functions. The constructor prototype will be set to a new object created from superconstructor . as an added convenience,superconstructor can be accessed through the constructor.super_ property.
1 varUtil = require ("Util");2 varEvents = require ("Events");3 4 functionMyStream () {5Events. Eventemitter.call ( This);6 }7 8 util.inherits (MyStream, events. Eventemitter);9 TenMyStream.prototype.write =function(data) { One    This. Emit ("Data", data); A } -  - varstream =NewMyStream (); the  -Console.log (stream instance of events. Eventemitter);//true -Console.log (Mystream.super_ = = = Events. Eventemitter);//true -  +Stream.on ("Data",function(data) { -Console.log (' reveived data: ' + data + ' "); + }) AStream.Write ("It works!");//Received data: "It works!"

node. js Api--util (Tools)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.