@d-matrix/utils 1.14.0 → 1.16.0
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.
- package/dist/decimal.js +1 -1
- package/dist/echarts.d.ts +3 -0
- package/dist/echarts.js +19 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/object.d.ts +6 -0
- package/dist/object.js +21 -0
- package/package.json +10 -2
- package/readme.md +41 -0
package/dist/decimal.js
CHANGED
|
@@ -7,7 +7,7 @@ import Decimal from 'decimal.js-light';
|
|
|
7
7
|
*/
|
|
8
8
|
export function format(value, options) {
|
|
9
9
|
const { decimalPlaces = 3, suffix = '', defaultValue = '--', prefix = '', operation } = options !== null && options !== void 0 ? options : {};
|
|
10
|
-
if (value === null || value === undefined || isNaN(Number(value))) {
|
|
10
|
+
if (value === null || value === undefined || isNaN(Number(value)) || value === '') {
|
|
11
11
|
return defaultValue;
|
|
12
12
|
}
|
|
13
13
|
let decimalValue = new Decimal(value);
|
package/dist/echarts.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import deepmerge from 'deepmerge';
|
|
2
|
+
const combineMerge = (target, source, options) => {
|
|
3
|
+
const destination = target.slice();
|
|
4
|
+
source.forEach((item, index) => {
|
|
5
|
+
if (typeof destination[index] === 'undefined') {
|
|
6
|
+
destination[index] = options.cloneUnlessOtherwiseSpecified(item, options);
|
|
7
|
+
}
|
|
8
|
+
else if (options.isMergeableObject(item)) {
|
|
9
|
+
destination[index] = deepmerge(target[index], item, options);
|
|
10
|
+
}
|
|
11
|
+
else if (target.indexOf(item) === -1) {
|
|
12
|
+
destination.push(item);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
return destination;
|
|
16
|
+
};
|
|
17
|
+
export function mergeOption(defaults, overrides, option) {
|
|
18
|
+
return deepmerge(defaults, overrides, Object.assign({ arrayMerge: combineMerge }, option));
|
|
19
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/object.d.ts
ADDED
package/dist/object.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d-matrix/utils",
|
|
3
3
|
"sideEffects": false,
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.16.0",
|
|
5
5
|
"description": "A dozen of utils for Front-End Development",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc",
|
|
9
|
+
"build:public": "tsc --project ./tsconfig.e2e.json",
|
|
9
10
|
"prebuild": "npm run clean",
|
|
10
11
|
"postpublish": "echo \"wait for 3 seconds, then sync cnpm\" && npm run wait3s && npm run cnpm:sync",
|
|
11
12
|
"prepublishOnly": "npm run build",
|
|
@@ -16,7 +17,8 @@
|
|
|
16
17
|
"test:tsd": "tsd",
|
|
17
18
|
"postversion": "git push && git push --tags",
|
|
18
19
|
"wait3s": "node -e \"setTimeout(() => process.exit(0), 3000)\"",
|
|
19
|
-
"cnpm:sync": "cnpm sync %npm_package_name%"
|
|
20
|
+
"cnpm:sync": "cnpm sync %npm_package_name%",
|
|
21
|
+
"start": "serve -l 64055 -n ./public"
|
|
20
22
|
},
|
|
21
23
|
"engines": {
|
|
22
24
|
"node": ">=16.20.0"
|
|
@@ -41,20 +43,26 @@
|
|
|
41
43
|
"peerDependencies": {
|
|
42
44
|
"@types/react": "^16.8.0",
|
|
43
45
|
"@types/react-dom": "^16.9.0",
|
|
46
|
+
"echarts": "^3.0.0 || ^4.0.0 || ^5.0.0",
|
|
44
47
|
"react": "^16.8.0 || ^17.0.0",
|
|
45
48
|
"react-dom": "^16.9.0",
|
|
46
49
|
"typescript": "~4.3.2"
|
|
47
50
|
},
|
|
48
51
|
"devDependencies": {
|
|
52
|
+
"@types/chai-subset": "^1.3.5",
|
|
49
53
|
"@vitejs/plugin-react": "^4.2.1",
|
|
54
|
+
"chai-subset": "^1.6.0",
|
|
50
55
|
"cnpm": "^9.4.0",
|
|
51
56
|
"cypress": "^13.6.6",
|
|
52
57
|
"expect-type": "^0.19.0",
|
|
58
|
+
"read-excel-file": "^5.8.4",
|
|
53
59
|
"rimraf": "^5.0.5",
|
|
60
|
+
"serve": "^14.2.3",
|
|
54
61
|
"vite": "^4.5.2"
|
|
55
62
|
},
|
|
56
63
|
"dependencies": {
|
|
57
64
|
"decimal.js-light": "^2.5.1",
|
|
65
|
+
"deepmerge": "^4.3.1",
|
|
58
66
|
"react-fast-compare": "^3.2.2"
|
|
59
67
|
}
|
|
60
68
|
}
|
package/readme.md
CHANGED
|
@@ -18,6 +18,8 @@ A dozen of utils for Front-End Development
|
|
|
18
18
|
- [timer](#timer)
|
|
19
19
|
- [operator](#operator)
|
|
20
20
|
- [decimal](#decimal)
|
|
21
|
+
- [object](#object)
|
|
22
|
+
- [echarts](#echarts)
|
|
21
23
|
|
|
22
24
|
### clipboard
|
|
23
25
|
|
|
@@ -402,6 +404,8 @@ trueTypeOf(null); // null
|
|
|
402
404
|
trueTypeOf(undefined); // undefined
|
|
403
405
|
```
|
|
404
406
|
|
|
407
|
+
## decimal
|
|
408
|
+
|
|
405
409
|
- `format(value: number | string | undefined | null, options?: FormatOptions): string`
|
|
406
410
|
|
|
407
411
|
格式化数字,默认保留3位小数,可添加前缀,后缀,默认值为'--',用法见[测试](./tests//decimal.cy.ts)
|
|
@@ -419,6 +423,23 @@ type FormatOptions = {
|
|
|
419
423
|
};
|
|
420
424
|
```
|
|
421
425
|
|
|
426
|
+
## object
|
|
427
|
+
|
|
428
|
+
- `removeZeroValueKeys = <T extends Record<string, any>>(obj: T, zeroValues = ZeroValues): T`
|
|
429
|
+
|
|
430
|
+
移除零值的键, 默认的零值是:`undefined`、`null`, `''`, `NaN`, `[]`, `{}`
|
|
431
|
+
|
|
432
|
+
```ts
|
|
433
|
+
removeZeroValueKeys({ a: '', b: 'abc', c: undefined, d: null, e: NaN, f: -1, g: [], h: {} })
|
|
434
|
+
// { b: 'abc', f: -1 }
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
## echarts
|
|
438
|
+
|
|
439
|
+
- `mergeOption(defaults: EChartsOption, overrides: EChartsOption, option?: deepmerge.Options): EChartsOption`
|
|
440
|
+
|
|
441
|
+
deep merge Echarts配置,用法见[测试用例](./tests//echarts/echarts.cy.ts)
|
|
442
|
+
|
|
422
443
|
## 测试
|
|
423
444
|
|
|
424
445
|
运行全部组件测试
|
|
@@ -433,6 +454,26 @@ npm run cy:run -- --component
|
|
|
433
454
|
npm run cy:run -- --component --spec tests/date.cy.ts
|
|
434
455
|
```
|
|
435
456
|
|
|
457
|
+
运行E2E测试
|
|
458
|
+
|
|
459
|
+
将`src`通过`tsc` build到`public/dist`目录
|
|
460
|
+
|
|
461
|
+
```bash
|
|
462
|
+
npm run build:public
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
启动一个Web服务器来访问`public/index.html`文件,`dist`目录的脚本可以通过`<script type="module"/>`引入
|
|
466
|
+
|
|
467
|
+
```bash
|
|
468
|
+
npm run serve
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
最后启动cypress GUI客户端,选择E2E测试
|
|
472
|
+
|
|
473
|
+
```bash
|
|
474
|
+
npm run cy:open
|
|
475
|
+
```
|
|
476
|
+
|
|
436
477
|
## 发布
|
|
437
478
|
|
|
438
479
|
更新package version:
|