@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 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
@@ -9,3 +9,4 @@ export * as support from './support';
9
9
  export * as timer from './timer';
10
10
  export * as operator from './operator';
11
11
  export * as decimal from './decimal';
12
+ export * as object from './object';
package/dist/index.js CHANGED
@@ -9,3 +9,4 @@ export * as support from './support';
9
9
  export * as timer from './timer';
10
10
  export * as operator from './operator';
11
11
  export * as decimal from './decimal';
12
+ export * as object from './object';
@@ -0,0 +1,6 @@
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;
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,7 +1,7 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
3
  "sideEffects": false,
4
- "version": "1.14.0",
4
+ "version": "1.15.0",
5
5
  "description": "A dozen of utils for Front-End Development",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {
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
  运行全部组件测试