0
# util
1
2
Cross-platform implementation of Node.js's built-in util module, providing utility functions for string formatting, type checking, inheritance, promises, and debugging. Works seamlessly across Node.js, browsers, browserify, and webpack environments.
3
4
## Package Information
5
6
- **Package Name**: util
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install util`
10
- **Repository**: https://github.com/browserify/node-util
11
12
## Core Imports
13
14
```javascript
15
const util = require('util');
16
```
17
18
For ES modules:
19
```javascript
20
import * as util from 'util';
21
```
22
23
Individual imports:
24
```javascript
25
const { format, inspect, promisify, inherits } = require('util');
26
```
27
28
## Basic Usage
29
30
```javascript
31
const util = require('util');
32
33
// String formatting
34
const message = util.format('Hello %s, you are %d years old', 'Alice', 25);
35
console.log(message); // "Hello Alice, you are 25 years old"
36
37
// Object inspection
38
const obj = { name: 'Bob', details: { age: 30, active: true } };
39
console.log(util.inspect(obj, { colors: true, depth: 2 }));
40
41
// Type checking
42
console.log(util.isString('hello')); // true
43
console.log(util.isArray([1, 2, 3])); // true
44
45
// Promise conversion
46
const fs = require('fs');
47
const readFileAsync = util.promisify(fs.readFile);
48
```
49
50
## Architecture
51
52
The util module is organized into several key areas:
53
54
- **String Formatting**: Functions for formatted string output and object inspection
55
- **Type Checking**: Comprehensive type validation utilities including basic and advanced type detection
56
- **Promise Utilities**: Tools for converting between callback and promise-based functions
57
- **Utilities**: Inheritance, deprecation warnings, debugging, and object extension helpers
58
- **Browser Compatibility**: Platform-specific implementations for cross-environment support
59
60
## Capabilities
61
62
### String Formatting
63
64
Essential tools for formatted output and object inspection. Provides printf-style formatting and detailed object visualization with customizable styling.
65
66
```javascript { .api }
67
function format(f, ...args): string;
68
function inspect(obj, opts?): string;
69
```
70
71
[String Formatting](./string-formatting.md)
72
73
### Type Checking
74
75
Comprehensive type validation system with both basic JavaScript type checking and advanced type detection for modern JavaScript features including typed arrays, collections, and boxed primitives.
76
77
```javascript { .api }
78
function isString(arg): boolean;
79
function isNumber(arg): boolean;
80
function isArray(ar): boolean;
81
function isFunction(arg): boolean;
82
function isObject(arg): boolean;
83
84
// Extended types via util.types
85
const types: {
86
isMap(value): boolean;
87
isSet(value): boolean;
88
isPromise(value): boolean;
89
isArrayBuffer(value): boolean;
90
isTypedArray(value): boolean;
91
// ... and many more
92
};
93
```
94
95
[Type Checking](./type-checking.md)
96
97
### Promise Utilities
98
99
Conversion tools between callback-based and promise-based function patterns, enabling modern async/await usage with legacy callback APIs.
100
101
```javascript { .api }
102
function promisify(original): Function;
103
function callbackify(original): Function;
104
```
105
106
[Promise Utilities](./promises.md)
107
108
### Utilities
109
110
Core utilities for inheritance, deprecation warnings, debugging, object extension, and timestamp logging.
111
112
```javascript { .api }
113
function inherits(ctor, superCtor): void;
114
function deprecate(fn, msg): Function;
115
function debuglog(set): Function;
116
function log(...args): void;
117
```
118
119
[Utilities](./utilities.md)
120
121
## Browser Support
122
123
- Uses ES5 features - requires es5-shim for IE8 support
124
- Promise utilities require native Promises or polyfill (e.g., es6-promise)
125
- Includes browser-specific Buffer detection for cross-platform compatibility
126
- Supports CommonJS, AMD, and global usage patterns