@agions/taroviz 1.1.1 → 1.2.1
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/README.md +324 -53
- package/dist/cjs/index.js +1 -0
- package/dist/esm/index.js +82979 -0
- package/package.json +160 -30
- package/src/__tests__/integration.test.tsx +168 -0
- package/src/adapters/__tests__/index.test.ts +91 -0
- package/src/adapters/h5/__tests__/index.test.ts +156 -0
- package/src/adapters/h5/index.ts +301 -0
- package/src/adapters/harmony/index.ts +274 -0
- package/src/adapters/index.ts +166 -0
- package/src/adapters/swan/index.ts +274 -0
- package/src/adapters/tt/index.ts +274 -0
- package/src/adapters/types.ts +162 -0
- package/src/adapters/weapp/index.ts +237 -0
- package/src/charts/bar/__tests__/index.test.tsx +113 -0
- package/src/charts/bar/index.tsx +18 -0
- package/src/charts/common/BaseChartWrapper.tsx +136 -0
- package/src/charts/funnel/index.tsx +18 -0
- package/src/charts/gauge/index.tsx +18 -0
- package/src/charts/heatmap/index.tsx +18 -0
- package/src/charts/index.ts +21 -0
- package/src/charts/line/__tests__/index.test.tsx +107 -0
- package/src/charts/line/index.tsx +18 -0
- package/src/charts/pie/__tests__/index.test.tsx +112 -0
- package/src/charts/pie/index.tsx +19 -0
- package/src/charts/radar/index.tsx +18 -0
- package/src/charts/scatter/index.tsx +18 -0
- package/src/charts/types.ts +619 -0
- package/src/charts/utils.ts +56 -0
- package/src/core/__tests__/platform.test.ts +48 -0
- package/src/core/animation/AnimationManager.ts +391 -0
- package/src/core/animation/index.ts +20 -0
- package/src/core/animation/types.ts +248 -0
- package/src/core/components/BaseChart.tsx +1313 -0
- package/src/core/components/ErrorBoundary.tsx +458 -0
- package/src/core/echarts.ts +58 -0
- package/src/core/index.ts +22 -0
- package/src/core/types/chart.ts +66 -0
- package/src/core/types/common.ts +224 -0
- package/src/core/types/index.ts +281 -0
- package/src/core/types/platform.ts +325 -0
- package/src/core/utils/__tests__/common.test.ts +52 -0
- package/src/core/utils/__tests__/environment.test.ts +94 -0
- package/src/core/utils/__tests__/i18n.test.ts +247 -0
- package/src/core/utils/__tests__/index.test.ts +219 -0
- package/src/core/utils/__tests__/uuid.test.ts +78 -0
- package/src/core/utils/chartInstances.ts +69 -0
- package/src/core/utils/codeGenerator/CodeGenerator.ts +655 -0
- package/src/core/utils/codeGenerator/index.ts +13 -0
- package/src/core/utils/codeGenerator/types.ts +198 -0
- package/src/core/utils/common.ts +58 -0
- package/src/core/utils/configGenerator/ConfigGenerator.ts +583 -0
- package/src/core/utils/configGenerator/index.ts +13 -0
- package/src/core/utils/configGenerator/types.ts +445 -0
- package/src/core/utils/debug/DebugPanel.tsx +637 -0
- package/src/core/utils/debug/debugger.ts +322 -0
- package/src/core/utils/debug/index.ts +21 -0
- package/src/core/utils/debug/types.ts +142 -0
- package/src/core/utils/i18n.ts +452 -0
- package/src/core/utils/index.ts +162 -0
- package/src/core/utils/performance/PerformanceAnalyzer.ts +586 -0
- package/src/core/utils/performance/index.ts +13 -0
- package/src/core/utils/performance/types.ts +180 -0
- package/src/core/utils/uuid.ts +30 -0
- package/src/editor/ThemeEditor.tsx +449 -0
- package/src/editor/index.ts +10 -0
- package/src/hooks/__tests__/index.test.tsx +333 -0
- package/src/hooks/index.ts +594 -0
- package/src/index.ts +75 -0
- package/src/main.tsx +247 -0
- package/src/react-dom.d.ts +7 -0
- package/src/themes/__tests__/index.test.ts +91 -0
- package/src/themes/index.ts +860 -0
- package/dist/389.index.esm.js +0 -1
- package/dist/389.index.js +0 -1
- package/dist/633.index.esm.js +0 -1
- package/dist/633.index.js +0 -1
- package/dist/967.index.esm.js +0 -1
- package/dist/967.index.js +0 -1
- package/dist/index.esm.js +0 -1
- package/dist/index.js +0 -1
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TaroViz 国际化工具
|
|
3
|
+
* 提供日期、时间、数字、货币等格式化功能
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 本地化配置选项
|
|
8
|
+
*/
|
|
9
|
+
export interface LocaleOptions {
|
|
10
|
+
/**
|
|
11
|
+
* 语言标签,如 'zh-CN', 'en-US', 'de-DE' 等
|
|
12
|
+
*/
|
|
13
|
+
locale?: string;
|
|
14
|
+
/**
|
|
15
|
+
* 时区,如 'Asia/Shanghai', 'America/New_York' 等
|
|
16
|
+
*/
|
|
17
|
+
timeZone?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 日期格式化选项
|
|
22
|
+
*/
|
|
23
|
+
export interface DateFormatOptions extends LocaleOptions {
|
|
24
|
+
/**
|
|
25
|
+
* 日期样式:'full', 'long', 'medium', 'short' 或自定义格式
|
|
26
|
+
*/
|
|
27
|
+
dateStyle?: 'full' | 'long' | 'medium' | 'short';
|
|
28
|
+
/**
|
|
29
|
+
* 时间样式:'full', 'long', 'medium', 'short' 或自定义格式
|
|
30
|
+
*/
|
|
31
|
+
timeStyle?: 'full' | 'long' | 'medium' | 'short';
|
|
32
|
+
/**
|
|
33
|
+
* 是否包含年
|
|
34
|
+
*/
|
|
35
|
+
year?: 'numeric' | '2-digit';
|
|
36
|
+
/**
|
|
37
|
+
* 是否包含月
|
|
38
|
+
*/
|
|
39
|
+
month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';
|
|
40
|
+
/**
|
|
41
|
+
* 是否包含日
|
|
42
|
+
*/
|
|
43
|
+
day?: 'numeric' | '2-digit';
|
|
44
|
+
/**
|
|
45
|
+
* 是否包含小时
|
|
46
|
+
*/
|
|
47
|
+
hour?: 'numeric' | '2-digit';
|
|
48
|
+
/**
|
|
49
|
+
* 是否包含分钟
|
|
50
|
+
*/
|
|
51
|
+
minute?: 'numeric' | '2-digit';
|
|
52
|
+
/**
|
|
53
|
+
* 是否包含秒
|
|
54
|
+
*/
|
|
55
|
+
second?: 'numeric' | '2-digit';
|
|
56
|
+
/**
|
|
57
|
+
* 是否包含毫秒
|
|
58
|
+
*/
|
|
59
|
+
fractionalSecondDigits?: 0 | 1 | 2 | 3;
|
|
60
|
+
/**
|
|
61
|
+
* 是否包含星期
|
|
62
|
+
*/
|
|
63
|
+
weekday?: 'long' | 'short' | 'narrow';
|
|
64
|
+
/**
|
|
65
|
+
* 时间格式:'12hour' 或 '24hour'
|
|
66
|
+
*/
|
|
67
|
+
hour12?: boolean;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 货币格式化选项
|
|
72
|
+
*/
|
|
73
|
+
export interface CurrencyFormatOptions extends LocaleOptions {
|
|
74
|
+
/**
|
|
75
|
+
* 货币代码,如 'CNY', 'USD', 'EUR' 等
|
|
76
|
+
*/
|
|
77
|
+
currency: string;
|
|
78
|
+
/**
|
|
79
|
+
* 货币显示样式:'symbol', 'narrowSymbol', 'code', 'name'
|
|
80
|
+
*/
|
|
81
|
+
currencyDisplay?: 'symbol' | 'narrowSymbol' | 'code' | 'name';
|
|
82
|
+
/**
|
|
83
|
+
* 小数位数
|
|
84
|
+
*/
|
|
85
|
+
digits?: number;
|
|
86
|
+
/**
|
|
87
|
+
* 是否使用分组分隔符
|
|
88
|
+
*/
|
|
89
|
+
useGrouping?: boolean;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 百分比格式化选项
|
|
94
|
+
*/
|
|
95
|
+
export interface PercentFormatOptions extends LocaleOptions {
|
|
96
|
+
/**
|
|
97
|
+
* 小数位数
|
|
98
|
+
*/
|
|
99
|
+
digits?: number;
|
|
100
|
+
/**
|
|
101
|
+
* 是否使用分组分隔符
|
|
102
|
+
*/
|
|
103
|
+
useGrouping?: boolean;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 数字格式化选项
|
|
108
|
+
*/
|
|
109
|
+
export interface NumberFormatOptions extends LocaleOptions {
|
|
110
|
+
/**
|
|
111
|
+
* 小数位数
|
|
112
|
+
*/
|
|
113
|
+
digits?: number;
|
|
114
|
+
/**
|
|
115
|
+
* 是否使用分组分隔符
|
|
116
|
+
*/
|
|
117
|
+
useGrouping?: boolean;
|
|
118
|
+
/**
|
|
119
|
+
* 最小整数位数
|
|
120
|
+
*/
|
|
121
|
+
minimumIntegerDigits?: number;
|
|
122
|
+
/**
|
|
123
|
+
* 最小小数位数
|
|
124
|
+
*/
|
|
125
|
+
minimumFractionDigits?: number;
|
|
126
|
+
/**
|
|
127
|
+
* 最大小数位数
|
|
128
|
+
*/
|
|
129
|
+
maximumFractionDigits?: number;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 格式化日期时间
|
|
134
|
+
* @param date 日期对象或时间戳
|
|
135
|
+
* @param options 格式化选项
|
|
136
|
+
* @returns 格式化后的日期时间字符串
|
|
137
|
+
*/
|
|
138
|
+
export function formatDateTime(
|
|
139
|
+
date: Date | number | string,
|
|
140
|
+
options: DateFormatOptions = {}
|
|
141
|
+
): string {
|
|
142
|
+
const { locale = 'zh-CN', timeZone = 'Asia/Shanghai', ...formatOptions } = options;
|
|
143
|
+
|
|
144
|
+
// 确保date是Date对象
|
|
145
|
+
const dateObj = typeof date === 'string' || typeof date === 'number' ? new Date(date) : date;
|
|
146
|
+
|
|
147
|
+
return new Intl.DateTimeFormat(locale, {
|
|
148
|
+
timeZone,
|
|
149
|
+
...formatOptions,
|
|
150
|
+
}).format(dateObj);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 格式化日期
|
|
155
|
+
* @param date 日期对象或时间戳
|
|
156
|
+
* @param options 格式化选项
|
|
157
|
+
* @returns 格式化后的日期字符串
|
|
158
|
+
*/
|
|
159
|
+
export function formatDate(date: Date | number | string, options: DateFormatOptions = {}): string {
|
|
160
|
+
return formatDateTime(date, {
|
|
161
|
+
...options,
|
|
162
|
+
timeStyle: undefined,
|
|
163
|
+
hour: undefined,
|
|
164
|
+
minute: undefined,
|
|
165
|
+
second: undefined,
|
|
166
|
+
fractionalSecondDigits: undefined,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 格式化时间
|
|
172
|
+
* @param date 日期对象或时间戳
|
|
173
|
+
* @param options 格式化选项
|
|
174
|
+
* @returns 格式化后的时间字符串
|
|
175
|
+
*/
|
|
176
|
+
export function formatTime(date: Date | number | string, options: DateFormatOptions = {}): string {
|
|
177
|
+
// 确保至少显示小时和分钟
|
|
178
|
+
const timeOptions = {
|
|
179
|
+
...options,
|
|
180
|
+
dateStyle: undefined,
|
|
181
|
+
year: undefined,
|
|
182
|
+
month: undefined,
|
|
183
|
+
day: undefined,
|
|
184
|
+
weekday: undefined,
|
|
185
|
+
hour: options.hour || '2-digit',
|
|
186
|
+
minute: options.minute || '2-digit',
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
return formatDateTime(date, timeOptions);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* 格式化货币
|
|
194
|
+
* @param value 数值
|
|
195
|
+
* @param options 格式化选项
|
|
196
|
+
* @returns 格式化后的货币字符串
|
|
197
|
+
*/
|
|
198
|
+
export function formatCurrency(value: number, options: CurrencyFormatOptions): string {
|
|
199
|
+
const {
|
|
200
|
+
locale = 'zh-CN',
|
|
201
|
+
currency,
|
|
202
|
+
currencyDisplay = 'symbol',
|
|
203
|
+
digits = 2,
|
|
204
|
+
useGrouping = true,
|
|
205
|
+
} = options;
|
|
206
|
+
|
|
207
|
+
return new Intl.NumberFormat(locale, {
|
|
208
|
+
style: 'currency',
|
|
209
|
+
currency,
|
|
210
|
+
currencyDisplay,
|
|
211
|
+
minimumFractionDigits: digits,
|
|
212
|
+
maximumFractionDigits: digits,
|
|
213
|
+
useGrouping,
|
|
214
|
+
}).format(value);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 格式化百分比
|
|
219
|
+
* @param value 数值(0-1之间)
|
|
220
|
+
* @param options 格式化选项
|
|
221
|
+
* @returns 格式化后的百分比字符串
|
|
222
|
+
*/
|
|
223
|
+
export function formatPercent(value: number, options: PercentFormatOptions = {}): string {
|
|
224
|
+
const { locale = 'zh-CN', digits = 2, useGrouping = true } = options;
|
|
225
|
+
|
|
226
|
+
return new Intl.NumberFormat(locale, {
|
|
227
|
+
style: 'percent',
|
|
228
|
+
minimumFractionDigits: digits,
|
|
229
|
+
maximumFractionDigits: digits,
|
|
230
|
+
useGrouping,
|
|
231
|
+
}).format(value);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* 格式化数字
|
|
236
|
+
* @param value 数值
|
|
237
|
+
* @param options 格式化选项
|
|
238
|
+
* @returns 格式化后的数字字符串
|
|
239
|
+
*/
|
|
240
|
+
export function formatNumber(value: number, options: NumberFormatOptions = {}): string {
|
|
241
|
+
const { locale = 'zh-CN', digits = 2, useGrouping = true, ...formatOptions } = options;
|
|
242
|
+
|
|
243
|
+
return new Intl.NumberFormat(locale, {
|
|
244
|
+
minimumFractionDigits: digits,
|
|
245
|
+
maximumFractionDigits: digits,
|
|
246
|
+
useGrouping,
|
|
247
|
+
...formatOptions,
|
|
248
|
+
}).format(value);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* 获取支持的语言列表
|
|
253
|
+
* @returns 支持的语言列表
|
|
254
|
+
*/
|
|
255
|
+
export function getSupportedLocales(): string[] {
|
|
256
|
+
return Intl.NumberFormat.supportedLocalesOf([
|
|
257
|
+
'zh-CN',
|
|
258
|
+
'zh-TW',
|
|
259
|
+
'en-US',
|
|
260
|
+
'en-GB',
|
|
261
|
+
'ja-JP',
|
|
262
|
+
'ko-KR',
|
|
263
|
+
'fr-FR',
|
|
264
|
+
'de-DE',
|
|
265
|
+
'es-ES',
|
|
266
|
+
'it-IT',
|
|
267
|
+
'pt-BR',
|
|
268
|
+
'ru-RU',
|
|
269
|
+
'ar-SA',
|
|
270
|
+
'hi-IN',
|
|
271
|
+
'th-TH',
|
|
272
|
+
'vi-VN',
|
|
273
|
+
'id-ID',
|
|
274
|
+
'ms-MY',
|
|
275
|
+
]);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* 获取当前系统语言
|
|
280
|
+
* @returns 当前系统语言
|
|
281
|
+
*/
|
|
282
|
+
export function getSystemLocale(): string {
|
|
283
|
+
if (typeof window !== 'undefined') {
|
|
284
|
+
return window.navigator.language || 'zh-CN';
|
|
285
|
+
}
|
|
286
|
+
return 'zh-CN';
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* 获取当前系统时区
|
|
291
|
+
* @returns 当前系统时区
|
|
292
|
+
*/
|
|
293
|
+
export function getSystemTimeZone(): string {
|
|
294
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* 本地化工具类
|
|
299
|
+
*/
|
|
300
|
+
export class Localization {
|
|
301
|
+
private locale: string;
|
|
302
|
+
private timeZone: string;
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* 构造函数
|
|
306
|
+
* @param options 本地化配置
|
|
307
|
+
*/
|
|
308
|
+
constructor(options: LocaleOptions = {}) {
|
|
309
|
+
this.locale = options.locale || getSystemLocale();
|
|
310
|
+
this.timeZone = options.timeZone || getSystemTimeZone();
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* 设置语言
|
|
315
|
+
* @param locale 语言标签
|
|
316
|
+
*/
|
|
317
|
+
setLocale(locale: string): void {
|
|
318
|
+
this.locale = locale;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* 获取当前语言
|
|
323
|
+
* @returns 当前语言标签
|
|
324
|
+
*/
|
|
325
|
+
getLocale(): string {
|
|
326
|
+
return this.locale;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* 设置时区
|
|
331
|
+
* @param timeZone 时区
|
|
332
|
+
*/
|
|
333
|
+
setTimeZone(timeZone: string): void {
|
|
334
|
+
this.timeZone = timeZone;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* 获取当前时区
|
|
339
|
+
* @returns 当前时区
|
|
340
|
+
*/
|
|
341
|
+
getTimeZone(): string {
|
|
342
|
+
return this.timeZone;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* 格式化日期时间
|
|
347
|
+
* @param date 日期对象或时间戳
|
|
348
|
+
* @param options 格式化选项
|
|
349
|
+
* @returns 格式化后的日期时间字符串
|
|
350
|
+
*/
|
|
351
|
+
formatDateTime(
|
|
352
|
+
date: Date | number | string,
|
|
353
|
+
options: Omit<DateFormatOptions, 'locale' | 'timeZone'> = {}
|
|
354
|
+
): string {
|
|
355
|
+
return formatDateTime(date, {
|
|
356
|
+
...options,
|
|
357
|
+
locale: this.locale,
|
|
358
|
+
timeZone: this.timeZone,
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* 格式化日期
|
|
364
|
+
* @param date 日期对象或时间戳
|
|
365
|
+
* @param options 格式化选项
|
|
366
|
+
* @returns 格式化后的日期字符串
|
|
367
|
+
*/
|
|
368
|
+
formatDate(
|
|
369
|
+
date: Date | number | string,
|
|
370
|
+
options: Omit<DateFormatOptions, 'locale' | 'timeZone'> = {}
|
|
371
|
+
): string {
|
|
372
|
+
return formatDate(date, {
|
|
373
|
+
...options,
|
|
374
|
+
locale: this.locale,
|
|
375
|
+
timeZone: this.timeZone,
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* 格式化时间
|
|
381
|
+
* @param date 日期对象或时间戳
|
|
382
|
+
* @param options 格式化选项
|
|
383
|
+
* @returns 格式化后的时间字符串
|
|
384
|
+
*/
|
|
385
|
+
formatTime(
|
|
386
|
+
date: Date | number | string,
|
|
387
|
+
options: Omit<DateFormatOptions, 'locale' | 'timeZone'> = {}
|
|
388
|
+
): string {
|
|
389
|
+
return formatTime(date, {
|
|
390
|
+
...options,
|
|
391
|
+
locale: this.locale,
|
|
392
|
+
timeZone: this.timeZone,
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* 格式化货币
|
|
398
|
+
* @param value 数值
|
|
399
|
+
* @param options 格式化选项
|
|
400
|
+
* @returns 格式化后的货币字符串
|
|
401
|
+
*/
|
|
402
|
+
formatCurrency(value: number, options: Omit<CurrencyFormatOptions, 'locale'>): string {
|
|
403
|
+
return formatCurrency(value, {
|
|
404
|
+
...options,
|
|
405
|
+
locale: this.locale,
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* 格式化百分比
|
|
411
|
+
* @param value 数值
|
|
412
|
+
* @param options 格式化选项
|
|
413
|
+
* @returns 格式化后的百分比字符串
|
|
414
|
+
*/
|
|
415
|
+
formatPercent(value: number, options: Omit<PercentFormatOptions, 'locale'> = {}): string {
|
|
416
|
+
return formatPercent(value, {
|
|
417
|
+
...options,
|
|
418
|
+
locale: this.locale,
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* 格式化数字
|
|
424
|
+
* @param value 数值
|
|
425
|
+
* @param options 格式化选项
|
|
426
|
+
* @returns 格式化后的数字字符串
|
|
427
|
+
*/
|
|
428
|
+
formatNumber(value: number, options: Omit<NumberFormatOptions, 'locale'> = {}): string {
|
|
429
|
+
return formatNumber(value, {
|
|
430
|
+
...options,
|
|
431
|
+
locale: this.locale,
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// 默认本地化实例
|
|
437
|
+
export const defaultLocalization = new Localization();
|
|
438
|
+
|
|
439
|
+
// 导出所有格式化函数
|
|
440
|
+
export default {
|
|
441
|
+
formatDateTime,
|
|
442
|
+
formatDate,
|
|
443
|
+
formatTime,
|
|
444
|
+
formatCurrency,
|
|
445
|
+
formatPercent,
|
|
446
|
+
formatNumber,
|
|
447
|
+
getSupportedLocales,
|
|
448
|
+
getSystemLocale,
|
|
449
|
+
getSystemTimeZone,
|
|
450
|
+
Localization,
|
|
451
|
+
defaultLocalization,
|
|
452
|
+
};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// 事件定义
|
|
2
|
+
export const events = {
|
|
3
|
+
click: 'click',
|
|
4
|
+
mousemove: 'mousemove',
|
|
5
|
+
mouseup: 'mouseup',
|
|
6
|
+
mousedown: 'mousedown',
|
|
7
|
+
mouseover: 'mouseover',
|
|
8
|
+
mouseout: 'mouseout',
|
|
9
|
+
globalout: 'globalout',
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// 导入UUID工具函数
|
|
13
|
+
import * as i18n from './i18n';
|
|
14
|
+
import { uuid, shortId, prefixedId } from './uuid';
|
|
15
|
+
|
|
16
|
+
// 导入国际化工具
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 深度合并对象
|
|
20
|
+
* @param target 目标对象
|
|
21
|
+
* @param source 源对象
|
|
22
|
+
* @returns 合并后的对象
|
|
23
|
+
*/
|
|
24
|
+
export function deepMerge<T extends Record<string, any>>(
|
|
25
|
+
target: T,
|
|
26
|
+
source: Record<string, any>
|
|
27
|
+
): T {
|
|
28
|
+
const result: any = { ...target };
|
|
29
|
+
|
|
30
|
+
Object.keys(source).forEach((key) => {
|
|
31
|
+
if (source[key] instanceof Object && key in target && target[key] instanceof Object) {
|
|
32
|
+
result[key] = deepMerge(target[key], source[key]);
|
|
33
|
+
} else {
|
|
34
|
+
result[key] = source[key];
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 防抖函数
|
|
43
|
+
* @param fn 需要防抖的函数
|
|
44
|
+
* @param delay 延迟时间(毫秒)
|
|
45
|
+
* @returns 防抖后的函数
|
|
46
|
+
*/
|
|
47
|
+
export function debounce<T extends (...args: any[]) => any>(
|
|
48
|
+
fn: T,
|
|
49
|
+
delay: number
|
|
50
|
+
): (...args: Parameters<T>) => void {
|
|
51
|
+
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
52
|
+
|
|
53
|
+
return function (this: any, ...args: Parameters<T>): void {
|
|
54
|
+
if (timer) {
|
|
55
|
+
clearTimeout(timer);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
timer = setTimeout(() => {
|
|
59
|
+
fn.apply(this, args);
|
|
60
|
+
timer = null;
|
|
61
|
+
}, delay);
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 节流函数
|
|
67
|
+
* @param fn 需要节流的函数
|
|
68
|
+
* @param interval 间隔时间(毫秒)
|
|
69
|
+
* @returns 节流后的函数
|
|
70
|
+
*/
|
|
71
|
+
export function throttle<T extends (...args: any[]) => any>(
|
|
72
|
+
fn: T,
|
|
73
|
+
interval: number
|
|
74
|
+
): (...args: Parameters<T>) => void {
|
|
75
|
+
let lastTime = 0;
|
|
76
|
+
|
|
77
|
+
return function (this: any, ...args: Parameters<T>): void {
|
|
78
|
+
const now = Date.now();
|
|
79
|
+
|
|
80
|
+
if (now - lastTime >= interval) {
|
|
81
|
+
fn.apply(this, args);
|
|
82
|
+
lastTime = now;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 检测当前环境
|
|
89
|
+
* @returns 环境信息
|
|
90
|
+
*/
|
|
91
|
+
export function getEnvironment() {
|
|
92
|
+
const isServer = typeof window === 'undefined';
|
|
93
|
+
const isClient = !isServer;
|
|
94
|
+
|
|
95
|
+
// 使用类型断言解决wx和my未定义的问题
|
|
96
|
+
const isWeapp =
|
|
97
|
+
typeof (window as any)['wx'] !== 'undefined' &&
|
|
98
|
+
typeof (window as any)['wx'].getSystemInfoSync === 'function';
|
|
99
|
+
const isAlipay =
|
|
100
|
+
typeof (window as any)['my'] !== 'undefined' &&
|
|
101
|
+
typeof (window as any)['my'].getSystemInfoSync === 'function';
|
|
102
|
+
const isWeb = isClient && !isWeapp && !isAlipay;
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
isServer,
|
|
106
|
+
isClient,
|
|
107
|
+
isWeapp,
|
|
108
|
+
isAlipay,
|
|
109
|
+
isWeb,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 格式化数值
|
|
115
|
+
* @param value 要格式化的数值
|
|
116
|
+
* @param digits 小数位数
|
|
117
|
+
* @param options 配置选项
|
|
118
|
+
* @returns 格式化后的字符串
|
|
119
|
+
*/
|
|
120
|
+
export function formatNumber(
|
|
121
|
+
value: number,
|
|
122
|
+
digits: number = 2,
|
|
123
|
+
options: {
|
|
124
|
+
useGrouping?: boolean;
|
|
125
|
+
locale?: string;
|
|
126
|
+
} = {}
|
|
127
|
+
): string {
|
|
128
|
+
const { useGrouping = true, locale = 'zh-CN' } = options;
|
|
129
|
+
|
|
130
|
+
return new Intl.NumberFormat(locale, {
|
|
131
|
+
minimumFractionDigits: digits,
|
|
132
|
+
maximumFractionDigits: digits,
|
|
133
|
+
useGrouping,
|
|
134
|
+
}).format(value);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 获取颜色的对比色
|
|
139
|
+
* @param color 十六进制颜色值
|
|
140
|
+
* @returns 对比色
|
|
141
|
+
*/
|
|
142
|
+
export function getContrastColor(color: string): string {
|
|
143
|
+
// 移除#前缀
|
|
144
|
+
const hex = color.replace('#', '');
|
|
145
|
+
|
|
146
|
+
// 转换为RGB
|
|
147
|
+
const r = parseInt(hex.substring(0, 2), 16);
|
|
148
|
+
const g = parseInt(hex.substring(2, 4), 16);
|
|
149
|
+
const b = parseInt(hex.substring(4, 6), 16);
|
|
150
|
+
|
|
151
|
+
// 计算亮度
|
|
152
|
+
const brightness = (r * 299 + g * 587 + b * 114) / 1000;
|
|
153
|
+
|
|
154
|
+
// 根据亮度返回黑色或白色
|
|
155
|
+
return brightness > 128 ? '#000000' : '#FFFFFF';
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 导出UUID工具函数
|
|
159
|
+
export { uuid, shortId, prefixedId };
|
|
160
|
+
|
|
161
|
+
// 导出国际化工具
|
|
162
|
+
export { i18n };
|