@d-matrix/utils 1.27.1 → 1.28.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/dist/algorithm/binary.d.ts +8 -8
  2. package/dist/algorithm/binary.js +38 -38
  3. package/dist/algorithm/index.d.ts +2 -2
  4. package/dist/algorithm/index.js +2 -2
  5. package/dist/algorithm/tree.d.ts +34 -34
  6. package/dist/algorithm/tree.js +124 -124
  7. package/dist/array.d.ts +46 -5
  8. package/dist/array.js +123 -37
  9. package/dist/clipboard.d.ts +12 -12
  10. package/dist/clipboard.js +107 -107
  11. package/dist/color.d.ts +7 -7
  12. package/dist/color.js +41 -41
  13. package/dist/date.d.ts +34 -34
  14. package/dist/date.js +62 -62
  15. package/dist/decimal.d.ts +17 -17
  16. package/dist/decimal.js +23 -23
  17. package/dist/dom.d.ts +11 -11
  18. package/dist/dom.js +27 -27
  19. package/dist/echarts.d.ts +36 -36
  20. package/dist/echarts.js +112 -112
  21. package/dist/file.d.ts +49 -49
  22. package/dist/file.js +154 -154
  23. package/dist/i18n.d.ts +10 -10
  24. package/dist/i18n.js +12 -12
  25. package/dist/index.d.ts +16 -16
  26. package/dist/index.js +16 -16
  27. package/dist/number.d.ts +2 -2
  28. package/dist/number.js +4 -4
  29. package/dist/object.d.ts +13 -13
  30. package/dist/object.js +28 -28
  31. package/dist/operator.d.ts +6 -6
  32. package/dist/operator.js +6 -6
  33. package/dist/react/enhancedComponent.d.ts +12 -12
  34. package/dist/react/enhancedComponent.js +16 -16
  35. package/dist/react/index.d.ts +15 -12
  36. package/dist/react/index.js +15 -12
  37. package/dist/react/renderToString.d.ts +3 -3
  38. package/dist/react/renderToString.js +30 -30
  39. package/dist/react/types.d.ts +9 -9
  40. package/dist/react/types.js +1 -1
  41. package/dist/react/useCopyToClipboard.d.ts +21 -21
  42. package/dist/react/useCopyToClipboard.js +44 -44
  43. package/dist/react/useDeepCompareRef.d.ts +2 -2
  44. package/dist/react/useDeepCompareRef.js +11 -11
  45. package/dist/react/useDisableContextMenu.d.ts +7 -7
  46. package/dist/react/useDisableContextMenu.js +24 -24
  47. package/dist/react/useEventCallback.d.ts +7 -0
  48. package/dist/react/useEventCallback.js +19 -0
  49. package/dist/react/useForwardRef.d.ts +9 -9
  50. package/dist/react/useForwardRef.js +22 -22
  51. package/dist/react/useId.d.ts +1 -0
  52. package/dist/react/useId.js +12 -0
  53. package/dist/react/useIsFirstRender.d.ts +6 -6
  54. package/dist/react/useIsFirstRender.js +14 -14
  55. package/dist/react/useIsMounted.d.ts +2 -2
  56. package/dist/react/useIsMounted.js +13 -13
  57. package/dist/react/useIsomorphicLayoutEffect.d.ts +2 -2
  58. package/dist/react/useIsomorphicLayoutEffect.js +2 -2
  59. package/dist/react/useMediaQuery.d.ts +14 -14
  60. package/dist/react/useMediaQuery.js +42 -42
  61. package/dist/react/useSafeTimeout.d.ts +12 -0
  62. package/dist/react/useSafeTimeout.js +28 -0
  63. package/dist/react/useStateCallback.d.ts +2 -2
  64. package/dist/react/useStateCallback.js +17 -17
  65. package/dist/support.d.ts +12 -12
  66. package/dist/support.js +12 -12
  67. package/dist/timer.d.ts +5 -5
  68. package/dist/timer.js +5 -5
  69. package/dist/types.d.ts +128 -21
  70. package/dist/types.js +1 -1
  71. package/package.json +1 -1
  72. package/readme.md +5 -1
package/dist/file.js CHANGED
@@ -1,154 +1,154 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- /**
11
- * 转换BlobPart或者文件地址为图片对象
12
- * @param file 传入文件 或者 url
13
- * @returns 返回 一个图片详情对象
14
- */
15
- export const toImage = (file, options) => {
16
- return new Promise((resolve, reject) => {
17
- const image = new Image();
18
- if (typeof file === 'string') {
19
- image.src = file;
20
- }
21
- else {
22
- const blob = URL.createObjectURL(new Blob([file], options));
23
- image.src = blob;
24
- }
25
- // 如果有缓存,读缓存
26
- if (image.complete) {
27
- resolve(image);
28
- }
29
- else {
30
- //否则加载图片
31
- image.onload = function () {
32
- resolve(image);
33
- image.onload = null; // 避免重复加载
34
- };
35
- image.onerror = function () {
36
- reject('图片加载失败');
37
- };
38
- }
39
- });
40
- };
41
- /**
42
- * 图片宽,高校验
43
- * @param file 传入文件 或者 url
44
- * @param limitSize 限制宽度, 高度
45
- * @returns isOK: 是否超出宽高; width, height: 图片的宽高
46
- */
47
- export const validateImageSize = (file, limitSize, options) => __awaiter(void 0, void 0, void 0, function* () {
48
- const image = yield toImage(file, options);
49
- return { isOk: image.width <= limitSize.width && image.height <= limitSize.height, width: image.width, height: image.height };
50
- });
51
- /**
52
- * 检测图片地址是否可用
53
- * @param src 图片地址
54
- * @param img HTMLImageElement
55
- * @returns Promise<boolean>
56
- */
57
- export function isImageExists(src, img = new Image()) {
58
- return __awaiter(this, void 0, void 0, function* () {
59
- if (!src)
60
- return false;
61
- return new Promise((resolve) => {
62
- img.src = src;
63
- img.onload = () => resolve(true);
64
- img.onerror = () => resolve(false);
65
- });
66
- });
67
- }
68
- /**
69
- * 从Content-Disposition中获取文件名
70
- * @param header 包含Content-Disposition, 值为'attachment;filename=%E5%A4%A7%E8%A1%8C%E6%8C%87%E5%AF%BC2024-06-27-2024-06-28.xlsx'
71
- * @returns string
72
- *
73
- */
74
- export function getFilenameFromContentDispositionHeader(header) {
75
- const contentDisposition = header['content-disposition'];
76
- if (!contentDisposition)
77
- return '';
78
- const filename = contentDisposition.split('filename=')[1].split(';')[0];
79
- return decodeURIComponent(filename);
80
- }
81
- const HyperLinkTargets = ['_self', '_blank', '_parent', '_top'];
82
- /**
83
- * 文件下载
84
- * @param source 文件地址或blob对象
85
- * @param fileName 文件名
86
- * @param isPreview 是否
87
- */
88
- // `a.click()` doesn't work for all browsers (#465)
89
- function click(node) {
90
- try {
91
- node.dispatchEvent(new MouseEvent('click'));
92
- }
93
- catch (e) {
94
- var evt = document.createEvent('MouseEvents');
95
- evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
96
- node.dispatchEvent(evt);
97
- }
98
- }
99
- export function download(source, fileName = '', target) {
100
- const link = document.createElement('a');
101
- if (typeof source === 'string') {
102
- if (!source.length) {
103
- if (process.env.NODE_ENV === 'development') {
104
- console.error('下载失败,原因:source为空字符串');
105
- return;
106
- }
107
- }
108
- if (typeof target === 'string' && HyperLinkTargets.includes(target)) {
109
- link.target = target;
110
- }
111
- else {
112
- // https://stackoverflow.com/questions/33909763/download-attribute-with-a-file-name-not-working
113
- // download 只在同源 URL 或 blob:、data: 协议起作用, 如果下载不同源的oss文件,download属性无效
114
- link.download = fileName;
115
- }
116
- link.href = source;
117
- }
118
- let objectURL;
119
- if (source instanceof Blob) {
120
- const objectURL = window.URL.createObjectURL(source);
121
- link.href = objectURL;
122
- link.download = fileName;
123
- }
124
- document.body.appendChild(link);
125
- click(link);
126
- setTimeout(() => {
127
- if (typeof objectURL === 'string') {
128
- window.URL.revokeObjectURL(objectURL);
129
- }
130
- document.body.removeChild(link);
131
- }, 4e4); // 40S
132
- }
133
- /**
134
- * 通过创建iframe进行文件下载
135
- * @param source
136
- * @returns
137
- */
138
- export function downloadFileByIframe(source) {
139
- const httpsPath = source.replace(/http:\/\//, "https://");
140
- const iframes = document.getElementsByTagName("iframe");
141
- if (iframes.length === 0 ||
142
- (iframes.length > 0 && iframes[0].className === "fill" && iframes[iframes.length - 1].className === "fill")) {
143
- const element = document.createElement("iframe");
144
- element.style.display = "none";
145
- element.src = httpsPath;
146
- document.body.appendChild(element);
147
- return true;
148
- }
149
- if (iframes.length > 0 && iframes[iframes.length - 1].className !== "fill") {
150
- iframes[0].src = httpsPath;
151
- return true;
152
- }
153
- return false;
154
- }
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ /**
11
+ * 转换BlobPart或者文件地址为图片对象
12
+ * @param file 传入文件 或者 url
13
+ * @returns 返回 一个图片详情对象
14
+ */
15
+ export const toImage = (file, options) => {
16
+ return new Promise((resolve, reject) => {
17
+ const image = new Image();
18
+ if (typeof file === 'string') {
19
+ image.src = file;
20
+ }
21
+ else {
22
+ const blob = URL.createObjectURL(new Blob([file], options));
23
+ image.src = blob;
24
+ }
25
+ // 如果有缓存,读缓存
26
+ if (image.complete) {
27
+ resolve(image);
28
+ }
29
+ else {
30
+ //否则加载图片
31
+ image.onload = function () {
32
+ resolve(image);
33
+ image.onload = null; // 避免重复加载
34
+ };
35
+ image.onerror = function () {
36
+ reject('图片加载失败');
37
+ };
38
+ }
39
+ });
40
+ };
41
+ /**
42
+ * 图片宽,高校验
43
+ * @param file 传入文件 或者 url
44
+ * @param limitSize 限制宽度, 高度
45
+ * @returns isOK: 是否超出宽高; width, height: 图片的宽高
46
+ */
47
+ export const validateImageSize = (file, limitSize, options) => __awaiter(void 0, void 0, void 0, function* () {
48
+ const image = yield toImage(file, options);
49
+ return { isOk: image.width <= limitSize.width && image.height <= limitSize.height, width: image.width, height: image.height };
50
+ });
51
+ /**
52
+ * 检测图片地址是否可用
53
+ * @param src 图片地址
54
+ * @param img HTMLImageElement
55
+ * @returns Promise<boolean>
56
+ */
57
+ export function isImageExists(src, img = new Image()) {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ if (!src)
60
+ return false;
61
+ return new Promise((resolve) => {
62
+ img.src = src;
63
+ img.onload = () => resolve(true);
64
+ img.onerror = () => resolve(false);
65
+ });
66
+ });
67
+ }
68
+ /**
69
+ * 从Content-Disposition中获取文件名
70
+ * @param header 包含Content-Disposition, 值为'attachment;filename=%E5%A4%A7%E8%A1%8C%E6%8C%87%E5%AF%BC2024-06-27-2024-06-28.xlsx'
71
+ * @returns string
72
+ *
73
+ */
74
+ export function getFilenameFromContentDispositionHeader(header) {
75
+ const contentDisposition = header['content-disposition'];
76
+ if (!contentDisposition)
77
+ return '';
78
+ const filename = contentDisposition.split('filename=')[1].split(';')[0];
79
+ return decodeURIComponent(filename);
80
+ }
81
+ const HyperLinkTargets = ['_self', '_blank', '_parent', '_top'];
82
+ /**
83
+ * 文件下载
84
+ * @param source 文件地址或blob对象
85
+ * @param fileName 文件名
86
+ * @param isPreview 是否
87
+ */
88
+ // `a.click()` doesn't work for all browsers (#465)
89
+ function click(node) {
90
+ try {
91
+ node.dispatchEvent(new MouseEvent('click'));
92
+ }
93
+ catch (e) {
94
+ var evt = document.createEvent('MouseEvents');
95
+ evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
96
+ node.dispatchEvent(evt);
97
+ }
98
+ }
99
+ export function download(source, fileName = '', target) {
100
+ const link = document.createElement('a');
101
+ if (typeof source === 'string') {
102
+ if (!source.length) {
103
+ if (process.env.NODE_ENV === 'development') {
104
+ console.error('下载失败,原因:source为空字符串');
105
+ return;
106
+ }
107
+ }
108
+ if (typeof target === 'string' && HyperLinkTargets.includes(target)) {
109
+ link.target = target;
110
+ }
111
+ else {
112
+ // https://stackoverflow.com/questions/33909763/download-attribute-with-a-file-name-not-working
113
+ // download 只在同源 URL 或 blob:、data: 协议起作用, 如果下载不同源的oss文件,download属性无效
114
+ link.download = fileName;
115
+ }
116
+ link.href = source;
117
+ }
118
+ let objectURL;
119
+ if (source instanceof Blob) {
120
+ const objectURL = window.URL.createObjectURL(source);
121
+ link.href = objectURL;
122
+ link.download = fileName;
123
+ }
124
+ document.body.appendChild(link);
125
+ click(link);
126
+ setTimeout(() => {
127
+ if (typeof objectURL === 'string') {
128
+ window.URL.revokeObjectURL(objectURL);
129
+ }
130
+ document.body.removeChild(link);
131
+ }, 4e4); // 40S
132
+ }
133
+ /**
134
+ * 通过创建iframe进行文件下载
135
+ * @param source
136
+ * @returns
137
+ */
138
+ export function downloadFileByIframe(source) {
139
+ const httpsPath = source.replace(/http:\/\//, "https://");
140
+ const iframes = document.getElementsByTagName("iframe");
141
+ if (iframes.length === 0 ||
142
+ (iframes.length > 0 && iframes[0].className === "fill" && iframes[iframes.length - 1].className === "fill")) {
143
+ const element = document.createElement("iframe");
144
+ element.style.display = "none";
145
+ element.src = httpsPath;
146
+ document.body.appendChild(element);
147
+ return true;
148
+ }
149
+ if (iframes.length > 0 && iframes[iframes.length - 1].className !== "fill") {
150
+ iframes[0].src = httpsPath;
151
+ return true;
152
+ }
153
+ return false;
154
+ }
package/dist/i18n.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- export declare const i18n: {
2
- readonly en: {
3
- readonly months: readonly ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
4
- readonly dayOfWeek: readonly ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
5
- };
6
- readonly zh: {
7
- readonly months: readonly ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"];
8
- readonly dayOfWeek: readonly ["日", "一", "二", "三", "四", "五", "六"];
9
- };
10
- };
1
+ export declare const i18n: {
2
+ readonly en: {
3
+ readonly months: readonly ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
4
+ readonly dayOfWeek: readonly ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
5
+ };
6
+ readonly zh: {
7
+ readonly months: readonly ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"];
8
+ readonly dayOfWeek: readonly ["日", "一", "二", "三", "四", "五", "六"];
9
+ };
10
+ };
package/dist/i18n.js CHANGED
@@ -1,12 +1,12 @@
1
- export const i18n = {
2
- en: {
3
- // English
4
- months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
5
- dayOfWeek: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
6
- },
7
- zh: {
8
- // Simplified Chinese (简体中文)
9
- months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
10
- dayOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
11
- },
12
- };
1
+ export const i18n = {
2
+ en: {
3
+ // English
4
+ months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
5
+ dayOfWeek: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
6
+ },
7
+ zh: {
8
+ // Simplified Chinese (简体中文)
9
+ months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
10
+ dayOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
11
+ },
12
+ };
package/dist/index.d.ts CHANGED
@@ -1,16 +1,16 @@
1
- export * as clipboard from './clipboard';
2
- export * as react from './react';
3
- export * as dom from './dom';
4
- export * as date from './date';
5
- export * as types from './types';
6
- export * as algorithm from './algorithm';
7
- export * as file from './file';
8
- export * as support from './support';
9
- export * as timer from './timer';
10
- export * as operator from './operator';
11
- export * as decimal from './decimal';
12
- export * as object from './object';
13
- export * as echarts from './echarts';
14
- export * as array from './array';
15
- export * as number from './number';
16
- export * as color from './color';
1
+ export * as clipboard from './clipboard';
2
+ export * as react from './react';
3
+ export * as dom from './dom';
4
+ export * as date from './date';
5
+ export * as types from './types';
6
+ export * as algorithm from './algorithm';
7
+ export * as file from './file';
8
+ export * as support from './support';
9
+ export * as timer from './timer';
10
+ export * as operator from './operator';
11
+ export * as decimal from './decimal';
12
+ export * as object from './object';
13
+ export * as echarts from './echarts';
14
+ export * as array from './array';
15
+ export * as number from './number';
16
+ export * as color from './color';
package/dist/index.js CHANGED
@@ -1,16 +1,16 @@
1
- export * as clipboard from './clipboard';
2
- export * as react from './react';
3
- export * as dom from './dom';
4
- export * as date from './date';
5
- export * as types from './types';
6
- export * as algorithm from './algorithm';
7
- export * as file from './file';
8
- export * as support from './support';
9
- export * as timer from './timer';
10
- export * as operator from './operator';
11
- export * as decimal from './decimal';
12
- export * as object from './object';
13
- export * as echarts from './echarts';
14
- export * as array from './array';
15
- export * as number from './number';
16
- export * as color from './color';
1
+ export * as clipboard from './clipboard';
2
+ export * as react from './react';
3
+ export * as dom from './dom';
4
+ export * as date from './date';
5
+ export * as types from './types';
6
+ export * as algorithm from './algorithm';
7
+ export * as file from './file';
8
+ export * as support from './support';
9
+ export * as timer from './timer';
10
+ export * as operator from './operator';
11
+ export * as decimal from './decimal';
12
+ export * as object from './object';
13
+ export * as echarts from './echarts';
14
+ export * as array from './array';
15
+ export * as number from './number';
16
+ export * as color from './color';
package/dist/number.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- /** 返回[min, max]之间的随机整数 */
2
- export declare function randomInt(min: number, max: number): number;
1
+ /** 返回[min, max]之间的随机整数 */
2
+ export declare function randomInt(min: number, max: number): number;
package/dist/number.js CHANGED
@@ -1,4 +1,4 @@
1
- /** 返回[min, max]之间的随机整数 */
2
- export function randomInt(min, max) {
3
- return Math.floor(Math.random() * (max - min + 1) + min);
4
- }
1
+ /** 返回[min, max]之间的随机整数 */
2
+ export function randomInt(min, max) {
3
+ return Math.floor(Math.random() * (max - min + 1) + min);
4
+ }
package/dist/object.d.ts CHANGED
@@ -1,13 +1,13 @@
1
- export declare const ZeroValues: ({} | null | undefined)[];
2
- /**
3
- * 移除零值的键
4
- * 默认的零值是:undefined、null、空字符串, NaN, [], {}
5
- */
6
- export declare const removeZeroValueKeys: <T extends Record<string, any>>(obj: T, zeroValues?: ({} | null | undefined)[]) => T;
7
- /**
8
- * 返回tuple,而不是string[]
9
- * const obj = { a: 1, b: '2' };
10
- * Object.keys(obj) => string[]
11
- * typedKeys({ a: 1, b: '2' }) => ('a' | 'b')[]
12
- */
13
- export declare const typedKeys: <T extends object>(obj: T) => (keyof T)[];
1
+ export declare const ZeroValues: ({} | null | undefined)[];
2
+ /**
3
+ * 移除零值的键
4
+ * 默认的零值是:undefined、null、空字符串, NaN, [], {}
5
+ */
6
+ export declare const removeZeroValueKeys: <T extends Record<string, any>>(obj: T, zeroValues?: ({} | null | undefined)[]) => T;
7
+ /**
8
+ * 返回tuple,而不是string[]
9
+ * const obj = { a: 1, b: '2' };
10
+ * Object.keys(obj) => string[]
11
+ * typedKeys({ a: 1, b: '2' }) => ('a' | 'b')[]
12
+ */
13
+ export declare const typedKeys: <T extends object>(obj: T) => (keyof T)[];
package/dist/object.js CHANGED
@@ -1,28 +1,28 @@
1
- import isEqual from 'react-fast-compare';
2
- export const ZeroValues = [undefined, null, '', NaN, [], {}];
3
- /**
4
- * 移除零值的键
5
- * 默认的零值是:undefined、null、空字符串, NaN, [], {}
6
- */
7
- // TODO: improve TS type
8
- export const removeZeroValueKeys = (obj, zeroValues = ZeroValues) => {
9
- if (!Array.isArray(zeroValues)) {
10
- throw new Error('zeroValues must be an array');
11
- }
12
- const r = {};
13
- for (const key in obj) {
14
- const value = obj[key];
15
- const shouldRemove = zeroValues.some((v) => isEqual(v, value));
16
- if (shouldRemove)
17
- continue;
18
- r[key] = value;
19
- }
20
- return r;
21
- };
22
- /**
23
- * 返回tuple,而不是string[]
24
- * const obj = { a: 1, b: '2' };
25
- * Object.keys(obj) => string[]
26
- * typedKeys({ a: 1, b: '2' }) => ('a' | 'b')[]
27
- */
28
- export const typedKeys = Object.keys;
1
+ import isEqual from 'react-fast-compare';
2
+ export const ZeroValues = [undefined, null, '', NaN, [], {}];
3
+ /**
4
+ * 移除零值的键
5
+ * 默认的零值是:undefined、null、空字符串, NaN, [], {}
6
+ */
7
+ // TODO: improve TS type
8
+ export const removeZeroValueKeys = (obj, zeroValues = ZeroValues) => {
9
+ if (!Array.isArray(zeroValues)) {
10
+ throw new Error('zeroValues must be an array');
11
+ }
12
+ const r = {};
13
+ for (const key in obj) {
14
+ const value = obj[key];
15
+ const shouldRemove = zeroValues.some((v) => isEqual(v, value));
16
+ if (shouldRemove)
17
+ continue;
18
+ r[key] = value;
19
+ }
20
+ return r;
21
+ };
22
+ /**
23
+ * 返回tuple,而不是string[]
24
+ * const obj = { a: 1, b: '2' };
25
+ * Object.keys(obj) => string[]
26
+ * typedKeys({ a: 1, b: '2' }) => ('a' | 'b')[]
27
+ */
28
+ export const typedKeys = Object.keys;
@@ -1,6 +1,6 @@
1
- /**
2
- * 检查数据类型
3
- * @param obj
4
- * @returns
5
- */
6
- export declare const trueTypeOf: (obj: unknown) => string;
1
+ /**
2
+ * 检查数据类型
3
+ * @param obj
4
+ * @returns
5
+ */
6
+ export declare const trueTypeOf: (obj: unknown) => string;
package/dist/operator.js CHANGED
@@ -1,6 +1,6 @@
1
- /**
2
- * 检查数据类型
3
- * @param obj
4
- * @returns
5
- */
6
- export const trueTypeOf = (obj) => Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
1
+ /**
2
+ * 检查数据类型
3
+ * @param obj
4
+ * @returns
5
+ */
6
+ export const trueTypeOf = (obj) => Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
@@ -1,12 +1,12 @@
1
- import React from 'react';
2
- /**
3
- * react component 的增强类
4
- */
5
- export declare class EnhancedComponent<P, S> extends React.Component<P, S> {
6
- /**
7
- * setState 方法的同步版本
8
- * @param state
9
- * @returns Promise
10
- */
11
- protected setStateAsync<K extends keyof S>(state: ((prevState: Readonly<S>, props: Readonly<P>) => Pick<S, K> | S | null) | (Pick<S, K> | S | null)): Promise<void>;
12
- }
1
+ import React from 'react';
2
+ /**
3
+ * react component 的增强类
4
+ */
5
+ export declare class EnhancedComponent<P, S> extends React.Component<P, S> {
6
+ /**
7
+ * setState 方法的同步版本
8
+ * @param state
9
+ * @returns Promise
10
+ */
11
+ protected setStateAsync<K extends keyof S>(state: ((prevState: Readonly<S>, props: Readonly<P>) => Pick<S, K> | S | null) | (Pick<S, K> | S | null)): Promise<void>;
12
+ }
@@ -1,16 +1,16 @@
1
- import React from 'react';
2
- /**
3
- * react component 的增强类
4
- */
5
- export class EnhancedComponent extends React.Component {
6
- /**
7
- * setState 方法的同步版本
8
- * @param state
9
- * @returns Promise
10
- */
11
- setStateAsync(state) {
12
- return new Promise((resolve) => {
13
- this.setState(state, resolve);
14
- });
15
- }
16
- }
1
+ import React from 'react';
2
+ /**
3
+ * react component 的增强类
4
+ */
5
+ export class EnhancedComponent extends React.Component {
6
+ /**
7
+ * setState 方法的同步版本
8
+ * @param state
9
+ * @returns Promise
10
+ */
11
+ setStateAsync(state) {
12
+ return new Promise((resolve) => {
13
+ this.setState(state, resolve);
14
+ });
15
+ }
16
+ }
@@ -1,12 +1,15 @@
1
- export * from './useDisableContextMenu';
2
- export * from './useStateCallback';
3
- export * from './renderToString';
4
- export * from './useIsMounted';
5
- export * from './useCopyToClipboard';
6
- export * from './enhancedComponent';
7
- export * from './useDeepCompareRef';
8
- export * from './types';
9
- export * from './useForwardRef';
10
- export * from './useIsomorphicLayoutEffect';
11
- export * from './useMediaQuery';
12
- export * from './useIsFirstRender';
1
+ export * from './useDisableContextMenu';
2
+ export * from './useStateCallback';
3
+ export * from './renderToString';
4
+ export * from './useIsMounted';
5
+ export * from './useCopyToClipboard';
6
+ export * from './enhancedComponent';
7
+ export * from './useDeepCompareRef';
8
+ export * from './types';
9
+ export * from './useForwardRef';
10
+ export * from './useIsomorphicLayoutEffect';
11
+ export * from './useMediaQuery';
12
+ export * from './useIsFirstRender';
13
+ export * from './useStateCallback';
14
+ export * from './useSafeTimeout';
15
+ export * from './useId';