@d-matrix/utils 1.12.1 → 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/date.d.ts +16 -10
- package/dist/date.js +15 -3
- package/dist/decimal.d.ts +17 -0
- package/dist/decimal.js +23 -0
- package/dist/i18n.d.ts +10 -0
- package/dist/i18n.js +12 -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 +3 -1
- package/readme.md +127 -5
package/dist/date.d.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
label: string;
|
|
3
|
-
value: number;
|
|
4
|
-
}
|
|
1
|
+
import { i18n } from './i18n';
|
|
5
2
|
/**
|
|
6
3
|
* Generates an array of numbers representing a range of years between the start year and the end year.
|
|
7
4
|
*
|
|
@@ -10,7 +7,15 @@ export interface RecentYearOption {
|
|
|
10
7
|
* @return {number[]} an array of numbers representing the range of years
|
|
11
8
|
*/
|
|
12
9
|
export declare function rangeOfYears(start: number, end?: number): number[];
|
|
13
|
-
export declare
|
|
10
|
+
export declare enum YearOptionKind {
|
|
11
|
+
Numbers = 0,
|
|
12
|
+
Objects = 1
|
|
13
|
+
}
|
|
14
|
+
export interface YearOption {
|
|
15
|
+
label: string;
|
|
16
|
+
value: number;
|
|
17
|
+
}
|
|
18
|
+
export declare type GetYearsOptions = {
|
|
14
19
|
startYear?: number;
|
|
15
20
|
recentYears?: number;
|
|
16
21
|
endYear?: number;
|
|
@@ -20,9 +25,10 @@ export declare type GetRecentYearsOptions = {
|
|
|
20
25
|
* 获取n年, 从大到小
|
|
21
26
|
* @param options
|
|
22
27
|
*/
|
|
23
|
-
export declare function getYears(options:
|
|
24
|
-
type:
|
|
28
|
+
export declare function getYears(options: GetYearsOptions & {
|
|
29
|
+
type: YearOptionKind.Numbers;
|
|
25
30
|
}): number[];
|
|
26
|
-
export declare function getYears(options:
|
|
27
|
-
type:
|
|
28
|
-
}):
|
|
31
|
+
export declare function getYears(options: GetYearsOptions & {
|
|
32
|
+
type: YearOptionKind.Objects;
|
|
33
|
+
}): YearOption[];
|
|
34
|
+
export declare const dayOfWeek: (num: number, lang?: keyof typeof i18n) => string;
|
package/dist/date.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { i18n } from './i18n';
|
|
1
2
|
/**
|
|
2
3
|
* Generates an array of numbers representing a range of years between the start year and the end year.
|
|
3
4
|
*
|
|
@@ -10,6 +11,11 @@ export function rangeOfYears(start, end = new Date().getFullYear()) {
|
|
|
10
11
|
.fill(start)
|
|
11
12
|
.map((year, index) => year + index);
|
|
12
13
|
}
|
|
14
|
+
export var YearOptionKind;
|
|
15
|
+
(function (YearOptionKind) {
|
|
16
|
+
YearOptionKind[YearOptionKind["Numbers"] = 0] = "Numbers";
|
|
17
|
+
YearOptionKind[YearOptionKind["Objects"] = 1] = "Objects";
|
|
18
|
+
})(YearOptionKind || (YearOptionKind = {}));
|
|
13
19
|
export function getYears(options) {
|
|
14
20
|
const { recentYears = 0, startYear, endYear, suffix = '年', type } = options;
|
|
15
21
|
const endY = endYear ? endYear : new Date().getFullYear();
|
|
@@ -29,14 +35,14 @@ export function getYears(options) {
|
|
|
29
35
|
return [];
|
|
30
36
|
}
|
|
31
37
|
}
|
|
32
|
-
if (type ===
|
|
38
|
+
if (type === YearOptionKind.Numbers) {
|
|
33
39
|
const result = [];
|
|
34
40
|
for (let i = 0; i < ranges; i++) {
|
|
35
41
|
result.push(endY - i);
|
|
36
42
|
}
|
|
37
43
|
return result;
|
|
38
44
|
}
|
|
39
|
-
if (type ===
|
|
45
|
+
if (type === YearOptionKind.Objects) {
|
|
40
46
|
const result = [];
|
|
41
47
|
for (let i = 0; i < ranges; i++) {
|
|
42
48
|
result.push({
|
|
@@ -46,5 +52,11 @@ export function getYears(options) {
|
|
|
46
52
|
}
|
|
47
53
|
return result;
|
|
48
54
|
}
|
|
49
|
-
throw new Error('type must be
|
|
55
|
+
throw new Error('type must be enum: YearOptionKind.Numbers or YearOptionKind.Objects');
|
|
50
56
|
}
|
|
57
|
+
export const dayOfWeek = (num, lang = 'zh') => {
|
|
58
|
+
if (!Number.isInteger(num)) {
|
|
59
|
+
throw new Error('请输入一个整数');
|
|
60
|
+
}
|
|
61
|
+
return i18n[lang].dayOfWeek[num % 7];
|
|
62
|
+
};
|
|
@@ -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/i18n.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const i18n: {
|
|
2
|
+
readonly en: {
|
|
3
|
+
readonly months: readonly ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
|
4
|
+
readonly dayOfWeek: readonly ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
5
|
+
};
|
|
6
|
+
readonly zh: {
|
|
7
|
+
readonly months: readonly ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"];
|
|
8
|
+
readonly dayOfWeek: readonly ["日", "一", "二", "三", "四", "五", "六"];
|
|
9
|
+
};
|
|
10
|
+
};
|
package/dist/i18n.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const i18n = {
|
|
2
|
+
en: {
|
|
3
|
+
// English
|
|
4
|
+
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
|
|
5
|
+
dayOfWeek: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
|
|
6
|
+
},
|
|
7
|
+
zh: {
|
|
8
|
+
// Simplified Chinese (简体中文)
|
|
9
|
+
months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
|
|
10
|
+
dayOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
|
|
11
|
+
},
|
|
12
|
+
};
|
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,13 +1,14 @@
|
|
|
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": {
|
|
8
8
|
"build": "tsc",
|
|
9
9
|
"prebuild": "npm run clean",
|
|
10
10
|
"postpublish": "echo \"wait for 3 seconds, then sync cnpm\" && npm run wait3s && npm run cnpm:sync",
|
|
11
|
+
"prepublishOnly": "npm run build",
|
|
11
12
|
"clean": "rimraf dist",
|
|
12
13
|
"cy:open": "cypress open",
|
|
13
14
|
"cy:component": "cypress run --component --spec",
|
|
@@ -53,6 +54,7 @@
|
|
|
53
54
|
"vite": "^4.5.2"
|
|
54
55
|
},
|
|
55
56
|
"dependencies": {
|
|
57
|
+
"decimal.js-light": "^2.5.1",
|
|
56
58
|
"react-fast-compare": "^3.2.2"
|
|
57
59
|
}
|
|
58
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`
|
|
@@ -150,7 +187,17 @@ dom.strip('测试<em>高亮</em>测试'); // '测试高亮测试'
|
|
|
150
187
|
- `getYears()`
|
|
151
188
|
|
|
152
189
|
```ts
|
|
153
|
-
export
|
|
190
|
+
export interface YearOption {
|
|
191
|
+
label: string;
|
|
192
|
+
value: number;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export enum YearOptionKind {
|
|
196
|
+
Numbers,
|
|
197
|
+
Objects,
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export type GetYearsOptions = {
|
|
154
201
|
// 开始年份
|
|
155
202
|
startYear?: number;
|
|
156
203
|
// 最近几年
|
|
@@ -161,12 +208,12 @@ export type GetRecentYearsOptions = {
|
|
|
161
208
|
suffix?: string;
|
|
162
209
|
};
|
|
163
210
|
|
|
164
|
-
export function getYears(options:
|
|
165
|
-
export function getYears(options:
|
|
166
|
-
export function getYears(options:
|
|
211
|
+
export function getYears(options: GetYearsOptions & { type: YearOptionKind.Numbers }): number[];
|
|
212
|
+
export function getYears(options: GetYearsOptions & { type: YearOptionKind.Objects }): YearOption[];
|
|
213
|
+
export function getYears(options: GetYearsOptions & { type: YearOptionKind }): number[] | YearOption[]
|
|
167
214
|
```
|
|
168
215
|
|
|
169
|
-
获取n年,`type`传`
|
|
216
|
+
获取n年,`type`传`YearOptionKind.Numbers`,返回`[2023, 2022, 2021]`数字数组;`type`传`YearOptionKind.Objects`,返回如下的对象数组
|
|
170
217
|
|
|
171
218
|
```sh
|
|
172
219
|
[
|
|
@@ -178,6 +225,14 @@ export function getYears(options: GetRecentYearsOptions & { type: 'object[]' | '
|
|
|
178
225
|
|
|
179
226
|
更多用法,见[测试用例](./tests/date.cy.ts)
|
|
180
227
|
|
|
228
|
+
- `dayOfWeek(num: number, lang: keyof typeof i18n = 'zh'): string`
|
|
229
|
+
|
|
230
|
+
返回星期几, `lang`仅支持`zh`和`en`, `num`必须为正整数,否则报错
|
|
231
|
+
|
|
232
|
+
```js
|
|
233
|
+
dayOfWeek(0) // "日"
|
|
234
|
+
```
|
|
235
|
+
|
|
181
236
|
### types
|
|
182
237
|
|
|
183
238
|
- `WithOptional<T, K extends keyof T>`
|
|
@@ -187,6 +242,50 @@ type A = { a: number; b: number; c: number; };
|
|
|
187
242
|
type T0 = WithOptional<A, 'b' | 'c'>; // { a: number; b?: number; c?: number }
|
|
188
243
|
```
|
|
189
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
|
+
|
|
190
289
|
### algorithm
|
|
191
290
|
|
|
192
291
|
- `moveMulti<T extends unknown>(arr: T[], indexes: number[], start: number): T[]`
|
|
@@ -303,6 +402,23 @@ trueTypeOf(null); // null
|
|
|
303
402
|
trueTypeOf(undefined); // undefined
|
|
304
403
|
```
|
|
305
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
|
+
|
|
306
422
|
## 测试
|
|
307
423
|
|
|
308
424
|
运行全部组件测试
|
|
@@ -337,6 +453,12 @@ npm build
|
|
|
337
453
|
npm publish --access public
|
|
338
454
|
```
|
|
339
455
|
|
|
456
|
+
网络原因导致连接registry服务器超时,可指定proxy
|
|
457
|
+
|
|
458
|
+
```bash
|
|
459
|
+
npm --proxy http://127.0.0.1:7890 publish
|
|
460
|
+
```
|
|
461
|
+
|
|
340
462
|
镜像站查询版本与手动同步:
|
|
341
463
|
|
|
342
464
|
[npm镜像站](https://npmmirror.com/package/@d-matrix/utils)
|