@canvas-components/utils 0.2.0 → 0.2.2
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 +141 -96
- package/dist/clipboard/index.cjs +8 -5
- package/dist/clipboard/index.cjs.map +1 -1
- package/dist/clipboard/index.d.cts +1 -1
- package/dist/clipboard/index.d.ts +1 -1
- package/dist/clipboard/index.js +8 -5
- package/dist/clipboard/index.js.map +1 -1
- package/dist/data/index.cjs +31 -0
- package/dist/data/index.cjs.map +1 -0
- package/dist/data/index.d.cts +22 -0
- package/dist/data/index.d.ts +22 -0
- package/dist/data/index.js +27 -0
- package/dist/data/index.js.map +1 -0
- package/dist/date/index.cjs +162 -0
- package/dist/date/index.cjs.map +1 -0
- package/dist/date/index.d.cts +13 -0
- package/dist/date/index.d.ts +13 -0
- package/dist/date/index.js +158 -0
- package/dist/date/index.js.map +1 -0
- package/dist/export/index.cjs +14 -12
- package/dist/export/index.cjs.map +1 -1
- package/dist/export/index.d.cts +5 -1
- package/dist/export/index.d.ts +5 -1
- package/dist/export/index.js +14 -13
- package/dist/export/index.js.map +1 -1
- package/dist/geometry/index.cjs +14 -0
- package/dist/geometry/index.cjs.map +1 -0
- package/dist/geometry/index.d.cts +15 -0
- package/dist/geometry/index.d.ts +15 -0
- package/dist/geometry/index.js +11 -0
- package/dist/geometry/index.js.map +1 -0
- package/dist/index.cjs +612 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +590 -18
- package/dist/index.js.map +1 -1
- package/dist/number/index.cjs +399 -0
- package/dist/number/index.cjs.map +1 -0
- package/dist/number/index.d.cts +44 -0
- package/dist/number/index.d.ts +44 -0
- package/dist/number/index.js +384 -0
- package/dist/number/index.js.map +1 -0
- package/package.json +21 -1
package/README.md
CHANGED
|
@@ -1,96 +1,141 @@
|
|
|
1
|
-
# @canvas-components/utils
|
|
2
|
-
|
|
3
|
-
`@canvas-components/utils`
|
|
4
|
-
|
|
5
|
-
## 功能作用
|
|
6
|
-
|
|
7
|
-
- 写入文本、二维表格文本、JSON 到系统剪贴板
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
["
|
|
34
|
-
["
|
|
35
|
-
]
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
{ name: "
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
{ key: "
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
|
56
|
-
|
|
|
57
|
-
| `
|
|
58
|
-
| `
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
|
74
|
-
|
|
|
75
|
-
| `
|
|
76
|
-
| `
|
|
77
|
-
| `
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
|
83
|
-
|
|
|
84
|
-
| `
|
|
85
|
-
| `
|
|
86
|
-
| `
|
|
87
|
-
| `
|
|
88
|
-
| `
|
|
89
|
-
| `
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
1
|
+
# @canvas-components/utils
|
|
2
|
+
|
|
3
|
+
`@canvas-components/utils` 提供表格包之间可复用的纯原生工具能力,覆盖日期时间、数据读取、剪贴板写入和导出文件。
|
|
4
|
+
|
|
5
|
+
## 功能作用
|
|
6
|
+
|
|
7
|
+
- 写入文本、二维表格文本、JSON 到系统剪贴板
|
|
8
|
+
- 通过 `dataIndex` 安全读取嵌套数据
|
|
9
|
+
- 使用 Day.js 风格的常用 token 格式化日期时间,不依赖 Day.js 运行时
|
|
10
|
+
- 复用数字、金额、百分比、科学计数法和自定义数字格式化
|
|
11
|
+
- 复用数据路径比较、数值约束和矩形命中等纯函数
|
|
12
|
+
- 把行列数据导出为 `csv`、`txt`、`xlsx`
|
|
13
|
+
- 生成导出前的中间文本 / 记录结构
|
|
14
|
+
- 对大批量导出做分片让步,减少主线程长时间阻塞
|
|
15
|
+
|
|
16
|
+
## 安装方法
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pnpm add @canvas-components/utils
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
最低环境要求:
|
|
23
|
+
|
|
24
|
+
- `Node.js >= 18.12.0`
|
|
25
|
+
- `pnpm >= 9.0.0`
|
|
26
|
+
|
|
27
|
+
## 使用方法
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { copyRows, exportTableToXlsx } from "@canvas-components/utils";
|
|
31
|
+
|
|
32
|
+
await copyRows([
|
|
33
|
+
["姓名", "年龄"],
|
|
34
|
+
["张三", 28],
|
|
35
|
+
["李四", 31],
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
await exportTableToXlsx({
|
|
39
|
+
fileName: "users.xlsx",
|
|
40
|
+
rows: [
|
|
41
|
+
{ name: "张三", age: 28 },
|
|
42
|
+
{ name: "李四", age: 31 },
|
|
43
|
+
],
|
|
44
|
+
columns: [
|
|
45
|
+
{ key: "name", title: "姓名", value: (row) => row.name },
|
|
46
|
+
{ key: "age", title: "年龄", value: (row) => row.age },
|
|
47
|
+
],
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## 属性说明
|
|
52
|
+
|
|
53
|
+
### `ExportTableColumn<Row>`
|
|
54
|
+
|
|
55
|
+
| 属性 | 类型 | 说明 |
|
|
56
|
+
| ------- | -------------------------------------- | ------------------ |
|
|
57
|
+
| `key` | `string` | 列唯一标识 |
|
|
58
|
+
| `title` | `string` | 导出列标题 |
|
|
59
|
+
| `value` | `(row: Row, index: number) => unknown` | 从行数据中取导出值 |
|
|
60
|
+
|
|
61
|
+
### `ClipboardCellValue`
|
|
62
|
+
|
|
63
|
+
支持的剪贴板单元格值类型:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
type ClipboardCellValue = string | number | boolean | null | undefined;
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## 方法说明
|
|
70
|
+
|
|
71
|
+
### 剪贴板
|
|
72
|
+
|
|
73
|
+
| 方法 | 签名 | 说明 |
|
|
74
|
+
| ------------------------ | ------------------------------------------------- | ------------------------ |
|
|
75
|
+
| `copyText` | `(text: string) => Promise<void>` | 写入纯文本 |
|
|
76
|
+
| `copyRows` | `(rows: ClipboardCellValue[][]) => Promise<void>` | 把二维数据写入剪贴板 |
|
|
77
|
+
| `copyJson` | `(value: unknown, space = 2) => Promise<void>` | 写入 JSON 文本 |
|
|
78
|
+
| `formatRowsForClipboard` | `(rows) => string` | 把二维数据转成制表符文本 |
|
|
79
|
+
|
|
80
|
+
### 导出
|
|
81
|
+
|
|
82
|
+
| 方法 | 签名 | 说明 |
|
|
83
|
+
| ------------------------ | ------------------------------------------------------------ | --------------------------- |
|
|
84
|
+
| `mapRowsToExportRecords` | `({ rows, columns }) => Record<string, unknown>[]` | 生成导出记录数组 |
|
|
85
|
+
| `buildCsvContent` | `({ rows, columns }) => string` | 生成 CSV 文本 |
|
|
86
|
+
| `serializeCsvRows` | `(rows, lineBreak?) => string` | 将二维数据序列化为 CSV 文本 |
|
|
87
|
+
| `buildTxtContent` | `({ rows, columns }) => string` | 生成 TXT 文本 |
|
|
88
|
+
| `downloadJsonFile` | `({ fileName, data, space? }) => Promise<void>` | 下载 JSON |
|
|
89
|
+
| `exportTableToCsv` | `({ fileName, rows, columns }) => Promise<void>` | 导出 CSV |
|
|
90
|
+
| `exportTableToTxt` | `({ fileName, rows, columns }) => Promise<void>` | 导出 TXT |
|
|
91
|
+
| `exportTableToXlsx` | `({ fileName, rows, columns, sheetName? }) => Promise<void>` | 导出 XLSX |
|
|
92
|
+
|
|
93
|
+
### 数据读取
|
|
94
|
+
|
|
95
|
+
| 方法 | 签名 | 说明 |
|
|
96
|
+
| --------------------- | --------------------------------- | -------------------------------------------- |
|
|
97
|
+
| `getValueByDataIndex` | `(source, dataIndex?) => unknown` | 通过字符串、数字或属性路径数组安全读取嵌套值 |
|
|
98
|
+
| `stringifyDataIndex` | `(dataIndex) => string` | 将属性路径转换为稳定字符串 |
|
|
99
|
+
| `isSameDataIndex` | `(left, right) => boolean` | 判断两个属性路径是否一致 |
|
|
100
|
+
|
|
101
|
+
### 日期时间
|
|
102
|
+
|
|
103
|
+
可从主入口或 `@canvas-components/utils/date` 子路径引入。
|
|
104
|
+
|
|
105
|
+
| 方法 | 签名 | 说明 |
|
|
106
|
+
| ------------------------- | --------------------------------------------- | ---------------------------------------------- |
|
|
107
|
+
| `resolveDateValue` | `(value: unknown) => Date \| null` | 将日期、时间戳或日期字符串解析为有效日期 |
|
|
108
|
+
| `isDateTimeFormatPattern` | `(pattern: string) => boolean` | 判断是否包含日期时间格式 token |
|
|
109
|
+
| `formatDateTimeValue` | `(value, pattern, locale?) => string \| null` | 按格式字符串输出日期时间,默认使用中文区域设置 |
|
|
110
|
+
|
|
111
|
+
常用 token 包括 `YYYY`、`YY`、`M`、`MM`、`D`、`DD`、`ddd`、`dddd`、`H`、`HH`、`h`、`hh`、`m`、`mm`、`s`、`ss`、`SSS`、`A`。方括号中的内容按普通文本输出,例如 `YYYY年M月D日 [更新]`。
|
|
112
|
+
|
|
113
|
+
### 数字格式
|
|
114
|
+
|
|
115
|
+
可从主入口或 `@canvas-components/utils/number` 子路径引入。
|
|
116
|
+
|
|
117
|
+
| 方法 | 签名 | 说明 |
|
|
118
|
+
| --------------------------- | ------------------------------------ | -------------------------------------------- |
|
|
119
|
+
| `parseNumberValue` | `(value: unknown) => number \| null` | 解析数字和带千分位的数字字符串 |
|
|
120
|
+
| `formatNumberValue` | `(value, options?) => string` | 格式化数字、金额和千分位数字 |
|
|
121
|
+
| `formatPercentValue` | `(value, decimalPlaces?) => string` | 将比例数值格式化为百分比 |
|
|
122
|
+
| `formatScientificValue` | `(value, decimalPlaces?) => string` | 格式化科学计数法 |
|
|
123
|
+
| `formatCustomNumberPattern` | `(value, pattern) => string` | 按常见表格数字 pattern 格式化 |
|
|
124
|
+
| `formatChineseNumber` | `(value, style?) => string \| null` | 将数字转换为普通或金融大写中文数字 |
|
|
125
|
+
| `parseChineseNumber` | `(value) => number \| null` | 将规范的普通或金融大写中文数字转为阿拉伯数字 |
|
|
126
|
+
|
|
127
|
+
金融大写使用规范数字字符,例如 `918000` 输出 `玖拾壹万捌仟`;不兼容形近字或错别字。
|
|
128
|
+
|
|
129
|
+
### 基础几何
|
|
130
|
+
|
|
131
|
+
| 方法 | 签名 | 说明 |
|
|
132
|
+
| --------------- | ------------------------------------- | ---------------------- |
|
|
133
|
+
| `clamp` | `(value, minimum, maximum) => number` | 将数值限制在闭区间内 |
|
|
134
|
+
| `isPointInRect` | `(x, y, rect) => boolean` | 判断坐标是否位于矩形内 |
|
|
135
|
+
|
|
136
|
+
## 适用场景
|
|
137
|
+
|
|
138
|
+
- 表格导出
|
|
139
|
+
- 系统复制功能
|
|
140
|
+
- 大批量数据下载
|
|
141
|
+
- 非 React 项目里的浏览器工具函数复用
|
package/dist/clipboard/index.cjs
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
// src/clipboard/index.ts
|
|
4
|
-
async function copyText(text) {
|
|
4
|
+
async function copyText(text, ownerDocument = document) {
|
|
5
|
+
const clipboard = ownerDocument.defaultView?.navigator.clipboard;
|
|
5
6
|
try {
|
|
6
|
-
if (
|
|
7
|
-
await
|
|
8
|
-
return;
|
|
7
|
+
if (clipboard?.writeText) {
|
|
8
|
+
await clipboard.writeText(text);
|
|
9
|
+
return true;
|
|
9
10
|
}
|
|
10
11
|
} catch {
|
|
11
12
|
}
|
|
12
|
-
copyTextSync(text);
|
|
13
|
+
return copyTextSync(text, ownerDocument);
|
|
13
14
|
}
|
|
14
15
|
function copyTextSync(text, ownerDocument = document) {
|
|
15
16
|
const activeElement = ownerDocument.activeElement;
|
|
@@ -27,6 +28,8 @@ function copyTextSync(text, ownerDocument = document) {
|
|
|
27
28
|
let copied = false;
|
|
28
29
|
try {
|
|
29
30
|
copied = ownerDocument.execCommand?.("copy") ?? false;
|
|
31
|
+
} catch {
|
|
32
|
+
copied = false;
|
|
30
33
|
} finally {
|
|
31
34
|
textarea.remove();
|
|
32
35
|
const HTMLElementConstructor = ownerDocument.defaultView?.HTMLElement;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/clipboard/index.ts"],"names":[],"mappings":";;;AAKA,eAAsB,
|
|
1
|
+
{"version":3,"sources":["../../src/clipboard/index.ts"],"names":[],"mappings":";;;AAKA,eAAsB,QAAA,CAAS,IAAA,EAAc,aAAA,GAAgB,QAAA,EAAU;AACrE,EAAA,MAAM,SAAA,GAAY,aAAA,CAAc,WAAA,EAAa,SAAA,CAAU,SAAA;AACvD,EAAA,IAAI;AACF,IAAA,IAAI,WAAW,SAAA,EAAW;AACxB,MAAA,MAAM,SAAA,CAAU,UAAU,IAAI,CAAA;AAC9B,MAAA,OAAO,IAAA;AAAA,IACT;AAAA,EACF,CAAA,CAAA,MAAQ;AAAA,EAER;AAEA,EAAA,OAAO,YAAA,CAAa,MAAM,aAAa,CAAA;AACzC;AAGO,SAAS,YAAA,CAAa,IAAA,EAAc,aAAA,GAAgB,QAAA,EAAU;AACnE,EAAA,MAAM,gBAAgB,aAAA,CAAc,aAAA;AACpC,EAAA,MAAM,QAAA,GAAW,aAAA,CAAc,aAAA,CAAc,UAAU,CAAA;AACvD,EAAA,QAAA,CAAS,KAAA,GAAQ,IAAA;AACjB,EAAA,QAAA,CAAS,YAAA,CAAa,YAAY,MAAM,CAAA;AACxC,EAAA,QAAA,CAAS,MAAM,QAAA,GAAW,OAAA;AAC1B,EAAA,QAAA,CAAS,MAAM,IAAA,GAAO,SAAA;AACtB,EAAA,QAAA,CAAS,MAAM,GAAA,GAAM,GAAA;AACrB,EAAA,QAAA,CAAS,MAAM,OAAA,GAAU,GAAA;AAEzB,EAAA,aAAA,CAAc,IAAA,CAAK,YAAY,QAAQ,CAAA;AACvC,EAAA,QAAA,CAAS,KAAA,CAAM,EAAE,aAAA,EAAe,IAAA,EAAM,CAAA;AACtC,EAAA,QAAA,CAAS,MAAA,EAAO;AAChB,EAAA,QAAA,CAAS,iBAAA,CAAkB,CAAA,EAAG,QAAA,CAAS,KAAA,CAAM,MAAM,CAAA;AAEnD,EAAA,IAAI,MAAA,GAAS,KAAA;AACb,EAAA,IAAI;AACF,IAAA,MAAA,GAAS,aAAA,CAAc,WAAA,GAAc,MAAM,CAAA,IAAK,KAAA;AAAA,EAClD,CAAA,CAAA,MAAQ;AACN,IAAA,MAAA,GAAS,KAAA;AAAA,EACX,CAAA,SAAE;AACA,IAAA,QAAA,CAAS,MAAA,EAAO;AAChB,IAAA,MAAM,sBAAA,GAAyB,cAAc,WAAA,EAAa,WAAA;AAC1D,IAAA,IACE,sBAAA,IACA,yBAAyB,sBAAA,EACzB;AACA,MAAA,aAAA,CAAc,KAAA,CAAM,EAAE,aAAA,EAAe,IAAA,EAAM,CAAA;AAAA,IAC7C;AAAA,EACF;AACA,EAAA,OAAO,MAAA;AACT;AAKA,eAAsB,SAAS,IAAA,EAA8B;AAC3D,EAAA,MAAM,QAAA,CAAS,sBAAA,CAAuB,IAAI,CAAC,CAAA;AAC7C;AAKA,eAAsB,QAAA,CAAS,KAAA,EAAgB,KAAA,GAAQ,CAAA,EAAG;AACxD,EAAA,MAAM,SAAS,IAAA,CAAK,SAAA,CAAU,OAAO,IAAA,EAAM,KAAK,KAAK,EAAE,CAAA;AACzD;AAKO,SAAS,uBAAuB,IAAA,EAA8B;AACnE,EAAA,OAAO,IAAA,CAAK,GAAA,CAAI,CAAC,GAAA,KAAQ,GAAA,CAAI,GAAA,CAAI,mBAAmB,CAAA,CAAE,IAAA,CAAK,GAAI,CAAC,CAAA,CAAE,KAAK,IAAI,CAAA;AAC7E;AAEA,SAAS,oBAAoB,KAAA,EAA2B;AACtD,EAAA,IAAI,SAAS,IAAA,EAAM;AACjB,IAAA,OAAO,EAAA;AAAA,EACT;AAEA,EAAA,OAAO,OAAO,KAAK,CAAA;AACrB","file":"index.cjs","sourcesContent":["export type ClipboardCellValue = string | number | boolean | null | undefined;\n\n/**\n * 写入纯文本到系统剪贴板。\n */\nexport async function copyText(text: string, ownerDocument = document) {\n const clipboard = ownerDocument.defaultView?.navigator.clipboard;\n try {\n if (clipboard?.writeText) {\n await clipboard.writeText(text);\n return true;\n }\n } catch {\n // 非安全上下文或权限拒绝时回退到同步 DOM 复制。\n }\n\n return copyTextSync(text, ownerDocument);\n}\n\n/** 在当前用户激活事件中同步复制纯文本。 */\nexport function copyTextSync(text: string, ownerDocument = document) {\n const activeElement = ownerDocument.activeElement;\n const textarea = ownerDocument.createElement(\"textarea\");\n textarea.value = text;\n textarea.setAttribute(\"readonly\", \"true\");\n textarea.style.position = \"fixed\";\n textarea.style.left = \"-9999px\";\n textarea.style.top = \"0\";\n textarea.style.opacity = \"0\";\n\n ownerDocument.body.appendChild(textarea);\n textarea.focus({ preventScroll: true });\n textarea.select();\n textarea.setSelectionRange(0, textarea.value.length);\n\n let copied = false;\n try {\n copied = ownerDocument.execCommand?.(\"copy\") ?? false;\n } catch {\n copied = false;\n } finally {\n textarea.remove();\n const HTMLElementConstructor = ownerDocument.defaultView?.HTMLElement;\n if (\n HTMLElementConstructor &&\n activeElement instanceof HTMLElementConstructor\n ) {\n activeElement.focus({ preventScroll: true });\n }\n }\n return copied;\n}\n\n/**\n * 写入二维表格文本到系统剪贴板。\n */\nexport async function copyRows(rows: ClipboardCellValue[][]) {\n await copyText(formatRowsForClipboard(rows));\n}\n\n/**\n * 写入 JSON 文本到系统剪贴板。\n */\nexport async function copyJson(value: unknown, space = 2) {\n await copyText(JSON.stringify(value, null, space) ?? \"\");\n}\n\n/**\n * 格式化二维数据为剪贴板友好的文本。\n */\nexport function formatRowsForClipboard(rows: ClipboardCellValue[][]) {\n return rows.map((row) => row.map(formatClipboardCell).join(\"\\t\")).join(\"\\n\");\n}\n\nfunction formatClipboardCell(value: ClipboardCellValue) {\n if (value == null) {\n return \"\";\n }\n\n return String(value);\n}\n"]}
|
|
@@ -2,7 +2,7 @@ type ClipboardCellValue = string | number | boolean | null | undefined;
|
|
|
2
2
|
/**
|
|
3
3
|
* 写入纯文本到系统剪贴板。
|
|
4
4
|
*/
|
|
5
|
-
declare function copyText(text: string): Promise<
|
|
5
|
+
declare function copyText(text: string, ownerDocument?: Document): Promise<boolean>;
|
|
6
6
|
/** 在当前用户激活事件中同步复制纯文本。 */
|
|
7
7
|
declare function copyTextSync(text: string, ownerDocument?: Document): boolean;
|
|
8
8
|
/**
|
|
@@ -2,7 +2,7 @@ type ClipboardCellValue = string | number | boolean | null | undefined;
|
|
|
2
2
|
/**
|
|
3
3
|
* 写入纯文本到系统剪贴板。
|
|
4
4
|
*/
|
|
5
|
-
declare function copyText(text: string): Promise<
|
|
5
|
+
declare function copyText(text: string, ownerDocument?: Document): Promise<boolean>;
|
|
6
6
|
/** 在当前用户激活事件中同步复制纯文本。 */
|
|
7
7
|
declare function copyTextSync(text: string, ownerDocument?: Document): boolean;
|
|
8
8
|
/**
|
package/dist/clipboard/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
// src/clipboard/index.ts
|
|
2
|
-
async function copyText(text) {
|
|
2
|
+
async function copyText(text, ownerDocument = document) {
|
|
3
|
+
const clipboard = ownerDocument.defaultView?.navigator.clipboard;
|
|
3
4
|
try {
|
|
4
|
-
if (
|
|
5
|
-
await
|
|
6
|
-
return;
|
|
5
|
+
if (clipboard?.writeText) {
|
|
6
|
+
await clipboard.writeText(text);
|
|
7
|
+
return true;
|
|
7
8
|
}
|
|
8
9
|
} catch {
|
|
9
10
|
}
|
|
10
|
-
copyTextSync(text);
|
|
11
|
+
return copyTextSync(text, ownerDocument);
|
|
11
12
|
}
|
|
12
13
|
function copyTextSync(text, ownerDocument = document) {
|
|
13
14
|
const activeElement = ownerDocument.activeElement;
|
|
@@ -25,6 +26,8 @@ function copyTextSync(text, ownerDocument = document) {
|
|
|
25
26
|
let copied = false;
|
|
26
27
|
try {
|
|
27
28
|
copied = ownerDocument.execCommand?.("copy") ?? false;
|
|
29
|
+
} catch {
|
|
30
|
+
copied = false;
|
|
28
31
|
} finally {
|
|
29
32
|
textarea.remove();
|
|
30
33
|
const HTMLElementConstructor = ownerDocument.defaultView?.HTMLElement;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/clipboard/index.ts"],"names":[],"mappings":";AAKA,eAAsB,
|
|
1
|
+
{"version":3,"sources":["../../src/clipboard/index.ts"],"names":[],"mappings":";AAKA,eAAsB,QAAA,CAAS,IAAA,EAAc,aAAA,GAAgB,QAAA,EAAU;AACrE,EAAA,MAAM,SAAA,GAAY,aAAA,CAAc,WAAA,EAAa,SAAA,CAAU,SAAA;AACvD,EAAA,IAAI;AACF,IAAA,IAAI,WAAW,SAAA,EAAW;AACxB,MAAA,MAAM,SAAA,CAAU,UAAU,IAAI,CAAA;AAC9B,MAAA,OAAO,IAAA;AAAA,IACT;AAAA,EACF,CAAA,CAAA,MAAQ;AAAA,EAER;AAEA,EAAA,OAAO,YAAA,CAAa,MAAM,aAAa,CAAA;AACzC;AAGO,SAAS,YAAA,CAAa,IAAA,EAAc,aAAA,GAAgB,QAAA,EAAU;AACnE,EAAA,MAAM,gBAAgB,aAAA,CAAc,aAAA;AACpC,EAAA,MAAM,QAAA,GAAW,aAAA,CAAc,aAAA,CAAc,UAAU,CAAA;AACvD,EAAA,QAAA,CAAS,KAAA,GAAQ,IAAA;AACjB,EAAA,QAAA,CAAS,YAAA,CAAa,YAAY,MAAM,CAAA;AACxC,EAAA,QAAA,CAAS,MAAM,QAAA,GAAW,OAAA;AAC1B,EAAA,QAAA,CAAS,MAAM,IAAA,GAAO,SAAA;AACtB,EAAA,QAAA,CAAS,MAAM,GAAA,GAAM,GAAA;AACrB,EAAA,QAAA,CAAS,MAAM,OAAA,GAAU,GAAA;AAEzB,EAAA,aAAA,CAAc,IAAA,CAAK,YAAY,QAAQ,CAAA;AACvC,EAAA,QAAA,CAAS,KAAA,CAAM,EAAE,aAAA,EAAe,IAAA,EAAM,CAAA;AACtC,EAAA,QAAA,CAAS,MAAA,EAAO;AAChB,EAAA,QAAA,CAAS,iBAAA,CAAkB,CAAA,EAAG,QAAA,CAAS,KAAA,CAAM,MAAM,CAAA;AAEnD,EAAA,IAAI,MAAA,GAAS,KAAA;AACb,EAAA,IAAI;AACF,IAAA,MAAA,GAAS,aAAA,CAAc,WAAA,GAAc,MAAM,CAAA,IAAK,KAAA;AAAA,EAClD,CAAA,CAAA,MAAQ;AACN,IAAA,MAAA,GAAS,KAAA;AAAA,EACX,CAAA,SAAE;AACA,IAAA,QAAA,CAAS,MAAA,EAAO;AAChB,IAAA,MAAM,sBAAA,GAAyB,cAAc,WAAA,EAAa,WAAA;AAC1D,IAAA,IACE,sBAAA,IACA,yBAAyB,sBAAA,EACzB;AACA,MAAA,aAAA,CAAc,KAAA,CAAM,EAAE,aAAA,EAAe,IAAA,EAAM,CAAA;AAAA,IAC7C;AAAA,EACF;AACA,EAAA,OAAO,MAAA;AACT;AAKA,eAAsB,SAAS,IAAA,EAA8B;AAC3D,EAAA,MAAM,QAAA,CAAS,sBAAA,CAAuB,IAAI,CAAC,CAAA;AAC7C;AAKA,eAAsB,QAAA,CAAS,KAAA,EAAgB,KAAA,GAAQ,CAAA,EAAG;AACxD,EAAA,MAAM,SAAS,IAAA,CAAK,SAAA,CAAU,OAAO,IAAA,EAAM,KAAK,KAAK,EAAE,CAAA;AACzD;AAKO,SAAS,uBAAuB,IAAA,EAA8B;AACnE,EAAA,OAAO,IAAA,CAAK,GAAA,CAAI,CAAC,GAAA,KAAQ,GAAA,CAAI,GAAA,CAAI,mBAAmB,CAAA,CAAE,IAAA,CAAK,GAAI,CAAC,CAAA,CAAE,KAAK,IAAI,CAAA;AAC7E;AAEA,SAAS,oBAAoB,KAAA,EAA2B;AACtD,EAAA,IAAI,SAAS,IAAA,EAAM;AACjB,IAAA,OAAO,EAAA;AAAA,EACT;AAEA,EAAA,OAAO,OAAO,KAAK,CAAA;AACrB","file":"index.js","sourcesContent":["export type ClipboardCellValue = string | number | boolean | null | undefined;\n\n/**\n * 写入纯文本到系统剪贴板。\n */\nexport async function copyText(text: string, ownerDocument = document) {\n const clipboard = ownerDocument.defaultView?.navigator.clipboard;\n try {\n if (clipboard?.writeText) {\n await clipboard.writeText(text);\n return true;\n }\n } catch {\n // 非安全上下文或权限拒绝时回退到同步 DOM 复制。\n }\n\n return copyTextSync(text, ownerDocument);\n}\n\n/** 在当前用户激活事件中同步复制纯文本。 */\nexport function copyTextSync(text: string, ownerDocument = document) {\n const activeElement = ownerDocument.activeElement;\n const textarea = ownerDocument.createElement(\"textarea\");\n textarea.value = text;\n textarea.setAttribute(\"readonly\", \"true\");\n textarea.style.position = \"fixed\";\n textarea.style.left = \"-9999px\";\n textarea.style.top = \"0\";\n textarea.style.opacity = \"0\";\n\n ownerDocument.body.appendChild(textarea);\n textarea.focus({ preventScroll: true });\n textarea.select();\n textarea.setSelectionRange(0, textarea.value.length);\n\n let copied = false;\n try {\n copied = ownerDocument.execCommand?.(\"copy\") ?? false;\n } catch {\n copied = false;\n } finally {\n textarea.remove();\n const HTMLElementConstructor = ownerDocument.defaultView?.HTMLElement;\n if (\n HTMLElementConstructor &&\n activeElement instanceof HTMLElementConstructor\n ) {\n activeElement.focus({ preventScroll: true });\n }\n }\n return copied;\n}\n\n/**\n * 写入二维表格文本到系统剪贴板。\n */\nexport async function copyRows(rows: ClipboardCellValue[][]) {\n await copyText(formatRowsForClipboard(rows));\n}\n\n/**\n * 写入 JSON 文本到系统剪贴板。\n */\nexport async function copyJson(value: unknown, space = 2) {\n await copyText(JSON.stringify(value, null, space) ?? \"\");\n}\n\n/**\n * 格式化二维数据为剪贴板友好的文本。\n */\nexport function formatRowsForClipboard(rows: ClipboardCellValue[][]) {\n return rows.map((row) => row.map(formatClipboardCell).join(\"\\t\")).join(\"\\n\");\n}\n\nfunction formatClipboardCell(value: ClipboardCellValue) {\n if (value == null) {\n return \"\";\n }\n\n return String(value);\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/data/index.ts
|
|
4
|
+
function stringifyDataIndex(dataIndex) {
|
|
5
|
+
return (Array.isArray(dataIndex) ? dataIndex : [dataIndex]).map((item) => String(item)).join(".");
|
|
6
|
+
}
|
|
7
|
+
function isSameDataIndex(left, right) {
|
|
8
|
+
const leftPath = Array.isArray(left) ? left : [left];
|
|
9
|
+
const rightPath = Array.isArray(right) ? right : [right];
|
|
10
|
+
return leftPath.length === rightPath.length && leftPath.every((key, index) => key === rightPath[index]);
|
|
11
|
+
}
|
|
12
|
+
function getValueByDataIndex(source, dataIndex) {
|
|
13
|
+
if (dataIndex == null) {
|
|
14
|
+
return void 0;
|
|
15
|
+
}
|
|
16
|
+
const path = Array.isArray(dataIndex) ? dataIndex : [dataIndex];
|
|
17
|
+
let current = source;
|
|
18
|
+
for (const key of path) {
|
|
19
|
+
if (current == null || typeof current !== "object" && typeof current !== "function") {
|
|
20
|
+
return void 0;
|
|
21
|
+
}
|
|
22
|
+
current = current[key];
|
|
23
|
+
}
|
|
24
|
+
return current;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
exports.getValueByDataIndex = getValueByDataIndex;
|
|
28
|
+
exports.isSameDataIndex = isSameDataIndex;
|
|
29
|
+
exports.stringifyDataIndex = stringifyDataIndex;
|
|
30
|
+
//# sourceMappingURL=index.cjs.map
|
|
31
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/data/index.ts"],"names":[],"mappings":";;;AAQO,SAAS,mBAAmB,SAAA,EAA2B;AAC5D,EAAA,OAAA,CAAQ,MAAM,OAAA,CAAQ,SAAS,CAAA,GAAI,SAAA,GAAY,CAAC,SAAS,CAAA,EACtD,GAAA,CAAI,CAAC,SAAS,MAAA,CAAO,IAAI,CAAC,CAAA,CAC1B,KAAK,GAAG,CAAA;AACb;AAKO,SAAS,eAAA,CAAgB,MAAsB,KAAA,EAAuB;AAC3E,EAAA,MAAM,WAAW,KAAA,CAAM,OAAA,CAAQ,IAAI,CAAA,GAAI,IAAA,GAAO,CAAC,IAAI,CAAA;AACnD,EAAA,MAAM,YAAY,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,GAAI,KAAA,GAAQ,CAAC,KAAK,CAAA;AACvD,EAAA,OACE,QAAA,CAAS,MAAA,KAAW,SAAA,CAAU,MAAA,IAC9B,QAAA,CAAS,KAAA,CAAM,CAAC,GAAA,EAAK,KAAA,KAAU,GAAA,KAAQ,SAAA,CAAU,KAAK,CAAC,CAAA;AAE3D;AASO,SAAS,mBAAA,CACd,QACA,SAAA,EACA;AACA,EAAA,IAAI,aAAa,IAAA,EAAM;AACrB,IAAA,OAAO,MAAA;AAAA,EACT;AAEA,EAAA,MAAM,OAAO,KAAA,CAAM,OAAA,CAAQ,SAAS,CAAA,GAAI,SAAA,GAAY,CAAC,SAAS,CAAA;AAC9D,EAAA,IAAI,OAAA,GAAmB,MAAA;AAEvB,EAAA,KAAA,MAAW,OAAO,IAAA,EAAM;AACtB,IAAA,IACE,WAAW,IAAA,IACV,OAAO,YAAY,QAAA,IAAY,OAAO,YAAY,UAAA,EACnD;AACA,MAAA,OAAO,MAAA;AAAA,IACT;AACA,IAAA,OAAA,GAAW,QAA6C,GAAG,CAAA;AAAA,EAC7D;AAEA,EAAA,OAAO,OAAA;AACT","file":"index.cjs","sourcesContent":["/**\n * 支持对象属性路径的数据索引。\n */\nexport type ValueDataIndex = string | number | Array<string | number>;\n\n/**\n * 将 dataIndex 转换为稳定字符串。\n */\nexport function stringifyDataIndex(dataIndex: ValueDataIndex) {\n return (Array.isArray(dataIndex) ? dataIndex : [dataIndex])\n .map((item) => String(item))\n .join(\".\");\n}\n\n/**\n * 判断两个 dataIndex 是否指向相同属性路径。\n */\nexport function isSameDataIndex(left: ValueDataIndex, right: ValueDataIndex) {\n const leftPath = Array.isArray(left) ? left : [left];\n const rightPath = Array.isArray(right) ? right : [right];\n return (\n leftPath.length === rightPath.length &&\n leftPath.every((key, index) => key === rightPath[index])\n );\n}\n\n/**\n * 通过 dataIndex 安全读取值。\n *\n * @param source 数据记录。\n * @param dataIndex 属性路径。\n * @returns 读取结果。\n */\nexport function getValueByDataIndex(\n source: unknown,\n dataIndex?: ValueDataIndex,\n) {\n if (dataIndex == null) {\n return undefined;\n }\n\n const path = Array.isArray(dataIndex) ? dataIndex : [dataIndex];\n let current: unknown = source;\n\n for (const key of path) {\n if (\n current == null ||\n (typeof current !== \"object\" && typeof current !== \"function\")\n ) {\n return undefined;\n }\n current = (current as Record<string | number, unknown>)[key];\n }\n\n return current;\n}\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 支持对象属性路径的数据索引。
|
|
3
|
+
*/
|
|
4
|
+
type ValueDataIndex = string | number | Array<string | number>;
|
|
5
|
+
/**
|
|
6
|
+
* 将 dataIndex 转换为稳定字符串。
|
|
7
|
+
*/
|
|
8
|
+
declare function stringifyDataIndex(dataIndex: ValueDataIndex): string;
|
|
9
|
+
/**
|
|
10
|
+
* 判断两个 dataIndex 是否指向相同属性路径。
|
|
11
|
+
*/
|
|
12
|
+
declare function isSameDataIndex(left: ValueDataIndex, right: ValueDataIndex): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* 通过 dataIndex 安全读取值。
|
|
15
|
+
*
|
|
16
|
+
* @param source 数据记录。
|
|
17
|
+
* @param dataIndex 属性路径。
|
|
18
|
+
* @returns 读取结果。
|
|
19
|
+
*/
|
|
20
|
+
declare function getValueByDataIndex(source: unknown, dataIndex?: ValueDataIndex): unknown;
|
|
21
|
+
|
|
22
|
+
export { type ValueDataIndex, getValueByDataIndex, isSameDataIndex, stringifyDataIndex };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 支持对象属性路径的数据索引。
|
|
3
|
+
*/
|
|
4
|
+
type ValueDataIndex = string | number | Array<string | number>;
|
|
5
|
+
/**
|
|
6
|
+
* 将 dataIndex 转换为稳定字符串。
|
|
7
|
+
*/
|
|
8
|
+
declare function stringifyDataIndex(dataIndex: ValueDataIndex): string;
|
|
9
|
+
/**
|
|
10
|
+
* 判断两个 dataIndex 是否指向相同属性路径。
|
|
11
|
+
*/
|
|
12
|
+
declare function isSameDataIndex(left: ValueDataIndex, right: ValueDataIndex): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* 通过 dataIndex 安全读取值。
|
|
15
|
+
*
|
|
16
|
+
* @param source 数据记录。
|
|
17
|
+
* @param dataIndex 属性路径。
|
|
18
|
+
* @returns 读取结果。
|
|
19
|
+
*/
|
|
20
|
+
declare function getValueByDataIndex(source: unknown, dataIndex?: ValueDataIndex): unknown;
|
|
21
|
+
|
|
22
|
+
export { type ValueDataIndex, getValueByDataIndex, isSameDataIndex, stringifyDataIndex };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/data/index.ts
|
|
2
|
+
function stringifyDataIndex(dataIndex) {
|
|
3
|
+
return (Array.isArray(dataIndex) ? dataIndex : [dataIndex]).map((item) => String(item)).join(".");
|
|
4
|
+
}
|
|
5
|
+
function isSameDataIndex(left, right) {
|
|
6
|
+
const leftPath = Array.isArray(left) ? left : [left];
|
|
7
|
+
const rightPath = Array.isArray(right) ? right : [right];
|
|
8
|
+
return leftPath.length === rightPath.length && leftPath.every((key, index) => key === rightPath[index]);
|
|
9
|
+
}
|
|
10
|
+
function getValueByDataIndex(source, dataIndex) {
|
|
11
|
+
if (dataIndex == null) {
|
|
12
|
+
return void 0;
|
|
13
|
+
}
|
|
14
|
+
const path = Array.isArray(dataIndex) ? dataIndex : [dataIndex];
|
|
15
|
+
let current = source;
|
|
16
|
+
for (const key of path) {
|
|
17
|
+
if (current == null || typeof current !== "object" && typeof current !== "function") {
|
|
18
|
+
return void 0;
|
|
19
|
+
}
|
|
20
|
+
current = current[key];
|
|
21
|
+
}
|
|
22
|
+
return current;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { getValueByDataIndex, isSameDataIndex, stringifyDataIndex };
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/data/index.ts"],"names":[],"mappings":";AAQO,SAAS,mBAAmB,SAAA,EAA2B;AAC5D,EAAA,OAAA,CAAQ,MAAM,OAAA,CAAQ,SAAS,CAAA,GAAI,SAAA,GAAY,CAAC,SAAS,CAAA,EACtD,GAAA,CAAI,CAAC,SAAS,MAAA,CAAO,IAAI,CAAC,CAAA,CAC1B,KAAK,GAAG,CAAA;AACb;AAKO,SAAS,eAAA,CAAgB,MAAsB,KAAA,EAAuB;AAC3E,EAAA,MAAM,WAAW,KAAA,CAAM,OAAA,CAAQ,IAAI,CAAA,GAAI,IAAA,GAAO,CAAC,IAAI,CAAA;AACnD,EAAA,MAAM,YAAY,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,GAAI,KAAA,GAAQ,CAAC,KAAK,CAAA;AACvD,EAAA,OACE,QAAA,CAAS,MAAA,KAAW,SAAA,CAAU,MAAA,IAC9B,QAAA,CAAS,KAAA,CAAM,CAAC,GAAA,EAAK,KAAA,KAAU,GAAA,KAAQ,SAAA,CAAU,KAAK,CAAC,CAAA;AAE3D;AASO,SAAS,mBAAA,CACd,QACA,SAAA,EACA;AACA,EAAA,IAAI,aAAa,IAAA,EAAM;AACrB,IAAA,OAAO,MAAA;AAAA,EACT;AAEA,EAAA,MAAM,OAAO,KAAA,CAAM,OAAA,CAAQ,SAAS,CAAA,GAAI,SAAA,GAAY,CAAC,SAAS,CAAA;AAC9D,EAAA,IAAI,OAAA,GAAmB,MAAA;AAEvB,EAAA,KAAA,MAAW,OAAO,IAAA,EAAM;AACtB,IAAA,IACE,WAAW,IAAA,IACV,OAAO,YAAY,QAAA,IAAY,OAAO,YAAY,UAAA,EACnD;AACA,MAAA,OAAO,MAAA;AAAA,IACT;AACA,IAAA,OAAA,GAAW,QAA6C,GAAG,CAAA;AAAA,EAC7D;AAEA,EAAA,OAAO,OAAA;AACT","file":"index.js","sourcesContent":["/**\n * 支持对象属性路径的数据索引。\n */\nexport type ValueDataIndex = string | number | Array<string | number>;\n\n/**\n * 将 dataIndex 转换为稳定字符串。\n */\nexport function stringifyDataIndex(dataIndex: ValueDataIndex) {\n return (Array.isArray(dataIndex) ? dataIndex : [dataIndex])\n .map((item) => String(item))\n .join(\".\");\n}\n\n/**\n * 判断两个 dataIndex 是否指向相同属性路径。\n */\nexport function isSameDataIndex(left: ValueDataIndex, right: ValueDataIndex) {\n const leftPath = Array.isArray(left) ? left : [left];\n const rightPath = Array.isArray(right) ? right : [right];\n return (\n leftPath.length === rightPath.length &&\n leftPath.every((key, index) => key === rightPath[index])\n );\n}\n\n/**\n * 通过 dataIndex 安全读取值。\n *\n * @param source 数据记录。\n * @param dataIndex 属性路径。\n * @returns 读取结果。\n */\nexport function getValueByDataIndex(\n source: unknown,\n dataIndex?: ValueDataIndex,\n) {\n if (dataIndex == null) {\n return undefined;\n }\n\n const path = Array.isArray(dataIndex) ? dataIndex : [dataIndex];\n let current: unknown = source;\n\n for (const key of path) {\n if (\n current == null ||\n (typeof current !== \"object\" && typeof current !== \"function\")\n ) {\n return undefined;\n }\n current = (current as Record<string | number, unknown>)[key];\n }\n\n return current;\n}\n"]}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/date/index.ts
|
|
4
|
+
var DATE_TIME_FORMAT_TOKEN = /Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|S{1,3}|A|a/;
|
|
5
|
+
var FORMAT_TOKEN = /\[[^\]]*\]|YYYY|MMMM|MMM|dddd|ddd|SSS|YY|MM|DD|dd|HH|hh|mm|ss|SS|M|D|d|H|h|m|s|S|A|a/g;
|
|
6
|
+
var ZH_WEEKDAYS = [
|
|
7
|
+
"\u661F\u671F\u65E5",
|
|
8
|
+
"\u661F\u671F\u4E00",
|
|
9
|
+
"\u661F\u671F\u4E8C",
|
|
10
|
+
"\u661F\u671F\u4E09",
|
|
11
|
+
"\u661F\u671F\u56DB",
|
|
12
|
+
"\u661F\u671F\u4E94",
|
|
13
|
+
"\u661F\u671F\u516D"
|
|
14
|
+
];
|
|
15
|
+
var ZH_WEEKDAYS_SHORT = [
|
|
16
|
+
"\u5468\u65E5",
|
|
17
|
+
"\u5468\u4E00",
|
|
18
|
+
"\u5468\u4E8C",
|
|
19
|
+
"\u5468\u4E09",
|
|
20
|
+
"\u5468\u56DB",
|
|
21
|
+
"\u5468\u4E94",
|
|
22
|
+
"\u5468\u516D"
|
|
23
|
+
];
|
|
24
|
+
var EN_WEEKDAYS = [
|
|
25
|
+
"Sunday",
|
|
26
|
+
"Monday",
|
|
27
|
+
"Tuesday",
|
|
28
|
+
"Wednesday",
|
|
29
|
+
"Thursday",
|
|
30
|
+
"Friday",
|
|
31
|
+
"Saturday"
|
|
32
|
+
];
|
|
33
|
+
var EN_MONTHS = [
|
|
34
|
+
"January",
|
|
35
|
+
"February",
|
|
36
|
+
"March",
|
|
37
|
+
"April",
|
|
38
|
+
"May",
|
|
39
|
+
"June",
|
|
40
|
+
"July",
|
|
41
|
+
"August",
|
|
42
|
+
"September",
|
|
43
|
+
"October",
|
|
44
|
+
"November",
|
|
45
|
+
"December"
|
|
46
|
+
];
|
|
47
|
+
function resolveDateValue(value) {
|
|
48
|
+
if (value instanceof Date) {
|
|
49
|
+
return Number.isNaN(value.getTime()) ? null : value;
|
|
50
|
+
}
|
|
51
|
+
if (typeof value !== "string" && typeof value !== "number") {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
if (typeof value === "string") {
|
|
55
|
+
const localDate = resolveLocalDateString(value);
|
|
56
|
+
if (localDate) {
|
|
57
|
+
return localDate;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const resolved = new Date(value);
|
|
61
|
+
return Number.isNaN(resolved.getTime()) ? null : resolved;
|
|
62
|
+
}
|
|
63
|
+
function isDateTimeFormatPattern(pattern) {
|
|
64
|
+
return DATE_TIME_FORMAT_TOKEN.test(pattern.replace(/\[[^\]]*\]/g, ""));
|
|
65
|
+
}
|
|
66
|
+
function formatDateTimeValue(value, pattern, locale = "zh-cn") {
|
|
67
|
+
const date = resolveDateValue(value);
|
|
68
|
+
const normalizedPattern = pattern.trim();
|
|
69
|
+
if (!date || !normalizedPattern) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
const year = date.getFullYear();
|
|
73
|
+
const monthIndex = date.getMonth();
|
|
74
|
+
const month = monthIndex + 1;
|
|
75
|
+
const day = date.getDate();
|
|
76
|
+
const weekdayIndex = date.getDay();
|
|
77
|
+
const hour = date.getHours();
|
|
78
|
+
const hour12 = hour % 12 || 12;
|
|
79
|
+
const minute = date.getMinutes();
|
|
80
|
+
const second = date.getSeconds();
|
|
81
|
+
const millisecond = date.getMilliseconds();
|
|
82
|
+
const chinese = locale.toLowerCase().startsWith("zh");
|
|
83
|
+
const marker = hour >= 12 ? chinese ? "\u4E0B\u5348" : "PM" : chinese ? "\u4E0A\u5348" : "AM";
|
|
84
|
+
const weekday = chinese ? ZH_WEEKDAYS[weekdayIndex] ?? "" : EN_WEEKDAYS[weekdayIndex] ?? "";
|
|
85
|
+
const weekdayShort = chinese ? ZH_WEEKDAYS_SHORT[weekdayIndex] ?? "" : weekday.slice(0, 3);
|
|
86
|
+
const monthName = chinese ? `${month}\u6708` : EN_MONTHS[monthIndex] ?? "";
|
|
87
|
+
const values = {
|
|
88
|
+
YYYY: String(year).padStart(4, "0"),
|
|
89
|
+
YY: String(year).slice(-2),
|
|
90
|
+
M: String(month),
|
|
91
|
+
MM: padNumber(month),
|
|
92
|
+
MMM: chinese ? monthName : monthName.slice(0, 3),
|
|
93
|
+
MMMM: monthName,
|
|
94
|
+
D: String(day),
|
|
95
|
+
DD: padNumber(day),
|
|
96
|
+
d: String(weekdayIndex),
|
|
97
|
+
dd: chinese ? weekdayShort.slice(-1) : weekday.slice(0, 2),
|
|
98
|
+
ddd: weekdayShort,
|
|
99
|
+
dddd: weekday,
|
|
100
|
+
H: String(hour),
|
|
101
|
+
HH: padNumber(hour),
|
|
102
|
+
h: String(hour12),
|
|
103
|
+
hh: padNumber(hour12),
|
|
104
|
+
m: String(minute),
|
|
105
|
+
mm: padNumber(minute),
|
|
106
|
+
s: String(second),
|
|
107
|
+
ss: padNumber(second),
|
|
108
|
+
S: String(Math.floor(millisecond / 100)),
|
|
109
|
+
SS: String(Math.floor(millisecond / 10)).padStart(2, "0"),
|
|
110
|
+
SSS: String(millisecond).padStart(3, "0"),
|
|
111
|
+
A: marker,
|
|
112
|
+
a: marker.toLowerCase()
|
|
113
|
+
};
|
|
114
|
+
return normalizedPattern.replace(
|
|
115
|
+
FORMAT_TOKEN,
|
|
116
|
+
(token) => token.startsWith("[") && token.endsWith("]") ? token.slice(1, -1) : values[token] ?? token
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
function padNumber(value) {
|
|
120
|
+
return String(value).padStart(2, "0");
|
|
121
|
+
}
|
|
122
|
+
function resolveLocalDateString(value) {
|
|
123
|
+
const match = value.trim().match(
|
|
124
|
+
/^(\d{4})[-/](\d{1,2})[-/](\d{1,2})(?:[ T](\d{1,2}):?(\d{2})?(?::?(\d{2}))?(?:\.(\d{1,3}))?)?$/
|
|
125
|
+
);
|
|
126
|
+
if (!match) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
const [
|
|
130
|
+
,
|
|
131
|
+
yearText,
|
|
132
|
+
monthText,
|
|
133
|
+
dayText,
|
|
134
|
+
hourText,
|
|
135
|
+
minuteText,
|
|
136
|
+
secondText,
|
|
137
|
+
millisecondText
|
|
138
|
+
] = match;
|
|
139
|
+
const year = Number(yearText);
|
|
140
|
+
const monthIndex = Number(monthText) - 1;
|
|
141
|
+
const day = Number(dayText);
|
|
142
|
+
const hour = Number(hourText ?? 0);
|
|
143
|
+
const minute = Number(minuteText ?? 0);
|
|
144
|
+
const second = Number(secondText ?? 0);
|
|
145
|
+
const millisecond = Number((millisecondText ?? "0").padEnd(3, "0"));
|
|
146
|
+
const resolved = new Date(
|
|
147
|
+
year,
|
|
148
|
+
monthIndex,
|
|
149
|
+
day,
|
|
150
|
+
hour,
|
|
151
|
+
minute,
|
|
152
|
+
second,
|
|
153
|
+
millisecond
|
|
154
|
+
);
|
|
155
|
+
return resolved.getFullYear() === year && resolved.getMonth() === monthIndex && resolved.getDate() === day && resolved.getHours() === hour && resolved.getMinutes() === minute && resolved.getSeconds() === second ? resolved : null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
exports.formatDateTimeValue = formatDateTimeValue;
|
|
159
|
+
exports.isDateTimeFormatPattern = isDateTimeFormatPattern;
|
|
160
|
+
exports.resolveDateValue = resolveDateValue;
|
|
161
|
+
//# sourceMappingURL=index.cjs.map
|
|
162
|
+
//# sourceMappingURL=index.cjs.map
|