Skip to content

Commit

Permalink
util module fixes.
Browse files Browse the repository at this point in the history
node 16.x
- Add `debug` alias.
- Add `getSystemErrorName`.
- Add `stripVTControlCharacters`.

node 14.x
- Add `debug` alias. Fix `debuglog` types.
- Add `getSystemErrorName`.
  • Loading branch information
Semigradsky authored and panva committed Oct 12, 2021
1 parent fca0428 commit cf46e16
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion types/node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Type definitions for non-npm package Node.js 16.10
// Type definitions for non-npm package Node.js 16.11
// Project: https://nodejs.org/
// Definitions by: Microsoft TypeScript <https://github.com/Microsoft>
// DefinitelyTyped <https://github.com/DefinitelyTyped>
Expand Down
8 changes: 7 additions & 1 deletion types/node/test/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as util from 'node:util';
import assert = require('node:assert');
import { readFile } from 'node:fs';
import { access, readFile } from 'node:fs';

// Old and new util.inspect APIs
util.inspect(["This is nice"], false, 5);
Expand Down Expand Up @@ -182,12 +182,18 @@ const errorMap: Map<number, [string, string]> = util.getSystemErrorMap();
const logger: util.DebugLogger = util.debuglog('section');
logger.enabled; // $ExpectType boolean
util.debuglog('section', (fn: util.DebugLoggerFunction) => { });
util.debug('section', (fn: util.DebugLoggerFunction) => { });
}

{
const foo: string = util.toUSVString('foo');
}

access('file/that/does/not/exist', (err) => {
const name = util.getSystemErrorName(err!.errno!);
console.error(name);
});

{
util.stripVTControlCharacters('\u001B[4mvalue\u001B[0m'); // $ExpectType string
}
21 changes: 21 additions & 0 deletions types/node/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ declare module 'util' {
* @since v10.0.0
*/
export function formatWithOptions(inspectOptions: InspectOptions, format?: any, ...param: any[]): string;
/**
* Returns the string name for a numeric error code that comes from a Node.js API.
* The mapping between error codes and error names is platform-dependent.
* See `Common System Errors` for the names of common errors.
*
* ```js
* fs.access('file/that/does/not/exist', (err) => {
* const name = util.getSystemErrorName(err.errno);
* console.error(name); // ENOENT
* });
* ```
* @since v9.7.0
*/
export function getSystemErrorName(err: number): string;
/**
* Returns a Map of all system error codes available from the Node.js API.
* The mapping between error codes and error names is platform-dependent.
Expand Down Expand Up @@ -314,6 +328,9 @@ declare module 'util' {
* Allows changing inspect settings from the repl.
*/
let replDefaults: InspectOptions;
/**
* That can be used to declare custom inspect functions.
*/
const custom: unique symbol;
}
/**
Expand Down Expand Up @@ -520,6 +537,7 @@ declare module 'util' {
* @return The logging function
*/
export function debuglog(section: string, callback?: (fn: DebugLoggerFunction) => void): DebugLogger;
export const debug: typeof debuglog;
/**
* Returns `true` if the given `object` is a `Boolean`. Otherwise, returns `false`.
*
Expand Down Expand Up @@ -971,6 +989,9 @@ declare module 'util' {
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<void>;
export function promisify(fn: Function): Function;
export namespace promisify {
/**
* That can be used to declare custom promisified variants of functions.
*/
const custom: unique symbol;
}
/**
Expand Down
16 changes: 15 additions & 1 deletion types/node/v14/test/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as util from 'util';
import assert = require('assert');
import { readFile } from 'fs';
import { access, readFile } from 'fs';

{
// Old and new util.inspect APIs
Expand Down Expand Up @@ -323,3 +323,17 @@ function testUtilTypes(
object; // $ExpectType WeakSet<any>
}
}

{
const logger: util.DebugLogger = util.debuglog('section');
logger.enabled; // $ExpectType boolean
util.debuglog('section', (fn: util.DebugLoggerFunction) => { });
util.debug('section', (fn: util.DebugLoggerFunction) => { });
}

{
access('file/that/does/not/exist', (err) => {
const name = util.getSystemErrorName(err!.errno!);
console.error(name);
});
}
8 changes: 7 additions & 1 deletion types/node/v14/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ declare module 'util' {
}
function format(format?: any, ...param: any[]): string;
function formatWithOptions(inspectOptions: InspectOptions, format?: any, ...param: any[]): string;
function getSystemErrorName(err: number): string;
/** @deprecated since v0.11.3 - use a third party module instead. */
function log(string: string): void;
function inspect(object: any, showHidden?: boolean, depth?: number | null, color?: boolean): string;
Expand All @@ -32,7 +33,12 @@ declare module 'util' {
/** @deprecated since v4.0.0 - use `util.types.isNativeError()` instead. */
function isError(object: any): object is Error;
function inherits(constructor: any, superConstructor: any): void;
function debuglog(key: string): (msg: string, ...param: any[]) => void;
type DebugLoggerFunction = (msg: string, ...param: any[]) => void;
interface DebugLogger extends DebugLoggerFunction {
enabled: boolean;
}
function debuglog(key: string, callback?: (fn: DebugLoggerFunction) => void): DebugLogger;
const debug: typeof debuglog;
/** @deprecated since v4.0.0 - use `typeof value === 'boolean'` instead. */
function isBoolean(object: any): object is boolean;
/** @deprecated since v4.0.0 - use `Buffer.isBuffer()` instead. */
Expand Down

0 comments on commit cf46e16

Please sign in to comment.