Starting from type judgment In JavaScript, variable type checking is a very troublesome thing. If you simply use Here are a few simple ones: console.log(typeof null) // 'object' console.log(typeof new Array) // 'object' console.log(typeof new String) // 'object' Later, everyone discovered that const getTypeString = obj => Object.prototype.toString.call(obj) getTypeString(null) // '[object Null]' getTypeString('string') //'[object String]' getTypeString(new String) //'[object String]' By proxying const getTypeString = obj => { return Object.prototype.toString.call(obj) } const isType = type => { return obj => { return getTypeString(obj) === `[object ${type}]` } } const isArray = isType('Array') // This method is generally replaced by Array.isArray const isNull = isType('Null') const isObject = isType('Object') const isRegExp = isType('RegExp') const isFunction = isType('Function') const isAsyncFunction = isType('AsyncFunction') isNull(null) // true isObject({}) // true isRegExp(/\w/) // true isFunction(() => {}) // true isAsyncFunction(async () => {}) // true But, in Node.js, there is actually a set of APIs for determining variable types. And it has extremely rich functions. In addition to the judgment of basic types, it also supports the judgment of Promise objects, Date objects, and various ArrayBuffers. const types = require('util/types') types.isDate(new Date) // true types.isPromise(new Promise(() => {})) // true types.isArrayBuffer(new ArrayBuffer(16)) // true Strict equality In JavaScript, when judging the equality of objects, arrays and other variables, if const util = require('util') const val1 = { name: 'shenfq' } const val2 = { name: 'shenfq' } console.log('val1 === val2', val1 === val2) // false console.log('isDeepStrictEqual', util.isDeepStrictEqual(val1, val2)) // true This method can also be used to determine whether arrays are strictly equal: const util = require('util') const arr1 = [1, 3, 5] const arr2 = [1, 3, 5] console.log('arr1 === arr2', arr1 === arr2) // false console.log('isDeepStrictEqual', util.isDeepStrictEqual(arr1, arr2)) // true Error First & Promise Early Node APIs were all // Here is an example of reading a file const fs = require('fs') fs.readFile('nginx.log', (error, data) => { if (error) { // Failed to read the file console.error(error) return } // Read the file successfully, print the result console.log(data) }) When Node 8 was released, a new const fs = require('fs') const util = require('util') const readFile = util.promisify(fs.readFile) readFile('./2021-11-11.log', { encoding: 'utf-8' }) .then(text => console.log(text)) .catch(error => console.error(error)) However, many people later felt that the way these native APIs support Promise is too cumbersome, and each API needs to be wrapped with a separate const fs = require('fs').promises fs.readFile('./2021-11-11.log', { encoding: 'utf-8' }) .then(text => console.log(text)) .catch(error => console.error(error)) Note: After Node 14, a new way to import const fs = require('fs/promises') fs.readFile('./2021-11-11.log', { encoding: 'utf-8' }) .then(text => console.log(text)) .catch(error => console.error(error)) In addition to converting Next, const fs = require('fs/promises') const util = require('util') const readFile = util.callbackify(fs.readFile) readFile('./2021-11-12.log', { encoding: 'utf-8' }, (error, text) => { if (error) { console.error(error) return } console.log(text) }) Debugging and Output If you have developed a Node service, you should have used the const debug = require('debug') const log = debug('app') const user = { name: 'shenfq' } log('Current user: %o', user) In fact, a similar effect can be achieved through const debug = require('debug') const log = debug('app') const user = { name: 'shenfq' } log('Current user: %o', user) Just replace the If you look carefully at the code above, you will find that there is a const { format } = require('util') console.log( format('Current user: %o', { name: 'shenfq', age: 25 }) ) In addition to
Objects in JavaScript are very complex. In addition to directly using const { inspect } = require('util') const user = { age: 25, name: 'shenfq', work: name: 'coding', Seniority: 5 } } console.log(inspect(user)) It seems that Of course, the above is only part of the configuration. For more detailed configuration, please refer to the node documentation. Let's write a few examples below: All properties are displayed in line breaks: inspect(user, { compact: false }) Format only the first level value of an object: inspect(user, { depth: 0, compact: false }) Output in reverse order according to the key value encoding: inspect(user, { compact: false, sorted: (a, b) => a < b ? 1 : -1 }) The above is the detailed content of the tutorial example of the Util module in node.js. For more information about the Util module in node.js, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Sharing of two website page translation plug-ins
1. Description Earlier we talked about the instal...
Table of contents Preface JS Magic Number Storing...
This article discusses the difficulties and ideas...
1. The Linux server configures /etc/hosts.deny to...
Today I learned a new CSS special effect, the wav...
1. Modify the firewall configuration file # vi /e...
mysql create table sql statement Common SQL state...
Requirements: Remove HTTP response headers in IIS...
Because colleagues in the company need Nginx log ...
The table creation command requires: The name of...
Table of contents 1. Introduction 2. Solution Imp...
After the official release of Activiti7, it has f...
Effect principle Mainly use CSS gradient to achie...
In most application scenarios, we need to back up...
Environment Preparation Before starting any opera...