summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Guerrero <stephenguerrero43@gmail.com>2020-11-16 18:20:46 -0500
committerGitHub <noreply@github.com>2020-11-16 18:20:46 -0500
commit06cf6df954f0ab745bccb9455e7da14104acf1ae (patch)
treea790459dcd61ea7cc7ec3bc79aafdd05df19679f
parent636af2850c90f6bf7005a270d95b68bc9718de75 (diff)
feat(std/node): Add util.deprecate (#8407)
-rw-r--r--std/node/_util.ts16
-rw-r--r--std/node/util_test.ts25
2 files changed, 41 insertions, 0 deletions
diff --git a/std/node/_util.ts b/std/node/_util.ts
index 9cf996670..b21743541 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<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 */
diff --git a/std/node/util_test.ts b/std/node/util_test.ts
index 9db463b99..d9c05f8a5 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;
+});