@d-matrix/utils 1.13.0 → 1.14.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.d.ts +17 -0
- package/dist/decimal.js +23 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/react/types.d.ts +2 -0
- package/dist/react/types.js +1 -0
- package/dist/types.d.ts +8 -0
- package/package.json +2 -1
- package/readme.md +104 -0
|
@@ -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;
|
package/dist/decimal.js
ADDED
|
@@ -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))) {
|
|
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
package/dist/index.js
CHANGED
|
@@ -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.
|
|
4
|
+
"version": "1.14.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,7 @@ A dozen of utils for Front-End Development
|
|
|
17
17
|
- [support](#support)
|
|
18
18
|
- [timer](#timer)
|
|
19
19
|
- [operator](#operator)
|
|
20
|
+
- [decimal](#decimal)
|
|
20
21
|
|
|
21
22
|
### clipboard
|
|
22
23
|
|
|
@@ -125,6 +126,42 @@ import { react } from '@d-matrix/utils';
|
|
|
125
126
|
|
|
126
127
|
深比较`deps`。返回`ref`,`ref.current`是一个自增数字,每次`deps`变化,`ref.current`加`1`。用法见[测试](./tests/react.cy.tsx)
|
|
127
128
|
|
|
129
|
+
- `InferRef<T>`
|
|
130
|
+
|
|
131
|
+
推导子组件的`ref`类型,适用于组件没有导出其`ref`类型的场景, 更多用法见[测试](./tests/react-types.tsx)
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
interface ChildRefProps {
|
|
135
|
+
prop1: () => void;
|
|
136
|
+
prop2: () => void;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
interface ChildProps {
|
|
140
|
+
otherProp: string;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const Child = React.forwardRef<ChildRefProps, ChildProps>((props, ref) => {
|
|
144
|
+
React.useImperativeHandle(
|
|
145
|
+
ref,
|
|
146
|
+
() => ({
|
|
147
|
+
prop1() {},
|
|
148
|
+
prop2() {},
|
|
149
|
+
}),
|
|
150
|
+
[],
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
return null;
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
type InferredChildRef = InferRef<typeof Child>; // 等价于ChildRefProps
|
|
157
|
+
|
|
158
|
+
const Parent = () => {
|
|
159
|
+
const childRef = React.useRef<InferredChildRef>(null);
|
|
160
|
+
|
|
161
|
+
return <Child ref={childRef} otherProp="a" />;
|
|
162
|
+
};
|
|
163
|
+
```
|
|
164
|
+
|
|
128
165
|
### dom
|
|
129
166
|
|
|
130
167
|
- `scrollToTop(element: Element | null | undefined): void`
|
|
@@ -205,6 +242,50 @@ type A = { a: number; b: number; c: number; };
|
|
|
205
242
|
type T0 = WithOptional<A, 'b' | 'c'>; // { a: number; b?: number; c?: number }
|
|
206
243
|
```
|
|
207
244
|
|
|
245
|
+
- `FunctionPropertyNames<T>`
|
|
246
|
+
|
|
247
|
+
获取对象中的方法名称,返回union type
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
class A {
|
|
251
|
+
add() {}
|
|
252
|
+
minus() {}
|
|
253
|
+
div() {}
|
|
254
|
+
public result: number = 0;
|
|
255
|
+
}
|
|
256
|
+
type T0 = FunctionPropertyNames<A>; // 'add' | 'minus' | 'div'
|
|
257
|
+
|
|
258
|
+
const t1 = {
|
|
259
|
+
add() {},
|
|
260
|
+
minus() {},
|
|
261
|
+
div() {},
|
|
262
|
+
result: 0,
|
|
263
|
+
};
|
|
264
|
+
type T1 = FunctionPropertyNames<typeof t1>; // 'add' | 'minus' | 'div'
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
- `NonFunctionPropertyNames<T>`
|
|
268
|
+
|
|
269
|
+
获取对象中非函数属性名称,返回union type
|
|
270
|
+
|
|
271
|
+
```ts
|
|
272
|
+
class A {
|
|
273
|
+
add() {}
|
|
274
|
+
minus() {}
|
|
275
|
+
div() {}
|
|
276
|
+
public result: number = 0;
|
|
277
|
+
}
|
|
278
|
+
type T0 = FunctionPropertyNames<A>; // 'result'
|
|
279
|
+
|
|
280
|
+
const t1 = {
|
|
281
|
+
add() {},
|
|
282
|
+
minus() {},
|
|
283
|
+
div() {},
|
|
284
|
+
result: 0,
|
|
285
|
+
};
|
|
286
|
+
type T1 = FunctionPropertyNames<typeof t1>; // 'result'
|
|
287
|
+
```
|
|
288
|
+
|
|
208
289
|
### algorithm
|
|
209
290
|
|
|
210
291
|
- `moveMulti<T extends unknown>(arr: T[], indexes: number[], start: number): T[]`
|
|
@@ -321,6 +402,23 @@ trueTypeOf(null); // null
|
|
|
321
402
|
trueTypeOf(undefined); // undefined
|
|
322
403
|
```
|
|
323
404
|
|
|
405
|
+
- `format(value: number | string | undefined | null, options?: FormatOptions): string`
|
|
406
|
+
|
|
407
|
+
格式化数字,默认保留3位小数,可添加前缀,后缀,默认值为'--',用法见[测试](./tests//decimal.cy.ts)
|
|
408
|
+
|
|
409
|
+
```ts
|
|
410
|
+
type FormatOptions = {
|
|
411
|
+
decimalPlaces?: number | false;
|
|
412
|
+
suffix?: string;
|
|
413
|
+
prefix?: string;
|
|
414
|
+
defaultValue?: string;
|
|
415
|
+
operation?: {
|
|
416
|
+
operator: 'add' | 'sub' | 'mul' | 'div' | 'toDecimalPlaces';
|
|
417
|
+
value: number;
|
|
418
|
+
}[];
|
|
419
|
+
};
|
|
420
|
+
```
|
|
421
|
+
|
|
324
422
|
## 测试
|
|
325
423
|
|
|
326
424
|
运行全部组件测试
|
|
@@ -355,6 +453,12 @@ npm build
|
|
|
355
453
|
npm publish --access public
|
|
356
454
|
```
|
|
357
455
|
|
|
456
|
+
网络原因导致连接registry服务器超时,可指定proxy
|
|
457
|
+
|
|
458
|
+
```bash
|
|
459
|
+
npm --proxy http://127.0.0.1:7890 publish
|
|
460
|
+
```
|
|
461
|
+
|
|
358
462
|
镜像站查询版本与手动同步:
|
|
359
463
|
|
|
360
464
|
[npm镜像站](https://npmmirror.com/package/@d-matrix/utils)
|