From 06cf6df954f0ab745bccb9455e7da14104acf1ae Mon Sep 17 00:00:00 2001 From: Steven Guerrero Date: Mon, 16 Nov 2020 18:20:46 -0500 Subject: [PATCH] feat(std/node): Add util.deprecate (#8407) --- std/node/_util.ts | 16 ++++++++++++++++ std/node/util_test.ts | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/std/node/_util.ts b/std/node/_util.ts index 9cf9966701..b217435416 100644 --- a/std/node/_util.ts +++ b/std/node/_util.ts @@ -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, 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 */ diff --git a/std/node/util_test.ts b/std/node/util_test.ts index 9db463b99e..d9c05f8a5d 100644 --- a/std/node/util_test.ts +++ b/std/node/util_test.ts @@ -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; +});