-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
124 lines (110 loc) · 3.92 KB
/
index.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict'
const { format } = require('node:util')
/**
* @namespace processWarning
*/
/**
* Represents a warning item with details.
* @typedef {Function} WarningItem
* @param {*} [a] Possible message interpolation value.
* @param {*} [b] Possible message interpolation value.
* @param {*} [c] Possible message interpolation value.
* @property {string} name - The name of the warning.
* @property {string} code - The code associated with the warning.
* @property {string} message - The warning message.
* @property {boolean} emitted - Indicates if the warning has been emitted.
* @property {function} format - Formats the warning message.
*/
/**
* Options for creating a process warning.
* @typedef {Object} ProcessWarningOptions
* @property {string} name - The name of the warning.
* @property {string} code - The code associated with the warning.
* @property {string} message - The warning message.
* @property {boolean} [unlimited=false] - If true, allows unlimited emissions of the warning.
*/
/**
* Represents the process warning functionality.
* @typedef {Object} ProcessWarning
* @property {function} createWarning - Creates a warning item.
* @property {function} createDeprecation - Creates a deprecation warning item.
*/
/**
* Creates a deprecation warning item.
* @function
* @memberof processWarning
* @param {ProcessWarningOptions} params - Options for creating the warning.
* @returns {WarningItem} The created deprecation warning item.
*/
function createDeprecation (params) {
return createWarning({ ...params, name: 'DeprecationWarning' })
}
/**
* Creates a warning item.
* @function
* @memberof processWarning
* @param {ProcessWarningOptions} params - Options for creating the warning.
* @returns {WarningItem} The created warning item.
* @throws {Error} Throws an error if name, code, or message is empty, or if opts.unlimited is not a boolean.
*/
function createWarning ({ name, code, message, unlimited = false } = {}) {
if (!name) throw new Error('Warning name must not be empty')
if (!code) throw new Error('Warning code must not be empty')
if (!message) throw new Error('Warning message must not be empty')
if (typeof unlimited !== 'boolean') throw new Error('Warning opts.unlimited must be a boolean')
code = code.toUpperCase()
let warningContainer = {
[name]: function (a, b, c) {
if (warning.emitted === true && warning.unlimited !== true) {
return
}
warning.emitted = true
process.emitWarning(warning.format(a, b, c), warning.name, warning.code)
}
}
if (unlimited) {
warningContainer = {
[name]: function (a, b, c) {
warning.emitted = true
process.emitWarning(warning.format(a, b, c), warning.name, warning.code)
}
}
}
const warning = warningContainer[name]
warning.emitted = false
warning.message = message
warning.unlimited = unlimited
warning.code = code
/**
* Formats the warning message.
* @param {*} [a] Possible message interpolation value.
* @param {*} [b] Possible message interpolation value.
* @param {*} [c] Possible message interpolation value.
* @returns {string} The formatted warning message.
*/
warning.format = function (a, b, c) {
let formatted
if (a && b && c) {
formatted = format(message, a, b, c)
} else if (a && b) {
formatted = format(message, a, b)
} else if (a) {
formatted = format(message, a)
} else {
formatted = message
}
return formatted
}
return warning
}
/**
* Module exports containing the process warning functionality.
* @namespace
* @property {function} createWarning - Creates a warning item.
* @property {function} createDeprecation - Creates a deprecation warning item.
* @property {ProcessWarning} processWarning - Represents the process warning functionality.
*/
const out = { createWarning, createDeprecation }
module.exports = out
module.exports.default = out
module.exports.processWarning = out