@d-matrix/utils 1.13.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.
@@ -0,0 +1,17 @@
1
+ export declare type FormatOptions = {
2
+ decimalPlaces?: number | false;
3
+ suffix?: string;
4
+ prefix?: string;
5
+ defaultValue?: string;
6
+ operation?: {
7
+ operator: 'add' | 'sub' | 'mul' | 'div' | 'toDecimalPlaces';
8
+ value: number;
9
+ }[];
10
+ };
11
+ /**
12
+ * 格式化数字,默认保留3位小数,可添加前缀,后缀,默认值为'--'
13
+ * @param value
14
+ * @param options
15
+ * @returns
16
+ */
17
+ export declare function format(value: number | string | undefined | null, options?: FormatOptions): string;
@@ -0,0 +1,23 @@
1
+ import Decimal from 'decimal.js-light';
2
+ /**
3
+ * 格式化数字,默认保留3位小数,可添加前缀,后缀,默认值为'--'
4
+ * @param value
5
+ * @param options
6
+ * @returns
7
+ */
8
+ export function format(value, options) {
9
+ const { decimalPlaces = 3, suffix = '', defaultValue = '--', prefix = '', operation } = options !== null && options !== void 0 ? options : {};
10
+ if (value === null || value === undefined || isNaN(Number(value)) || value === '') {
11
+ return defaultValue;
12
+ }
13
+ let decimalValue = new Decimal(value);
14
+ if (Array.isArray(operation) && operation.length > 0) {
15
+ operation.forEach((item) => {
16
+ decimalValue = decimalValue[item.operator](item.value);
17
+ });
18
+ }
19
+ if (decimalPlaces == false) {
20
+ return prefix + decimalValue.toString() + suffix;
21
+ }
22
+ return prefix + decimalValue.toFixed(decimalPlaces) + suffix;
23
+ }
package/dist/index.d.ts CHANGED
@@ -8,3 +8,5 @@ export * as file from './file';
8
8
  export * as support from './support';
9
9
  export * as timer from './timer';
10
10
  export * as operator from './operator';
11
+ export * as decimal from './decimal';
12
+ export * as object from './object';
package/dist/index.js CHANGED
@@ -8,3 +8,5 @@ export * as file from './file';
8
8
  export * as support from './support';
9
9
  export * as timer from './timer';
10
10
  export * as operator from './operator';
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
+ };
@@ -0,0 +1,2 @@
1
+ import type { ForwardRefExoticComponent } from 'react';
2
+ export declare type InferRef<T> = T extends ForwardRefExoticComponent<infer Ref> ? Ref extends React.RefAttributes<infer RefElement> ? RefElement : never : never;
@@ -0,0 +1 @@
1
+ export {};
package/dist/types.d.ts CHANGED
@@ -5,3 +5,11 @@ export declare type ExcludePickPartial<T, K extends keyof T> = Partial<Omit<T, K
5
5
  export declare type Undefinable<T> = {
6
6
  [K in keyof T]: T[K] | undefined;
7
7
  };
8
+ /** 获取对象中的方法名称,返回union type */
9
+ export declare type FunctionPropertyNames<T> = {
10
+ [P in keyof T]-?: T[P] extends Function ? P : never;
11
+ }[keyof T];
12
+ /** 获取对象中非函数属性名称,返回union type */
13
+ export declare type NonFunctionPropertyNames<T> = {
14
+ [P in keyof T]-?: T[P] extends Function ? never : P;
15
+ }[keyof T];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
3
  "sideEffects": false,
4
- "version": "1.13.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": {
@@ -54,6 +54,7 @@
54
54
  "vite": "^4.5.2"
55
55
  },
56
56
  "dependencies": {
57
+ "decimal.js-light": "^2.5.1",
57
58
  "react-fast-compare": "^3.2.2"
58
59
  }
59
60
  }
package/readme.md CHANGED
@@ -17,6 +17,8 @@ A dozen of utils for Front-End Development
17
17
  - [support](#support)
18
18
  - [timer](#timer)
19
19
  - [operator](#operator)
20
+ - [decimal](#decimal)
21
+ - [object](#object)
20
22
 
21
23
  ### clipboard
22
24
 
@@ -125,6 +127,42 @@ import { react } from '@d-matrix/utils';
125
127
 
126
128
  深比较`deps`。返回`ref`,`ref.current`是一个自增数字,每次`deps`变化,`ref.current`加`1`。用法见[测试](./tests/react.cy.tsx)
127
129
 
130
+ - `InferRef<T>`
131
+
132
+ 推导子组件的`ref`类型,适用于组件没有导出其`ref`类型的场景, 更多用法见[测试](./tests/react-types.tsx)
133
+
134
+ ```tsx
135
+ interface ChildRefProps {
136
+ prop1: () => void;
137
+ prop2: () => void;
138
+ }
139
+
140
+ interface ChildProps {
141
+ otherProp: string;
142
+ }
143
+
144
+ const Child = React.forwardRef<ChildRefProps, ChildProps>((props, ref) => {
145
+ React.useImperativeHandle(
146
+ ref,
147
+ () => ({
148
+ prop1() {},
149
+ prop2() {},
150
+ }),
151
+ [],
152
+ );
153
+
154
+ return null;
155
+ });
156
+
157
+ type InferredChildRef = InferRef<typeof Child>; // 等价于ChildRefProps
158
+
159
+ const Parent = () => {
160
+ const childRef = React.useRef<InferredChildRef>(null);
161
+
162
+ return <Child ref={childRef} otherProp="a" />;
163
+ };
164
+ ```
165
+
128
166
  ### dom
129
167
 
130
168
  - `scrollToTop(element: Element | null | undefined): void`
@@ -205,6 +243,50 @@ type A = { a: number; b: number; c: number; };
205
243
  type T0 = WithOptional<A, 'b' | 'c'>; // { a: number; b?: number; c?: number }
206
244
  ```
207
245
 
246
+ - `FunctionPropertyNames<T>`
247
+
248
+ 获取对象中的方法名称,返回union type
249
+
250
+ ```ts
251
+ class A {
252
+ add() {}
253
+ minus() {}
254
+ div() {}
255
+ public result: number = 0;
256
+ }
257
+ type T0 = FunctionPropertyNames<A>; // 'add' | 'minus' | 'div'
258
+
259
+ const t1 = {
260
+ add() {},
261
+ minus() {},
262
+ div() {},
263
+ result: 0,
264
+ };
265
+ type T1 = FunctionPropertyNames<typeof t1>; // 'add' | 'minus' | 'div'
266
+ ```
267
+
268
+ - `NonFunctionPropertyNames<T>`
269
+
270
+ 获取对象中非函数属性名称,返回union type
271
+
272
+ ```ts
273
+ class A {
274
+ add() {}
275
+ minus() {}
276
+ div() {}
277
+ public result: number = 0;
278
+ }
279
+ type T0 = FunctionPropertyNames<A>; // 'result'
280
+
281
+ const t1 = {
282
+ add() {},
283
+ minus() {},
284
+ div() {},
285
+ result: 0,
286
+ };
287
+ type T1 = FunctionPropertyNames<typeof t1>; // 'result'
288
+ ```
289
+
208
290
  ### algorithm
209
291
 
210
292
  - `moveMulti<T extends unknown>(arr: T[], indexes: number[], start: number): T[]`
@@ -321,6 +403,36 @@ trueTypeOf(null); // null
321
403
  trueTypeOf(undefined); // undefined
322
404
  ```
323
405
 
406
+ ## decimal
407
+
408
+ - `format(value: number | string | undefined | null, options?: FormatOptions): string`
409
+
410
+ 格式化数字,默认保留3位小数,可添加前缀,后缀,默认值为'--',用法见[测试](./tests//decimal.cy.ts)
411
+
412
+ ```ts
413
+ type FormatOptions = {
414
+ decimalPlaces?: number | false;
415
+ suffix?: string;
416
+ prefix?: string;
417
+ defaultValue?: string;
418
+ operation?: {
419
+ operator: 'add' | 'sub' | 'mul' | 'div' | 'toDecimalPlaces';
420
+ value: number;
421
+ }[];
422
+ };
423
+ ```
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
+
324
436
  ## 测试
325
437
 
326
438
  运行全部组件测试
@@ -355,6 +467,12 @@ npm build
355
467
  npm publish --access public
356
468
  ```
357
469
 
470
+ 网络原因导致连接registry服务器超时,可指定proxy
471
+
472
+ ```bash
473
+ npm --proxy http://127.0.0.1:7890 publish
474
+ ```
475
+
358
476
  镜像站查询版本与手动同步:
359
477
 
360
478
  [npm镜像站](https://npmmirror.com/package/@d-matrix/utils)