Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Node.js – util.formatWithOptions() method
The util.formatWithOptions() method works in the same way as the util.format() method. The only difference between the two is that the formatWithOptions() method takes an inspectOptions argument that specifies options that are passed along the util.inspect() method.
Syntax
util.formatWithOptions(inspectOptions, format, [args])
Parameters
The parameters are defined below:
- inspectOptions − These options will be used to inspect the object passed along this method.
- format − This parameter takes input for the format type of the input value passed.
Example 1
Create a file with the name "formatWithOptions.js" and copy the following code snippet. After creating the file, use the command "node formatWithOptions.js" to run this code.
// Node.js util.formatWithOptions() method
// Importing the util module
const util = require('util');
function fun() {
// Passing different options along the format
var val0 = util.formatWithOptions(
{ depth:0, colors: true },
'See object %O', { foo: 21 });
var val1 = util.formatWithOptions(
{ colors: true, showProxy : true },
'%s:%s:%s', 'a', 'b', 'c');
var val2 = util.formatWithOptions(
{ showHidden: true, colors: true },
'%s:%s', 'raj', 'rahul');
var val3 = util.formatWithOptions(
{ breakLength: 3, colors: true },
10, 20, 30);
var val5 = util.formatWithOptions(
{ compact: true }, '%% : %s', 567);
console.log(val0, '
', val1, '
',
val2, '
', val3, '
', val5);
}
// Function call
fun();
Output
C:\home\node>> node formatWithOptions.js
See object { foo: 21 }
a:b:c
raj:rahul
10 20 30
% : 567
Example 2
// Node.js util.formatWithOptions() method
// Importing the util module
const util = require('util');
console.log("1. ", util.formatWithOptions(
{ colors: true },
'%%: %s', 'John', 'Cena', -0));
console.log("2. ", util.formatWithOptions(
{
showHidden: false, depth: 0,
colors: true
}, '%s', 'fun', Object.create(null,
{ [Symbol.toStringTag]: { value: 'function' } })));
console.log("3. ", util.formatWithOptions(
{ colors: true }, '%o',
class Bar { }, 'fun', 78987965465464));
console.log("4. ", util.formatWithOptions(
{ depth: 0, colors: true }, '%o:%d',
class Foo { get [Symbol.toStringTag]()
{ return 'fun'; } }, 'fun',
78987965465464));
Output
C:\home\node>> node formatWithOptions.js
1. %: John Cena 0
2. fun [Object: null prototype] [function] {}
3. { [Function: Bar]
[length]: 0,
[prototype]: Bar { [constructor]: [Circular] },
[name]: 'Bar' } fun 78987965465464
4. { [Function: Foo]
[length]: 0,
[prototype]:
Foo [fun] {
[constructor]: [Circular],
[Symbol(Symbol.toStringTag)]: [Getter] },
[name]: 'Foo' }:NaN 78987965465464 Advertisements
