NodeJS - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Node.js Util Module

Node.js Util Module

shape Introduction

The Node.js Util Module provides utility functions which are used every time while developing applications that based on node. Following are the concepts covered.
  • Module
  • Util Module

Module

shape Description

The module in Node are completely written in JavaScript and compiled at runtime, by offering developers the ability to see the working of modules and assess various development practices and analyze the shown bugs. Because of the implementation of modules the node modules need not particularly perfect to each other. A few principles, for example, The Express and connect have set up a typical API to handle basic issues, so there are various modules that offer Express similarity, or utilize Express as a part of core functionalities.

Util Module

shape Description

The util modules are designed in order to support Node.js internal APIs and these mainly useful for application which make node.js programming very easy for a developer and also useful for debugging. The util module function can be accessed by using the following statement. [c]const util = require(‘util’);[/c]

util.format(format[, …args])

shape Description

A formatted string from one or more arguments can be created using the method util.format(format[, …args]) In which the first argument may contain a placeholder or zero, following are the list of placeholders used. The example explains creating a formatted string using arguments. First create a .js file with the code as shown below. format.js [c] var util = require('util'); var site_name = 'SPLessons Online Exams', question_level = 'Medium', total_questions = 20, exam_technologies= { tech1: 'JavaScript', tech2: 'Node.js'}; var format1 = util.format('The exam conducted in %s ',site_name); var format2 = util.format('The question level is %s,',question_level); var format3 = util.format('Total questions are %d,',total_questions); var format4 = util.format('Technologies coverd in the exam are %j',exam_technologies); console.log(format1); console.log(format2); console.log(format3); console.log(format4); [/c] Compiling the above code will produce the output as shown in the image below.

utill.inspect(object[, optional])

shape Description

The method have two arguments in which the first argument should be the variable for which the information is required and the second argument is the array of options and is similar to the function console.dir(), following are the optional options of utill.inspect() function. In order to obtaining the given variable information. First create a .js file with code below which list the built-in objects in the Node.js. Inspect.js [c] var util = require('util') console.log(util.inspect(console)); [/c] Compiling the above code will produce the output as shown in the image below.

util.inherits(constructor, superConstructor)

shape Description

One object constructor’s prototype can be inherited into another object constructor using the function. util.inherits(constructor, superConstructor) The function contains 2 arguments in which the first argument should be the prototype function which is to be inherited to the object given in the second argument. A new object which is created by the superConstructor is set to the constructor’s prototype and this superConstructor can be accessed by using the following property. construnctor.super_property The snippet code below demonstrate simple example for working of util.inherit() function, create a .js file with the code as shown below. inherit.js [c] 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); console.log(MyStream.super_ === events.EventEmitter); stream.on("data", function(data) { console.log('Status: "' + data + '"'); }) stream.write("Recived data, process completed successfully"); [/c] Compiling the above code will produce the output as shown in the image below.

util.isError(object)

shape Description

The given argument is verified by the function util.isError(object) and the value returns true if the argument is error and the value is false if the given argument is not an error, the functions allows only one argument. The snippet code below demonstrate simple example for working of util.isError() function, create a .js file with the code as shown below. error.js [c] var util = require('util'); console.log(util.isError(new Error())); console.log(util.isError(new TypeError())); console.log(util.isError({ name: 'SPLessons', message: 'Online Tutorilas' })); [/c] Compiling the above code will produce the output as shown in the image below.

util.log() and util.Date() Functions

shape Description

util.log(string) - A message along with the time for the executed function gets printed to the console utilizing the util.log(string) The function allows only one argument and that argument is printed to the console. The snippet code below demonstrate the simple example which prints the given argument to the console. log.js [c] var util = require('util'); util.log('Date and Time message.'); [/c] Compiling the above code will produce the output as shown in the image below. util.isDate(object) - The function allows only one argument and returns true value if the given argument is a date if not then the value would be false. The snippet code below demonstrate the simple example which prints the true or false for the given argument to the console. date.js [c] var util = require('util'); console.log(util.isDate(new Date())); console.log(util.isDate(Date())); console.log(util.isDate({})) [/c] Compiling the above code will produce the output as shown in the image below.

util.isArray() and util.isRegExp() Function

shape Description

util.isArray(object) - The function allows only one argument and returns true value if the given argument is an array if not then the value would be false. The snippet code below demonstrate the simple example which prints the true or false for the given argument to the console. array.js [c] var util = require('util'); console.log(util.isArray([])); console.log(util.isArray(new Array)); console.log(util.isArray({})) [/c] Compiling the above code will produce the output as shown in the image below. util.isRegExp(object) - The function allows only one argument and returns true value if the given argument is an RegExp if not then the value would be false. The snippet code below demonstrate the simple example which prints the true or false for the given argument to the console. regExp.js [c] var util = require('util'); console.log(util.isRegExp(/some regexp/)); console.log(util.isRegExp(new RegExp('New regexp'))); console.log(util.isRegExp({})) [/c] Compiling the above code will produce the output as shown in the image below.

util.print() and util.puts() Funtions

shape Description

util.print([...strings]) - Multiple arguments are converted to strings using the utill.print() function and then writes them out to stdout without adding a new line after each argument. The snippet code below demonstrate the simple example which prints the given argument without adding a new line for each argument to the console. print.js [c] var util = require("util"); util.print(1, 2, '3'); [/c] Compiling the above code will produce the output as shown in the image below. util.puts([...strings]) - Multiple arguments are converted to strings using the utill.puts() function and then writes them out to stderr by adding a new line after each argument. The snippet code below demonstrate the simple example which prints the given argument with adding a new line for each argument to the console. puts.js [c] var util = require("util"); util.puts("1", "2","3"); [/c] Compiling the above code will produce the output as shown in the image below.

Summary

shape Key Points

  • The Node.js Util modules make Node programming easy.
  • There are various util modules available in Node.js.
  • The Node.js util modules are mostly required for node’s internal API.
  • Node.js util modules are very frequently used in node applications.