@ysgroup/core 0.1.29 → 0.1.31
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 +2 -2
- package/src/calendar.ts +165 -0
- package/src/index.ts +42 -1
- package/src/status.ts +115 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ysgroup/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.31",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "src/index.ts",
|
|
@@ -23,6 +23,6 @@
|
|
|
23
23
|
"access": "public"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@ysgroup/theme": "^0.3.
|
|
26
|
+
"@ysgroup/theme": "^0.3.3"
|
|
27
27
|
}
|
|
28
28
|
}
|
package/src/calendar.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 日历用的日期纯函数和日期信息类型(YsCalendar 用,业务页也可以直接用)。
|
|
3
|
+
*
|
|
4
|
+
* - 日期一律是 `YYYY-MM-DD` 字符串、月份是 `YYYY-MM`,按日历日计算(内部用 UTC,不受浏览器时区和夏令时影响)。
|
|
5
|
+
* - 「今天」按上海时间(`ysToday`),和服务端 ys_base.calendar 一致。
|
|
6
|
+
* - 一周从周一开始。
|
|
7
|
+
* - 工作日、交易日由服务端 ys_base.calendar 判断后下发(YsCalendarDay),前端只显示,不自己按周一到周五猜;
|
|
8
|
+
* 交易控制一律在服务端判断。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** workday 工作日、weekend 周末、holiday 放假、adjusted 调休上班。 */
|
|
12
|
+
export type YsDayKind = "workday" | "weekend" | "holiday" | "adjusted";
|
|
13
|
+
|
|
14
|
+
/** 一天的信息(服务端 ys_base.calendar 的 DayInfo.to_dict,covered=false 表示这一年还没有放假安排)。 */
|
|
15
|
+
export interface YsCalendarDay {
|
|
16
|
+
date: string;
|
|
17
|
+
kind: YsDayKind;
|
|
18
|
+
name?: string;
|
|
19
|
+
label?: string;
|
|
20
|
+
workday?: boolean;
|
|
21
|
+
trading?: boolean;
|
|
22
|
+
covered?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface YsMonthCell {
|
|
26
|
+
date: string;
|
|
27
|
+
/** 是否属于当前月(首尾补的上月、下月日期为 false)。 */
|
|
28
|
+
inMonth: boolean;
|
|
29
|
+
/** 0 = 周一 … 6 = 周日。 */
|
|
30
|
+
weekday: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const YS_WEEKDAY_LABELS = [
|
|
34
|
+
"一",
|
|
35
|
+
"二",
|
|
36
|
+
"三",
|
|
37
|
+
"四",
|
|
38
|
+
"五",
|
|
39
|
+
"六",
|
|
40
|
+
"日",
|
|
41
|
+
] as const;
|
|
42
|
+
|
|
43
|
+
export const YS_DAY_KIND_LABELS: Record<YsDayKind, string> = {
|
|
44
|
+
workday: "工作日",
|
|
45
|
+
weekend: "周末",
|
|
46
|
+
holiday: "放假",
|
|
47
|
+
adjusted: "调休上班",
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const DATE_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
51
|
+
const MONTH_RE = /^(\d{4})-(\d{2})$/;
|
|
52
|
+
const DAY_MS = 86_400_000;
|
|
53
|
+
const pad = (value: number) => String(value).padStart(2, "0");
|
|
54
|
+
|
|
55
|
+
function toUtc(date: string): number | null {
|
|
56
|
+
const match = DATE_RE.exec(date);
|
|
57
|
+
if (!match) return null;
|
|
58
|
+
const [year, month, day] = [
|
|
59
|
+
Number(match[1]),
|
|
60
|
+
Number(match[2]),
|
|
61
|
+
Number(match[3]),
|
|
62
|
+
];
|
|
63
|
+
const time = Date.UTC(year, month - 1, day);
|
|
64
|
+
const back = new Date(time);
|
|
65
|
+
// 2027-02-30 这类不存在的日期会被 Date 滚到下个月,这里拒绝
|
|
66
|
+
return back.getUTCFullYear() === year &&
|
|
67
|
+
back.getUTCMonth() === month - 1 &&
|
|
68
|
+
back.getUTCDate() === day
|
|
69
|
+
? time
|
|
70
|
+
: null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function fromUtc(time: number): string {
|
|
74
|
+
const date = new Date(time);
|
|
75
|
+
return `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 是不是存在的日期(格式对、月和日合法,2 月 29 日只在闰年有)。 */
|
|
79
|
+
export function ysIsDate(value: unknown): value is string {
|
|
80
|
+
return typeof value === "string" && toUtc(value) !== null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 上海时间的今天。 */
|
|
84
|
+
export function ysToday(now: Date = new Date()): string {
|
|
85
|
+
return new Intl.DateTimeFormat("sv-SE", { timeZone: "Asia/Shanghai" }).format(
|
|
86
|
+
now,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** 加减天数;日期不合法时原样返回。 */
|
|
91
|
+
export function ysAddDays(date: string, days: number): string {
|
|
92
|
+
const time = toUtc(date);
|
|
93
|
+
return time === null ? date : fromUtc(time + days * DAY_MS);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** 两个日期相差几天(to - from)。 */
|
|
97
|
+
export function ysDaysBetween(from: string, to: string): number {
|
|
98
|
+
const a = toUtc(from);
|
|
99
|
+
const b = toUtc(to);
|
|
100
|
+
return a === null || b === null ? Number.NaN : Math.round((b - a) / DAY_MS);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** 0 = 周一 … 6 = 周日;日期不合法时 -1。 */
|
|
104
|
+
export function ysWeekday(date: string): number {
|
|
105
|
+
const time = toUtc(date);
|
|
106
|
+
return time === null ? -1 : (new Date(time).getUTCDay() + 6) % 7;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 「周一」…「周日」。 */
|
|
110
|
+
export function ysWeekdayLabel(date: string): string {
|
|
111
|
+
const index = ysWeekday(date);
|
|
112
|
+
return index < 0 ? "" : `周${YS_WEEKDAY_LABELS[index]}`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 日期所在月份 `YYYY-MM`。 */
|
|
116
|
+
export function ysMonthOf(date: string): string {
|
|
117
|
+
return date.slice(0, 7);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** 月份加减;`2026-01` 减 1 得 `2025-12`。格式不对时原样返回。 */
|
|
121
|
+
export function ysShiftMonth(month: string, delta: number): string {
|
|
122
|
+
const match = MONTH_RE.exec(month);
|
|
123
|
+
if (!match) return month;
|
|
124
|
+
const index = Number(match[1]) * 12 + Number(match[2]) - 1 + delta;
|
|
125
|
+
return `${Math.floor(index / 12)}-${pad((index % 12) + 1)}`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** 这个月有几天。 */
|
|
129
|
+
export function ysDaysInMonth(month: string): number {
|
|
130
|
+
const match = MONTH_RE.exec(month);
|
|
131
|
+
if (!match) return 0;
|
|
132
|
+
return new Date(Date.UTC(Number(match[1]), Number(match[2]), 0)).getUTCDate();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 月历网格:固定 6 行 × 7 列(周一开头),首行补上月尾、末行补下月头,翻月时高度不跳。 */
|
|
136
|
+
export function ysMonthGrid(month: string): YsMonthCell[] {
|
|
137
|
+
if (!MONTH_RE.test(month)) return [];
|
|
138
|
+
const first = `${month}-01`;
|
|
139
|
+
const start = ysAddDays(first, -ysWeekday(first));
|
|
140
|
+
return Array.from({ length: 42 }, (_, index) => {
|
|
141
|
+
const date = ysAddDays(start, index);
|
|
142
|
+
return { date, inMonth: date.startsWith(month), weekday: index % 7 };
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** 月历网格覆盖的闭区间(按这个范围向服务端取日期信息)。 */
|
|
147
|
+
export function ysMonthRange(month: string): { from: string; to: string } {
|
|
148
|
+
const grid = ysMonthGrid(month);
|
|
149
|
+
return grid.length
|
|
150
|
+
? { from: grid[0]!.date, to: grid[41]!.date }
|
|
151
|
+
: { from: "", to: "" };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** 「2026 年 10 月」。 */
|
|
155
|
+
export function ysMonthTitle(month: string): string {
|
|
156
|
+
const match = MONTH_RE.exec(month);
|
|
157
|
+
return match ? `${match[1]} 年 ${Number(match[2])} 月` : "";
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 「2026 年 10 月 1 日 周四」。 */
|
|
161
|
+
export function ysDateTitle(date: string): string {
|
|
162
|
+
const match = DATE_RE.exec(date);
|
|
163
|
+
if (!match || !ysIsDate(date)) return "";
|
|
164
|
+
return `${match[1]} 年 ${Number(match[2])} 月 ${Number(match[3])} 日 ${ysWeekdayLabel(date)}`;
|
|
165
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -17,7 +17,14 @@ export {
|
|
|
17
17
|
type YsDisconnectInfo,
|
|
18
18
|
type YsWsClientOptions,
|
|
19
19
|
} from "./ws";
|
|
20
|
-
export {
|
|
20
|
+
export {
|
|
21
|
+
formatAmount,
|
|
22
|
+
formatNumber,
|
|
23
|
+
splitAmount,
|
|
24
|
+
type YsAmountParts,
|
|
25
|
+
type YsAmountUnit,
|
|
26
|
+
type YsNumberFormatOptions,
|
|
27
|
+
} from "./number";
|
|
21
28
|
export {
|
|
22
29
|
currentEntry,
|
|
23
30
|
latestEntry,
|
|
@@ -97,3 +104,37 @@ export {
|
|
|
97
104
|
type YsCatalogStore,
|
|
98
105
|
type YsCatalogStoreDefinition,
|
|
99
106
|
} from "./catalog";
|
|
107
|
+
export {
|
|
108
|
+
YS_STATUS_LABELS,
|
|
109
|
+
YS_STATUS_NAMES,
|
|
110
|
+
YS_STATUS_OPTIONS,
|
|
111
|
+
ysRecordStatus,
|
|
112
|
+
ysStatusCounts,
|
|
113
|
+
ysStatusEmptyText,
|
|
114
|
+
ysStatusRevealHint,
|
|
115
|
+
ysStatusToggle,
|
|
116
|
+
ysStatusVisible,
|
|
117
|
+
type YsRecordStatus,
|
|
118
|
+
type YsStatusCounts,
|
|
119
|
+
type YsStatusOption,
|
|
120
|
+
} from "./status";
|
|
121
|
+
export {
|
|
122
|
+
YS_DAY_KIND_LABELS,
|
|
123
|
+
YS_WEEKDAY_LABELS,
|
|
124
|
+
ysAddDays,
|
|
125
|
+
ysDateTitle,
|
|
126
|
+
ysDaysBetween,
|
|
127
|
+
ysDaysInMonth,
|
|
128
|
+
ysIsDate,
|
|
129
|
+
ysMonthGrid,
|
|
130
|
+
ysMonthOf,
|
|
131
|
+
ysMonthRange,
|
|
132
|
+
ysMonthTitle,
|
|
133
|
+
ysShiftMonth,
|
|
134
|
+
ysToday,
|
|
135
|
+
ysWeekday,
|
|
136
|
+
ysWeekdayLabel,
|
|
137
|
+
type YsCalendarDay,
|
|
138
|
+
type YsDayKind,
|
|
139
|
+
type YsMonthCell,
|
|
140
|
+
} from "./calendar";
|
package/src/status.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 列表记录的四种状态和表格上方的状态筛选(规范:启用与归档、表格检索区)。
|
|
3
|
+
*
|
|
4
|
+
* - 每条记录只属于一种状态,按优先级判断:已删除 > 已归档 > 已停用 > 启用。
|
|
5
|
+
* 已归档的记录必然停用,但只算「已归档」;没有 enabled 字段的记录算启用。
|
|
6
|
+
* - 列表默认只显示启用的记录;表格上方三个勾选框「显示已停用」「显示已归档」「显示软删除」各看一类,
|
|
7
|
+
* 三者互斥(勾一个就取消另外两个),都不勾回到启用。所以筛选状态只需一个值:当前看哪一类。
|
|
8
|
+
* - 服务端分页的表把这个值原样作为查询条件(如 status: "enabled"),默认 "enabled"。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export type YsRecordStatus = "enabled" | "disabled" | "archived" | "deleted";
|
|
12
|
+
/** 勾选框对应的三类(启用是都不勾时的默认)。 */
|
|
13
|
+
export type YsStatusOption = Exclude<YsRecordStatus, "enabled">;
|
|
14
|
+
export type YsStatusCounts = Record<YsRecordStatus, number>;
|
|
15
|
+
|
|
16
|
+
/** 勾选框固定顺序。 */
|
|
17
|
+
export const YS_STATUS_OPTIONS: readonly YsStatusOption[] = [
|
|
18
|
+
"disabled",
|
|
19
|
+
"archived",
|
|
20
|
+
"deleted",
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
/** 勾选框文字。 */
|
|
24
|
+
export const YS_STATUS_LABELS: Record<YsStatusOption, string> = {
|
|
25
|
+
disabled: "显示已停用",
|
|
26
|
+
archived: "显示已归档",
|
|
27
|
+
deleted: "显示软删除",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** 状态名称(提示文字里用)。 */
|
|
31
|
+
export const YS_STATUS_NAMES: Record<YsRecordStatus, string> = {
|
|
32
|
+
enabled: "启用",
|
|
33
|
+
disabled: "已停用",
|
|
34
|
+
archived: "已归档",
|
|
35
|
+
deleted: "已删除",
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
type StatusRow = { enabled?: unknown; archived?: unknown; deleted?: unknown };
|
|
39
|
+
|
|
40
|
+
/** 一条记录属于哪种状态。`deleted`、`archived` 只认 true;`enabled` 只有 false 才算停用。 */
|
|
41
|
+
export function ysRecordStatus(row: StatusRow): YsRecordStatus {
|
|
42
|
+
if (row.deleted === true) return "deleted";
|
|
43
|
+
if (row.archived === true) return "archived";
|
|
44
|
+
if (row.enabled === false) return "disabled";
|
|
45
|
+
return "enabled";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 记录是否属于当前查看的那一类。 */
|
|
49
|
+
export function ysStatusVisible(row: StatusRow, view: YsRecordStatus): boolean {
|
|
50
|
+
return ysRecordStatus(row) === view;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 四种状态各有几条(勾选框后面的数量、空表提示用)。 */
|
|
54
|
+
export function ysStatusCounts(rows: readonly StatusRow[]): YsStatusCounts {
|
|
55
|
+
const counts: YsStatusCounts = {
|
|
56
|
+
enabled: 0,
|
|
57
|
+
disabled: 0,
|
|
58
|
+
archived: 0,
|
|
59
|
+
deleted: 0,
|
|
60
|
+
};
|
|
61
|
+
for (const row of rows) counts[ysRecordStatus(row)] += 1;
|
|
62
|
+
return counts;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 勾选框变化后的查看状态:勾上就看这一类,取消勾选回到启用。 */
|
|
66
|
+
export function ysStatusToggle(
|
|
67
|
+
option: YsStatusOption,
|
|
68
|
+
checked: boolean,
|
|
69
|
+
): YsRecordStatus {
|
|
70
|
+
return checked ? option : "enabled";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 操作后记录离开当前列表时,告诉用户去哪里找它;还在当前列表里返回空串。
|
|
75
|
+
* 例:默认列表里把记录改成停用 →「勾选「显示已停用」可查看」;在「显示已归档」里取消归档 →「取消勾选「显示已归档」可查看」或「勾选「显示已停用」可查看」。
|
|
76
|
+
*/
|
|
77
|
+
export function ysStatusRevealHint(
|
|
78
|
+
view: YsRecordStatus,
|
|
79
|
+
status: YsRecordStatus,
|
|
80
|
+
): string {
|
|
81
|
+
if (view === status) return "";
|
|
82
|
+
if (status === "enabled")
|
|
83
|
+
return `取消勾选「${YS_STATUS_LABELS[view as YsStatusOption]}」可查看`;
|
|
84
|
+
return `勾选「${YS_STATUS_LABELS[status]}」可查看`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 当前查看的一类没有记录时,空表里说明另有几条在哪里;默认列表且别处也没有记录时返回空串(照常写「暂无数据」)。
|
|
89
|
+
* counts 按当前搜索和筛选范围统计,有搜索词时也能用。
|
|
90
|
+
* 例:「没有启用的记录。已停用 3 条,勾选上方「显示已停用」查看」;多类时「…已停用 3 条、已删除 2 条,勾选上方对应选项查看」。
|
|
91
|
+
*/
|
|
92
|
+
export function ysStatusEmptyText(
|
|
93
|
+
view: YsRecordStatus,
|
|
94
|
+
counts: YsStatusCounts,
|
|
95
|
+
options: readonly YsStatusOption[] = YS_STATUS_OPTIONS,
|
|
96
|
+
): string {
|
|
97
|
+
if (counts[view] > 0) return "";
|
|
98
|
+
const others = options.filter(
|
|
99
|
+
(option) => option !== view && counts[option] > 0,
|
|
100
|
+
);
|
|
101
|
+
// 表里没有启用字段(options 不含 disabled)时不说「启用的」,免得误解成有启停
|
|
102
|
+
const hasEnabled = options.includes("disabled");
|
|
103
|
+
if (view !== "enabled")
|
|
104
|
+
return counts.enabled > 0
|
|
105
|
+
? `没有${YS_STATUS_NAMES[view]}的记录,取消勾选可查看${hasEnabled ? "启用的" : "其余"} ${counts.enabled} 条`
|
|
106
|
+
: `没有${YS_STATUS_NAMES[view]}的记录`;
|
|
107
|
+
if (!others.length) return "";
|
|
108
|
+
const parts = others
|
|
109
|
+
.map((option) => `${YS_STATUS_NAMES[option]} ${counts[option]} 条`)
|
|
110
|
+
.join("、");
|
|
111
|
+
// 只有一类时点名勾哪个;多类时不逐个列勾选框名,免得空表里折成两行
|
|
112
|
+
const where =
|
|
113
|
+
others.length === 1 ? `「${YS_STATUS_LABELS[others[0]!]}」` : "对应选项";
|
|
114
|
+
return `${hasEnabled ? "没有启用的记录" : "当前没有记录"}。${parts},勾选上方${where}查看`;
|
|
115
|
+
}
|