Skip to content

Commit 102cf34

Browse files
authored
feat: default export & preMessage (#437)
* chore: preMessage * test: master export * test: update test case * chore: more export
1 parent 4225ce6 commit 102cf34

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"url": "[email protected]:react-component/util.git"
1616
},
1717
"license": "MIT",
18+
"main": "./lib/index",
19+
"module": "./es/index",
1820
"files": [
1921
"lib",
2022
"es"

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { default as useMergedState } from './hooks/useMergedState';
2+
export { default as get } from './utils/get';
3+
export { default as set } from './utils/set';
4+
export { default as warning } from './warning';

src/warning.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,54 @@
11
/* eslint-disable no-console */
22
let warned: Record<string, boolean> = {};
33

4+
export type preMessageFn = (
5+
message: string,
6+
type: 'warning' | 'note',
7+
) => string | null | undefined | number;
8+
9+
const preWarningFns: preMessageFn[] = [];
10+
11+
/**
12+
* Pre warning enable you to parse content before console.error.
13+
* Modify to null will prevent warning.
14+
*/
15+
export const preMessage = (fn: preMessageFn) => {
16+
preWarningFns.push(fn);
17+
};
18+
419
export function warning(valid: boolean, message: string) {
520
// Support uglify
6-
if (process.env.NODE_ENV !== 'production' && !valid && console !== undefined) {
7-
console.error(`Warning: ${message}`);
21+
if (
22+
process.env.NODE_ENV !== 'production' &&
23+
!valid &&
24+
console !== undefined
25+
) {
26+
const finalMessage = preWarningFns.reduce(
27+
(msg, preMessageFn) => preMessageFn(msg ?? '', 'warning'),
28+
message,
29+
);
30+
31+
if (finalMessage) {
32+
console.error(`Warning: ${finalMessage}`);
33+
}
834
}
935
}
1036

1137
export function note(valid: boolean, message: string) {
1238
// Support uglify
13-
if (process.env.NODE_ENV !== 'production' && !valid && console !== undefined) {
14-
console.warn(`Note: ${message}`);
39+
if (
40+
process.env.NODE_ENV !== 'production' &&
41+
!valid &&
42+
console !== undefined
43+
) {
44+
const finalMessage = preWarningFns.reduce(
45+
(msg, preMessageFn) => preMessageFn(msg ?? '', 'note'),
46+
message,
47+
);
48+
49+
if (finalMessage) {
50+
console.warn(`Note: ${finalMessage}`);
51+
}
1552
}
1653
}
1754

@@ -38,5 +75,9 @@ export function noteOnce(valid: boolean, message: string) {
3875
call(note, valid, message);
3976
}
4077

78+
warningOnce.preMessage = preMessage;
79+
warningOnce.resetWarned = resetWarned;
80+
warningOnce.noteOnce = noteOnce;
81+
4182
export default warningOnce;
4283
/* eslint-enable */

tests/warning.test.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import React from 'react';
21
import { render } from '@testing-library/react';
3-
import warning, { resetWarned, noteOnce } from '../src/warning';
2+
import React from 'react';
3+
import { warning } from '../src';
44
import unsafeLifecyclesPolyfill from '../src/unsafeLifecyclesPolyfill';
55

6+
const { resetWarned, noteOnce } = warning;
7+
68
describe('warning', () => {
79
beforeEach(() => {
810
resetWarned();
@@ -67,4 +69,31 @@ describe('warning', () => {
6769
);
6870
warnSpy.mockRestore();
6971
});
72+
73+
describe('preMessage', () => {
74+
it('modify message', () => {
75+
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
76+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
77+
78+
warning.preMessage((msg, type) => {
79+
if (type === 'warning') {
80+
return `WW-${msg}`;
81+
}
82+
if (type === 'note') {
83+
return `NN-${msg}`;
84+
}
85+
86+
return null;
87+
});
88+
89+
warning(false, 'warn');
90+
warning.noteOnce(false, 'note');
91+
92+
expect(errSpy).toHaveBeenCalledWith('Warning: WW-warn');
93+
expect(warnSpy).toHaveBeenCalledWith('Note: NN-note');
94+
95+
errSpy.mockRestore();
96+
warnSpy.mockRestore();
97+
});
98+
});
7099
});

0 commit comments

Comments
 (0)