UNPKG

2.3 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.call = call;
7exports.default = void 0;
8exports.note = note;
9exports.noteOnce = noteOnce;
10exports.preMessage = void 0;
11exports.resetWarned = resetWarned;
12exports.warning = warning;
13exports.warningOnce = warningOnce;
14/* eslint-disable no-console */
15var warned = {};
16var preWarningFns = [];
17
18/**
19 * Pre warning enable you to parse content before console.error.
20 * Modify to null will prevent warning.
21 */
22var preMessage = exports.preMessage = function preMessage(fn) {
23 preWarningFns.push(fn);
24};
25
26/**
27 * Warning if condition not match.
28 * @param valid Condition
29 * @param message Warning message
30 * @example
31 * ```js
32 * warning(false, 'some error'); // print some error
33 * warning(true, 'some error'); // print nothing
34 * warning(1 === 2, 'some error'); // print some error
35 * ```
36 */
37function warning(valid, message) {
38 if (process.env.NODE_ENV !== 'production' && !valid && console !== undefined) {
39 var finalMessage = preWarningFns.reduce(function (msg, preMessageFn) {
40 return preMessageFn(msg !== null && msg !== void 0 ? msg : '', 'warning');
41 }, message);
42 if (finalMessage) {
43 console.error("Warning: ".concat(finalMessage));
44 }
45 }
46}
47
48/** @see Similar to {@link warning} */
49function note(valid, message) {
50 if (process.env.NODE_ENV !== 'production' && !valid && console !== undefined) {
51 var finalMessage = preWarningFns.reduce(function (msg, preMessageFn) {
52 return preMessageFn(msg !== null && msg !== void 0 ? msg : '', 'note');
53 }, message);
54 if (finalMessage) {
55 console.warn("Note: ".concat(finalMessage));
56 }
57 }
58}
59function resetWarned() {
60 warned = {};
61}
62function call(method, valid, message) {
63 if (!valid && !warned[message]) {
64 method(false, message);
65 warned[message] = true;
66 }
67}
68
69/** @see Same as {@link warning}, but only warn once for the same message */
70function warningOnce(valid, message) {
71 call(warning, valid, message);
72}
73
74/** @see Same as {@link warning}, but only warn once for the same message */
75function noteOnce(valid, message) {
76 call(note, valid, message);
77}
78warningOnce.preMessage = preMessage;
79warningOnce.resetWarned = resetWarned;
80warningOnce.noteOnce = noteOnce;
81var _default = exports.default = warningOnce;
\No newline at end of file