mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 07:44:48 -05:00
feat(std/node): Add util.deprecate (#8407)
This commit is contained in:
parent
636af2850c
commit
06cf6df954
2 changed files with 41 additions and 0 deletions
|
@ -123,6 +123,22 @@ export function getSystemErrorName(code: number): string | undefined {
|
|||
return errorMap.get(code)?.[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* https://nodejs.org/api/util.html#util_util_deprecate_fn_msg_code
|
||||
* @param _code This implementation of deprecate won't apply the deprecation code
|
||||
*/
|
||||
export function deprecate<A extends Array<unknown>, B>(
|
||||
this: unknown,
|
||||
callback: (...args: A) => B,
|
||||
msg: string,
|
||||
_code?: string,
|
||||
) {
|
||||
return function (this: unknown, ...args: A) {
|
||||
console.warn(msg);
|
||||
return callback.apply(this, args);
|
||||
};
|
||||
}
|
||||
|
||||
import { _TextDecoder, _TextEncoder } from "./_utils.ts";
|
||||
|
||||
/** The global TextDecoder */
|
||||
|
|
|
@ -220,3 +220,28 @@ Deno.test({
|
|||
}
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test("[util] deprecate", () => {
|
||||
const warn = console.warn.bind(null);
|
||||
|
||||
let output;
|
||||
console.warn = function (str: string) {
|
||||
output = str;
|
||||
warn(output);
|
||||
};
|
||||
|
||||
const message = "x is deprecated";
|
||||
|
||||
const expected = 12;
|
||||
let result;
|
||||
const x = util.deprecate(() => {
|
||||
result = expected;
|
||||
}, message);
|
||||
|
||||
x();
|
||||
|
||||
assertEquals(expected, result);
|
||||
assertEquals(output, message);
|
||||
|
||||
console.warn = warn;
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue