@dune2/tools 1.0.5 → 1.1.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/package.json +5 -6
- package/src/factory/createStateContext.tsx +5 -1
- package/src/factory/fieldsMap.tsx +28 -0
- package/src/numbro/index.ts +1 -65
- package/src/numbro/shared.ts +0 -25
- package/src/rq/RequestBuilder.react-server.ts +1 -1
- package/src/rq/RequestBuilder.ts +2 -2
- package/src/rq/createApi.ts +1 -1
- package/src/numbro/currencies.ts +0 -23
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dune2/tools",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n"
|
|
@@ -33,17 +33,16 @@
|
|
|
33
33
|
"src"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
+
"@tanstack/react-query": "^5",
|
|
36
37
|
"bignumber.js": "^9.1.2",
|
|
37
38
|
"es-toolkit": "^1",
|
|
38
39
|
"js-cookie": "^3.0.5",
|
|
39
|
-
"store2": "^2.14.3"
|
|
40
|
+
"store2": "^2.14.3",
|
|
41
|
+
"valtio": "^2"
|
|
40
42
|
},
|
|
41
43
|
"devDependencies": {
|
|
42
|
-
"@tanstack/react-query": "5.51.11",
|
|
43
44
|
"@types/js-cookie": "3.0.3",
|
|
44
|
-
"
|
|
45
|
-
"react": "^19",
|
|
46
|
-
"valtio": "^2"
|
|
45
|
+
"react": "^19"
|
|
47
46
|
},
|
|
48
47
|
"publishConfig": {
|
|
49
48
|
"access": "public"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import type { ComponentType, FC, PropsWithChildren } from "react";
|
|
3
|
-
import React, { useContext } from "react";
|
|
3
|
+
import React, { use, useContext } from "react";
|
|
4
4
|
|
|
5
5
|
interface Params<P, T> {
|
|
6
6
|
/**
|
|
@@ -52,5 +52,9 @@ export function createStateContext<P, T>(params: Params<P, T>) {
|
|
|
52
52
|
Provider,
|
|
53
53
|
withProvider,
|
|
54
54
|
Context,
|
|
55
|
+
|
|
56
|
+
use: () => {
|
|
57
|
+
return use(Context);
|
|
58
|
+
},
|
|
55
59
|
};
|
|
56
60
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fieldsMap 是一个通过 Proxy 实现的工具,
|
|
3
|
+
* 用于在访问任意属性时返回属性名的字符串形式。
|
|
4
|
+
*
|
|
5
|
+
* 使用场景:
|
|
6
|
+
* - 在字段映射、动态生成键值等需要自动获取属性名的场景中,避免手动硬编码。
|
|
7
|
+
*/
|
|
8
|
+
export const fieldsMap = new Proxy(
|
|
9
|
+
// 目标对象,这里使用一个空对象作为代理的基础
|
|
10
|
+
{},
|
|
11
|
+
{
|
|
12
|
+
/**
|
|
13
|
+
* 拦截对象的属性读取操作,并返回属性名的字符串形式。
|
|
14
|
+
*
|
|
15
|
+
* @param target 原始被代理的对象(此处为空对象)
|
|
16
|
+
* @param p 被访问的属性名(可能是 string 或 symbol)
|
|
17
|
+
* @param receiver Proxy 接收者
|
|
18
|
+
* @returns 属性名的字符串表示
|
|
19
|
+
*/
|
|
20
|
+
get(target, p, receiver) {
|
|
21
|
+
return String(p);
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
) as any;
|
|
25
|
+
|
|
26
|
+
export type FieldsMap<T extends Record<string, any>> = {
|
|
27
|
+
[K in keyof T]: K;
|
|
28
|
+
};
|
package/src/numbro/index.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import BigNumber from "bignumber.js";
|
|
2
|
-
import {
|
|
3
|
-
import { defaultCurrencies } from "./currencies";
|
|
4
|
-
import type { CurrencyFormat, Format } from "./shared";
|
|
2
|
+
import type { Format } from "./shared";
|
|
5
3
|
import { RoundingMode } from "./shared";
|
|
6
4
|
|
|
7
5
|
export * from "./shared";
|
|
@@ -161,68 +159,6 @@ export class Numbro {
|
|
|
161
159
|
}
|
|
162
160
|
//#endregion
|
|
163
161
|
|
|
164
|
-
//#region currency format
|
|
165
|
-
/**
|
|
166
|
-
* currency format default
|
|
167
|
-
*/
|
|
168
|
-
static locale: string = LocalesEnum.id;
|
|
169
|
-
static setLocale(locale: string) {
|
|
170
|
-
// check locale is valid
|
|
171
|
-
if (!(locale in Numbro.defaultCurrencies)) {
|
|
172
|
-
throw new Error(`在 defaultCurrencies 中,找不到 ${locale},请先检查`);
|
|
173
|
-
}
|
|
174
|
-
Numbro.locale = locale;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* 默认的货币格式化配置
|
|
179
|
-
* key: locale
|
|
180
|
-
* value: currency format
|
|
181
|
-
*/
|
|
182
|
-
static defaultCurrencies = defaultCurrencies;
|
|
183
|
-
static setDefaultCurrencies(currencies: typeof defaultCurrencies) {
|
|
184
|
-
Numbro.defaultCurrencies = currencies;
|
|
185
|
-
}
|
|
186
|
-
formatCurrency(format: CurrencyFormat = {}) {
|
|
187
|
-
// 根据语言解析出来的默认格式
|
|
188
|
-
const defaultCurrencyFormat =
|
|
189
|
-
Numbro.defaultCurrencies[format.locale ?? Numbro.locale];
|
|
190
|
-
|
|
191
|
-
format = {
|
|
192
|
-
...defaultCurrencyFormat,
|
|
193
|
-
...format,
|
|
194
|
-
};
|
|
195
|
-
let {
|
|
196
|
-
position = "prefix",
|
|
197
|
-
symbol,
|
|
198
|
-
spaceSeparated,
|
|
199
|
-
|
|
200
|
-
...rest
|
|
201
|
-
} = format;
|
|
202
|
-
// 是否强制显示正负号
|
|
203
|
-
const sign = this.getPrefixSign(rest.forceSign);
|
|
204
|
-
|
|
205
|
-
let space = spaceSeparated ? " " : "";
|
|
206
|
-
// 在 currency format 中,需要使用绝对值来格式化
|
|
207
|
-
// 方便后续添加 正负号
|
|
208
|
-
rest.absoluteValue = true;
|
|
209
|
-
rest.forceSign = false;
|
|
210
|
-
let formattedString = this.format(rest);
|
|
211
|
-
|
|
212
|
-
//#region NaN 的 fallback
|
|
213
|
-
const { NaNFormat } = this.combineFormatOptions(rest);
|
|
214
|
-
if (formattedString === NaNFormat) {
|
|
215
|
-
return formattedString;
|
|
216
|
-
}
|
|
217
|
-
//#endregion
|
|
218
|
-
|
|
219
|
-
if (position === "prefix") {
|
|
220
|
-
return `${sign}${symbol}${space}${formattedString}`;
|
|
221
|
-
}
|
|
222
|
-
return `${sign}${formattedString}${space}${symbol}`;
|
|
223
|
-
}
|
|
224
|
-
//#endregion
|
|
225
|
-
|
|
226
162
|
private computeAverage(num: BigNumber) {
|
|
227
163
|
const powers = {
|
|
228
164
|
// 1t
|
package/src/numbro/shared.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import BigNumber from "bignumber.js";
|
|
2
|
-
import { LocalesEnum } from "../i18n/enums";
|
|
3
2
|
|
|
4
3
|
export interface Format extends BigNumber.Format {
|
|
5
4
|
output?: "percent";
|
|
@@ -73,30 +72,6 @@ export interface Format extends BigNumber.Format {
|
|
|
73
72
|
absoluteValue?: boolean;
|
|
74
73
|
}
|
|
75
74
|
|
|
76
|
-
export interface CurrencyFormat extends Format {
|
|
77
|
-
/**
|
|
78
|
-
* 货币符号
|
|
79
|
-
*/
|
|
80
|
-
symbol?: string;
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* 货币符号位置
|
|
84
|
-
*/
|
|
85
|
-
position?: "prefix" | "postfix";
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* 按哪种语言格式化,会根据传入的语言去 currencies 里获取对应的配置项
|
|
89
|
-
* 一般用在以下情况:
|
|
90
|
-
* - 当前设置的语言是 en,但是需要格式化成 id 的货币
|
|
91
|
-
*/
|
|
92
|
-
locale?: LocalesEnum | (string & {});
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* symbol 和 数字 之间是否需要空格
|
|
96
|
-
*/
|
|
97
|
-
spaceSeparated?: boolean;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
75
|
/**
|
|
101
76
|
* 重新导出 BigNumber.RoundingMode
|
|
102
77
|
* 由于项目不会使用太多的四舍五入模式,所以只导出常用的几个
|
|
@@ -46,7 +46,7 @@ export class RequestBuilder<Req = any, Res = any> {
|
|
|
46
46
|
* useQuery、useMutation 内部会调用这个
|
|
47
47
|
* 另外也可以直接调用这个函数来发送请求
|
|
48
48
|
* @param params 请求参数 默认会根据请求方法来放到 url 上或者 body 里
|
|
49
|
-
* @param config
|
|
49
|
+
* @param config 请求的配置,一般不需要传,内部用
|
|
50
50
|
*/
|
|
51
51
|
request<P extends Req, T = Res>(params?: P, config?: RequestConfig) {
|
|
52
52
|
const method = this.options.method!;
|
package/src/rq/RequestBuilder.ts
CHANGED
|
@@ -77,7 +77,7 @@ export class RequestBuilder<Req = any, Res = any> {
|
|
|
77
77
|
* useQuery、useMutation 内部会调用这个
|
|
78
78
|
* 另外也可以直接调用这个函数来发送请求
|
|
79
79
|
* @param params 请求参数 默认会根据请求方法来放到 url 上或者 body 里
|
|
80
|
-
* @param config
|
|
80
|
+
* @param config 请求的配置,一般不需要传,内部用
|
|
81
81
|
*/
|
|
82
82
|
request<P extends Req, T = Res>(params?: P, config?: RequestConfig) {
|
|
83
83
|
const method = this.options.method!;
|
|
@@ -149,7 +149,7 @@ export class RequestBuilder<Req = any, Res = any> {
|
|
|
149
149
|
return this.request(queryKey[2], {
|
|
150
150
|
signal: ctx.signal,
|
|
151
151
|
meta: ctx.meta,
|
|
152
|
-
requestFn: ctx.meta?.requestFn as never,
|
|
152
|
+
requestFn: ctx.meta?.["requestFn"] as never,
|
|
153
153
|
});
|
|
154
154
|
}
|
|
155
155
|
|
package/src/rq/createApi.ts
CHANGED
|
@@ -29,7 +29,7 @@ export function createApi<Req, Res>(opts: Options<Req, Res>) {
|
|
|
29
29
|
const { requestFn, ...rest } = opts;
|
|
30
30
|
const api = new RequestBuilder<Req, Res>({
|
|
31
31
|
url: opts.queryKey,
|
|
32
|
-
// 给定 get 则 在 requestFn 中可以通过 params 获取到参数,否则是 data
|
|
32
|
+
// 给定 get 则 在 requestFn 中可以通过 params 获取到参数,否则是 data 字段
|
|
33
33
|
method: "get",
|
|
34
34
|
requestFn: (config) => {
|
|
35
35
|
return requestFn(config.params) as never;
|
package/src/numbro/currencies.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { LocalesEnum } from "../i18n/enums";
|
|
2
|
-
import type { CurrencyFormat } from "./shared";
|
|
3
|
-
|
|
4
|
-
export const defaultCurrencies: Record<
|
|
5
|
-
string,
|
|
6
|
-
Omit<CurrencyFormat, "locale">
|
|
7
|
-
> = {
|
|
8
|
-
[LocalesEnum.en]: {
|
|
9
|
-
symbol: "$",
|
|
10
|
-
position: "prefix",
|
|
11
|
-
mantissa: 2,
|
|
12
|
-
},
|
|
13
|
-
[LocalesEnum.id]: {
|
|
14
|
-
symbol: "Rp",
|
|
15
|
-
position: "prefix",
|
|
16
|
-
mantissa: 2,
|
|
17
|
-
},
|
|
18
|
-
[LocalesEnum.zh]: {
|
|
19
|
-
symbol: "¥",
|
|
20
|
-
position: "prefix",
|
|
21
|
-
mantissa: 2,
|
|
22
|
-
},
|
|
23
|
-
};
|