forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (54 loc) · 1.93 KB
/
index.js
File metadata and controls
57 lines (54 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Script example for ScriptAPI
// Author: Jayly#1397 <Jayly Discord>
// Project: https://github.com/JaylyDev/ScriptAPI
/**
* @type {string[]}
*/
const emittedIds = [];
/**
* The `deprecate()` method wraps `fn` (which may be a function or class) in
* such a way that it is marked as deprecated.
*
* ```js
* exports.obsoleteFunction = deprecate(() => {
* // Do something here.
* }, 'obsoleteFunction() is deprecated. Use newShinyFunction() instead.');
* ```
*
* When called, `deprecate()` will return a function that will emit a`DeprecationWarning` using the `'warning'` event. The warning will
* be emitted and printed to `stderr` the first time the returned function is
* called. After the warning is emitted, the wrapped function is called without
* emitting a warning.
*
* If the same optional `id` is supplied in multiple calls to `deprecate()`,
* the warning will be emitted only once for that `id`.
*
* ```js
* const fn1 = deprecate(someFunction, someMessage, 'DEP0001');
* const fn2 = deprecate(someOtherFunction, someOtherMessage, 'DEP0001');
* fn1(); // Emits a deprecation warning with id DEP0001
* fn2(); // Does not emit a deprecation warning because it has the same id
* ```
*
*
* @template {Function} T
* @param {T} fn The function that is being deprecated.
* @param {string} msg A warning message to display when the deprecated function is invoked.
* @param {string} [id] A deprecation id. See the `list of deprecated APIs` for a list of ids.
* @return {T} The deprecated function wrapped to emit a warning.
*/
export function deprecate(fn, msg, id) {
let deprecationMessage = "";
if (typeof id === "string") {
if (emittedIds.includes(id)) return fn;
emittedIds.push(id);
deprecationMessage += `[${id}] `;
}
deprecationMessage += `DeprecationWarning: ${msg}`;
function deprecated() {
console.warn(deprecationMessage);
return fn.call(this, arguments);
}
// @ts-ignore
return deprecated;
}