@d-matrix/utils 1.14.0 → 1.15.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/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 +1 -1
- package/readme.md +14 -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/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
package/readme.md
CHANGED
|
@@ -18,6 +18,7 @@ A dozen of utils for Front-End Development
|
|
|
18
18
|
- [timer](#timer)
|
|
19
19
|
- [operator](#operator)
|
|
20
20
|
- [decimal](#decimal)
|
|
21
|
+
- [object](#object)
|
|
21
22
|
|
|
22
23
|
### clipboard
|
|
23
24
|
|
|
@@ -402,6 +403,8 @@ trueTypeOf(null); // null
|
|
|
402
403
|
trueTypeOf(undefined); // undefined
|
|
403
404
|
```
|
|
404
405
|
|
|
406
|
+
## decimal
|
|
407
|
+
|
|
405
408
|
- `format(value: number | string | undefined | null, options?: FormatOptions): string`
|
|
406
409
|
|
|
407
410
|
格式化数字,默认保留3位小数,可添加前缀,后缀,默认值为'--',用法见[测试](./tests//decimal.cy.ts)
|
|
@@ -419,6 +422,17 @@ type FormatOptions = {
|
|
|
419
422
|
};
|
|
420
423
|
```
|
|
421
424
|
|
|
425
|
+
## object
|
|
426
|
+
|
|
427
|
+
- `removeZeroValueKeys = <T extends Record<string, any>>(obj: T, zeroValues = ZeroValues): T`
|
|
428
|
+
|
|
429
|
+
移除零值的键, 默认的零值是:`undefined`、`null`, `''`, `NaN`, `[]`, `{}`
|
|
430
|
+
|
|
431
|
+
```ts
|
|
432
|
+
removeZeroValueKeys({ a: '', b: 'abc', c: undefined, d: null, e: NaN, f: -1, g: [], h: {} })
|
|
433
|
+
// { b: 'abc', f: -1 }
|
|
434
|
+
```
|
|
435
|
+
|
|
422
436
|
## 测试
|
|
423
437
|
|
|
424
438
|
运行全部组件测试
|