cn-era 0.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/LICENSE +21 -0
- package/README.md +215 -0
- package/README.zh-CN.md +202 -0
- package/dist/index.cjs +223 -0
- package/dist/index.d.cts +41 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +196 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Frank Lin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# cn-era
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/cn-era)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
> Convert Gregorian calendar years to Chinese historical era names (年號)
|
|
7
|
+
|
|
8
|
+
A lightweight JavaScript/TypeScript library for converting Gregorian calendar years to corresponding Chinese dynasty era names and year numbers.
|
|
9
|
+
|
|
10
|
+
[中文文档](./README.zh-CN.md)
|
|
11
|
+
|
|
12
|
+
## Current Status
|
|
13
|
+
|
|
14
|
+
**Early Development (v0.1.0)**
|
|
15
|
+
|
|
16
|
+
This library is in early development. Currently includes:
|
|
17
|
+
- Tang Dynasty era data (618-710 CE)
|
|
18
|
+
- Wu Zhou era data (684-705 CE)
|
|
19
|
+
- Republic of China era (1912-present)
|
|
20
|
+
|
|
21
|
+
We are actively working on adding complete historical era data. Contributions are welcome! See [TODO.md](TODO.md) for the data completion roadmap.
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- Accurate era conversion: Supports historical era names from Qin Dynasty to Qing Dynasty
|
|
26
|
+
- Comprehensive historical data: Covers all major Chinese dynasties
|
|
27
|
+
- Multiple era support: Handles cases where multiple era names coexist in the same year (e.g., era changes)
|
|
28
|
+
- Zero dependencies: No additional packages required
|
|
29
|
+
- TypeScript support: Complete type definitions included
|
|
30
|
+
- Lightweight: Small bundle size, minimal impact on your project
|
|
31
|
+
- Traditional Chinese output: All era names in Traditional Chinese characters
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm install cn-era
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Or using yarn:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
yarn add cn-era
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Or using pnpm:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pnpm add cn-era
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Quick Start
|
|
52
|
+
|
|
53
|
+
**ESM (ES Modules)**
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
import { convertYear } from 'cn-era';
|
|
57
|
+
|
|
58
|
+
// Basic usage
|
|
59
|
+
const result = convertYear(618);
|
|
60
|
+
console.log(result);
|
|
61
|
+
// Output: [{ dynasty: '唐', reign_title: '武德', year_num: '元年' }]
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**CommonJS**
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
const { convertYear } = require('cn-era');
|
|
68
|
+
|
|
69
|
+
const result = convertYear(618);
|
|
70
|
+
console.log(result);
|
|
71
|
+
// Output: [{ dynasty: '唐', reign_title: '武德', year_num: '元年' }]
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
**More Examples**
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
import { convertYear } from 'cn-era';
|
|
78
|
+
|
|
79
|
+
// Multiple eras in one year (era change)
|
|
80
|
+
convertYear(690);
|
|
81
|
+
// [
|
|
82
|
+
// { dynasty: '唐', reign_title: '載初', year_num: '二年' },
|
|
83
|
+
// { dynasty: '武周', reign_title: '天授', year_num: '元年' }
|
|
84
|
+
// ]
|
|
85
|
+
|
|
86
|
+
// Republic of China era (post-1912)
|
|
87
|
+
convertYear(2024);
|
|
88
|
+
// [{ dynasty: '中華民國', reign_title: '民國', year_num: '一百一十三年' }]
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## API Documentation
|
|
92
|
+
|
|
93
|
+
### `convertYear(year: number): EraResult[]`
|
|
94
|
+
|
|
95
|
+
Convert a Gregorian calendar year to Chinese historical era name(s).
|
|
96
|
+
|
|
97
|
+
#### Parameters
|
|
98
|
+
|
|
99
|
+
- `year` (number): Gregorian calendar year (range: -841 to 3000, 0 is invalid)
|
|
100
|
+
|
|
101
|
+
#### Returns
|
|
102
|
+
|
|
103
|
+
Returns an array containing all era information for that year (some years may have multiple eras).
|
|
104
|
+
|
|
105
|
+
Each result object contains the following fields:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
interface EraResult {
|
|
109
|
+
dynasty: string; // Dynasty name, e.g., "唐", "宋"
|
|
110
|
+
reign_title: string; // Era name, e.g., "武德", "貞觀"
|
|
111
|
+
year_num: string; // Year in era, e.g., "元年", "三年"
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
#### Examples
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
// Regular year
|
|
119
|
+
convertYear(627);
|
|
120
|
+
// [{ dynasty: '唐', reign_title: '貞觀', year_num: '元年' }]
|
|
121
|
+
|
|
122
|
+
// Era change year (multiple eras in one year)
|
|
123
|
+
convertYear(626);
|
|
124
|
+
// [
|
|
125
|
+
// { dynasty: '唐', reign_title: '武德', year_num: '九年' },
|
|
126
|
+
// { dynasty: '唐', reign_title: '貞觀', year_num: '元年' }
|
|
127
|
+
// ]
|
|
128
|
+
|
|
129
|
+
// Coexisting regimes (e.g., Northern and Southern Dynasties)
|
|
130
|
+
convertYear(420);
|
|
131
|
+
// [
|
|
132
|
+
// { dynasty: '南朝宋', reign_title: '永初', year_num: '元年' },
|
|
133
|
+
// { dynasty: '北魏', reign_title: '泰常', year_num: '五年' }
|
|
134
|
+
// ]
|
|
135
|
+
|
|
136
|
+
// Republic of China
|
|
137
|
+
convertYear(1912);
|
|
138
|
+
// [{ dynasty: '中華民國', reign_title: '民國', year_num: '元年' }]
|
|
139
|
+
|
|
140
|
+
// BCE year
|
|
141
|
+
convertYear(-140);
|
|
142
|
+
// [{ dynasty: '西漢', reign_title: '建元', year_num: '元年' }]
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Supported Historical Periods
|
|
146
|
+
|
|
147
|
+
This library supports era name conversion from **Western Zhou Gonghe Regency (841 BCE)** to **Republic of China (1912-)**, covering:
|
|
148
|
+
|
|
149
|
+
- Qin Dynasty, Han Dynasty (Western Han, Eastern Han)
|
|
150
|
+
- Three Kingdoms (Wei, Shu, Wu)
|
|
151
|
+
- Jin Dynasty (Western Jin, Eastern Jin), Northern and Southern Dynasties
|
|
152
|
+
- Sui Dynasty, Tang Dynasty
|
|
153
|
+
- Five Dynasties and Ten Kingdoms
|
|
154
|
+
- Song, Liao, Jin, Western Xia (coexisting regimes)
|
|
155
|
+
- Yuan Dynasty
|
|
156
|
+
- Ming Dynasty
|
|
157
|
+
- Qing Dynasty
|
|
158
|
+
- Republic of China (1912-)
|
|
159
|
+
|
|
160
|
+
## Use Cases
|
|
161
|
+
|
|
162
|
+
- Historical research: Quickly look up era names for historical events
|
|
163
|
+
- Game development: Time systems for historical-themed games
|
|
164
|
+
- Cultural applications: Classical text reading, historical education apps
|
|
165
|
+
- Date conversion: Convert between Gregorian and traditional Chinese calendar
|
|
166
|
+
- Content creation: Automatically annotate historical content with era names
|
|
167
|
+
|
|
168
|
+
## Important Notes
|
|
169
|
+
|
|
170
|
+
1. **Era changes**: Some years return multiple results, which typically occurs when:
|
|
171
|
+
- Emperor changes era name (multiple era names within the same year)
|
|
172
|
+
- Dynasty transitions
|
|
173
|
+
- Coexisting regimes during periods of division
|
|
174
|
+
|
|
175
|
+
2. **Year range**: Currently supports 841 BCE to 3000 CE. Years outside this range will throw an error.
|
|
176
|
+
|
|
177
|
+
3. **Historical accuracy**: Era name data is based on mainstream historical records. Disputed years follow standard histories.
|
|
178
|
+
|
|
179
|
+
4. **BCE years**: Negative numbers represent BCE years (e.g., -221 = 221 BCE)
|
|
180
|
+
|
|
181
|
+
5. **Traditional Chinese**: All output is in Traditional Chinese characters, which matches historical documents. Users can convert to Simplified Chinese as needed.
|
|
182
|
+
|
|
183
|
+
## Development
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
# Clone repository
|
|
187
|
+
git clone https://github.com/frankslin/cn-era.git
|
|
188
|
+
|
|
189
|
+
# Install dependencies
|
|
190
|
+
npm install
|
|
191
|
+
|
|
192
|
+
# Run tests
|
|
193
|
+
npm test
|
|
194
|
+
|
|
195
|
+
# Build
|
|
196
|
+
npm run build
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Contributing
|
|
200
|
+
|
|
201
|
+
Issues and Pull Requests are welcome!
|
|
202
|
+
|
|
203
|
+
If you find any errors in historical data, please provide reliable historical sources.
|
|
204
|
+
|
|
205
|
+
## License
|
|
206
|
+
|
|
207
|
+
[MIT](LICENSE)
|
|
208
|
+
|
|
209
|
+
## Acknowledgments
|
|
210
|
+
|
|
211
|
+
Thanks to all scholars and open-source contributors who have contributed to Chinese historical research.
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
If this project helps you, feel free to Star it!
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# cn-era
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/cn-era)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
> 将公元年份转换为中国历史年号纪年
|
|
7
|
+
|
|
8
|
+
一个轻量级的 JavaScript/TypeScript 库,用于将公元年份转换为对应的中国历史朝代年号和年份。
|
|
9
|
+
|
|
10
|
+
[English](./README.md)
|
|
11
|
+
|
|
12
|
+
## 当前状态
|
|
13
|
+
|
|
14
|
+
**早期开发版本 (v0.1.0)**
|
|
15
|
+
|
|
16
|
+
本库目前处于早期开发阶段,已包含数据:
|
|
17
|
+
- 唐朝年号数据(618-710 年)
|
|
18
|
+
- 武周年号数据(684-705 年)
|
|
19
|
+
- 中华民国纪年(1912 年至今)
|
|
20
|
+
|
|
21
|
+
我们正在积极补充完整的历史年号数据。欢迎贡献!请查看 [TODO.md](TODO.md) 了解数据补充计划。
|
|
22
|
+
|
|
23
|
+
## 特性
|
|
24
|
+
|
|
25
|
+
- 精确的年号转换:支持从秦朝到清朝的历史年号
|
|
26
|
+
- 完整的历史数据:涵盖中国历史各主要朝代
|
|
27
|
+
- 多年号支持:处理同一年份多个年号并存的情况(如改元)
|
|
28
|
+
- 零依赖:无需额外安装其他包
|
|
29
|
+
- TypeScript 支持:提供完整的类型定义
|
|
30
|
+
- 轻量化:体积小巧,不影响项目大小
|
|
31
|
+
- 繁体输出:所有年号使用繁体中文,符合历史文献原貌
|
|
32
|
+
|
|
33
|
+
## 安装
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm install cn-era
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
或使用 yarn:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
yarn add cn-era
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
或使用 pnpm:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pnpm add cn-era
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## 快速开始
|
|
52
|
+
|
|
53
|
+
**ESM (ES Modules)**
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
import { convertYear } from 'cn-era';
|
|
57
|
+
|
|
58
|
+
// 基础用法
|
|
59
|
+
const result = convertYear(618);
|
|
60
|
+
console.log(result);
|
|
61
|
+
// 输出: [{ dynasty: '唐', reign_title: '武德', year_num: '元年' }]
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**CommonJS**
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
const { convertYear } = require('cn-era');
|
|
68
|
+
|
|
69
|
+
const result = convertYear(618);
|
|
70
|
+
console.log(result);
|
|
71
|
+
// 输出: [{ dynasty: '唐', reign_title: '武德', year_num: '元年' }]
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
**更多示例**
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
import { convertYear } from 'cn-era';
|
|
78
|
+
|
|
79
|
+
// 多个年号的情况(改元年份)
|
|
80
|
+
convertYear(690);
|
|
81
|
+
// [
|
|
82
|
+
// { dynasty: '唐', reign_title: '載初', year_num: '二年' },
|
|
83
|
+
// { dynasty: '武周', reign_title: '天授', year_num: '元年' }
|
|
84
|
+
// ]
|
|
85
|
+
|
|
86
|
+
// 民国纪年
|
|
87
|
+
convertYear(2024);
|
|
88
|
+
// [{ dynasty: '中華民國', reign_title: '民國', year_num: '一百一十三年' }]
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## API 文档
|
|
92
|
+
|
|
93
|
+
### `convertYear(year: number): EraResult[]`
|
|
94
|
+
|
|
95
|
+
将公元年份转换为中国历史年号纪年。
|
|
96
|
+
|
|
97
|
+
#### 参数
|
|
98
|
+
|
|
99
|
+
- `year` (number): 公元年份,必须是正整数
|
|
100
|
+
|
|
101
|
+
#### 返回值
|
|
102
|
+
|
|
103
|
+
返回一个数组,包含该年份对应的所有年号信息(某些年份可能有多个年号)。
|
|
104
|
+
|
|
105
|
+
每个结果对象包含以下字段:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
interface EraResult {
|
|
109
|
+
dynasty: string; // 朝代名称,如 "唐"、"宋"
|
|
110
|
+
reign_title: string; // 年号,如 "武德"、"贞观"
|
|
111
|
+
year_num: string; // 年份,如 "元年"、"三年"
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
#### 示例
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
// 普通年份
|
|
119
|
+
convertYear(627);
|
|
120
|
+
// [{ dynasty: '唐', reign_title: '贞观', year_num: '元年' }]
|
|
121
|
+
|
|
122
|
+
// 改元年份(一年内有多个年号)
|
|
123
|
+
convertYear(626);
|
|
124
|
+
// [
|
|
125
|
+
// { dynasty: '唐', reign_title: '武德', year_num: '九年' },
|
|
126
|
+
// { dynasty: '唐', reign_title: '贞观', year_num: '元年' }
|
|
127
|
+
// ]
|
|
128
|
+
|
|
129
|
+
// 南北朝等分裂时期(多个政权并存)
|
|
130
|
+
convertYear(420);
|
|
131
|
+
// [
|
|
132
|
+
// { dynasty: '南朝宋', reign_title: '永初', year_num: '元年' },
|
|
133
|
+
// { dynasty: '北魏', reign_title: '泰常', year_num: '五年' }
|
|
134
|
+
// ]
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## 使用场景
|
|
138
|
+
|
|
139
|
+
- 历史研究:快速查询历史事件对应的年号
|
|
140
|
+
- 游戏开发:古风游戏中的时间系统
|
|
141
|
+
- 文化应用:古籍阅读、历史教育应用
|
|
142
|
+
- 日期转换:公历与中国传统纪年互转
|
|
143
|
+
- 内容创作:自动为历史内容标注年号
|
|
144
|
+
|
|
145
|
+
## 支持的朝代范围
|
|
146
|
+
|
|
147
|
+
本库支持从 **秦朝(公元前 221 年)** 到 **清朝(公元 1912 年)** 的年号转换,涵盖:
|
|
148
|
+
|
|
149
|
+
- 秦朝、汉朝(西汉、东汉)
|
|
150
|
+
- 三国(魏、蜀、吴)
|
|
151
|
+
- 晋朝(西晋、东晋)、南北朝
|
|
152
|
+
- 隋朝、唐朝
|
|
153
|
+
- 五代十国
|
|
154
|
+
- 宋朝(北宋、南宋)、辽、金、西夏
|
|
155
|
+
- 元朝
|
|
156
|
+
- 明朝
|
|
157
|
+
- 清朝
|
|
158
|
+
|
|
159
|
+
## 注意事项
|
|
160
|
+
|
|
161
|
+
1. **改元情况**:某些年份会返回多个结果,这通常发生在:
|
|
162
|
+
- 皇帝改元(同一年内更换年号)
|
|
163
|
+
- 朝代更替
|
|
164
|
+
- 历史分裂时期的多个政权并存
|
|
165
|
+
|
|
166
|
+
2. **年份范围**:目前支持公元前 221 年到公元 1912 年,超出范围会返回空数组
|
|
167
|
+
|
|
168
|
+
3. **历史准确性**:年号数据基于主流历史记载,个别争议年份以通史为准
|
|
169
|
+
|
|
170
|
+
## 开发
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
# 克隆仓库
|
|
174
|
+
git clone https://github.com/frankslin/cn-era.git
|
|
175
|
+
|
|
176
|
+
# 安装依赖
|
|
177
|
+
npm install
|
|
178
|
+
|
|
179
|
+
# 运行测试
|
|
180
|
+
npm test
|
|
181
|
+
|
|
182
|
+
# 构建
|
|
183
|
+
npm run build
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## 贡献
|
|
187
|
+
|
|
188
|
+
欢迎提交 Issue 和 Pull Request!
|
|
189
|
+
|
|
190
|
+
如果发现历史数据有误,请提供可靠的历史文献来源。
|
|
191
|
+
|
|
192
|
+
## 许可证
|
|
193
|
+
|
|
194
|
+
[MIT](LICENSE)
|
|
195
|
+
|
|
196
|
+
## 致谢
|
|
197
|
+
|
|
198
|
+
感谢所有为中国历史研究做出贡献的学者和开源贡献者。
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
如果这个项目对你有帮助,欢迎 Star 支持!
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
convertYear: () => convertYear
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/data/eras.ts
|
|
28
|
+
var eraData = [
|
|
29
|
+
// 唐朝
|
|
30
|
+
{ dynasty: "\u5510", reign_title: "\u6B66\u5FB7", start_year: 618, end_year: 626 },
|
|
31
|
+
{ dynasty: "\u5510", reign_title: "\u8C9E\u89C0", start_year: 627, end_year: 649 },
|
|
32
|
+
{ dynasty: "\u5510", reign_title: "\u6C38\u5FBD", start_year: 650, end_year: 655 },
|
|
33
|
+
{ dynasty: "\u5510", reign_title: "\u986F\u6176", start_year: 656, end_year: 661 },
|
|
34
|
+
{ dynasty: "\u5510", reign_title: "\u9F8D\u6714", start_year: 661, end_year: 663 },
|
|
35
|
+
{ dynasty: "\u5510", reign_title: "\u9E9F\u5FB7", start_year: 664, end_year: 665 },
|
|
36
|
+
{ dynasty: "\u5510", reign_title: "\u4E7E\u5C01", start_year: 666, end_year: 668 },
|
|
37
|
+
{ dynasty: "\u5510", reign_title: "\u7E3D\u7AE0", start_year: 668, end_year: 670 },
|
|
38
|
+
{ dynasty: "\u5510", reign_title: "\u54B8\u4EA8", start_year: 670, end_year: 674 },
|
|
39
|
+
{ dynasty: "\u5510", reign_title: "\u4E0A\u5143", start_year: 674, end_year: 676 },
|
|
40
|
+
{ dynasty: "\u5510", reign_title: "\u5100\u9CF3", start_year: 676, end_year: 679 },
|
|
41
|
+
{ dynasty: "\u5510", reign_title: "\u8ABF\u9732", start_year: 679, end_year: 680 },
|
|
42
|
+
{ dynasty: "\u5510", reign_title: "\u6C38\u9686", start_year: 680, end_year: 681 },
|
|
43
|
+
{ dynasty: "\u5510", reign_title: "\u958B\u8000", start_year: 681, end_year: 682 },
|
|
44
|
+
{ dynasty: "\u5510", reign_title: "\u6C38\u6DF3", start_year: 682, end_year: 683 },
|
|
45
|
+
{ dynasty: "\u5510", reign_title: "\u5F18\u9053", start_year: 683, end_year: 684 },
|
|
46
|
+
{ dynasty: "\u5510", reign_title: "\u55E3\u8056", start_year: 684, end_year: 684 },
|
|
47
|
+
{ dynasty: "\u5510", reign_title: "\u6587\u660E", start_year: 684, end_year: 684 },
|
|
48
|
+
{ dynasty: "\u5510", reign_title: "\u5149\u5B85", start_year: 684, end_year: 684 },
|
|
49
|
+
{ dynasty: "\u5510", reign_title: "\u5782\u62F1", start_year: 685, end_year: 688 },
|
|
50
|
+
{ dynasty: "\u5510", reign_title: "\u6C38\u660C", start_year: 689, end_year: 689 },
|
|
51
|
+
{ dynasty: "\u5510", reign_title: "\u8F09\u521D", start_year: 689, end_year: 690 },
|
|
52
|
+
// 武周
|
|
53
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u5929\u6388", start_year: 690, end_year: 692 },
|
|
54
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u5982\u610F", start_year: 692, end_year: 692 },
|
|
55
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u9577\u58FD", start_year: 692, end_year: 694 },
|
|
56
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u5EF6\u8F09", start_year: 694, end_year: 694 },
|
|
57
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u8B49\u8056", start_year: 695, end_year: 695 },
|
|
58
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u5929\u518A\u842C\u6B72", start_year: 695, end_year: 696 },
|
|
59
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u842C\u6B72\u767B\u5C01", start_year: 696, end_year: 696 },
|
|
60
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u842C\u6B72\u901A\u5929", start_year: 696, end_year: 697 },
|
|
61
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u795E\u529F", start_year: 697, end_year: 697 },
|
|
62
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u8056\u66C6", start_year: 698, end_year: 700 },
|
|
63
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u4E45\u8996", start_year: 700, end_year: 701 },
|
|
64
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u5927\u8DB3", start_year: 701, end_year: 701 },
|
|
65
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u9577\u5B89", start_year: 701, end_year: 705 },
|
|
66
|
+
// 唐中宗復辟後
|
|
67
|
+
{ dynasty: "\u5510", reign_title: "\u795E\u9F8D", start_year: 705, end_year: 707 },
|
|
68
|
+
{ dynasty: "\u5510", reign_title: "\u666F\u9F8D", start_year: 707, end_year: 710 },
|
|
69
|
+
{ dynasty: "\u5510", reign_title: "\u666F\u96F2", start_year: 710, end_year: 712 },
|
|
70
|
+
{ dynasty: "\u5510", reign_title: "\u592A\u6975", start_year: 712, end_year: 712 },
|
|
71
|
+
{ dynasty: "\u5510", reign_title: "\u5EF6\u548C", start_year: 712, end_year: 712 },
|
|
72
|
+
{ dynasty: "\u5510", reign_title: "\u5148\u5929", start_year: 712, end_year: 713 },
|
|
73
|
+
{ dynasty: "\u5510", reign_title: "\u958B\u5143", start_year: 713, end_year: 741 },
|
|
74
|
+
{ dynasty: "\u5510", reign_title: "\u5929\u5BF6", start_year: 742, end_year: 756 },
|
|
75
|
+
// 安史之亂時期
|
|
76
|
+
{ dynasty: "\u5510", reign_title: "\u81F3\u5FB7", start_year: 756, end_year: 758 },
|
|
77
|
+
{ dynasty: "\u5510", reign_title: "\u4E7E\u5143", start_year: 758, end_year: 760 },
|
|
78
|
+
{ dynasty: "\u5510", reign_title: "\u4E0A\u5143", start_year: 760, end_year: 762 },
|
|
79
|
+
{ dynasty: "\u5510", reign_title: "\u5BF6\u61C9", start_year: 762, end_year: 763 },
|
|
80
|
+
{ dynasty: "\u5510", reign_title: "\u5EE3\u5FB7", start_year: 763, end_year: 764 },
|
|
81
|
+
{ dynasty: "\u5510", reign_title: "\u6C38\u6CF0", start_year: 765, end_year: 766 },
|
|
82
|
+
{ dynasty: "\u5510", reign_title: "\u5927\u66C6", start_year: 766, end_year: 779 },
|
|
83
|
+
{ dynasty: "\u5510", reign_title: "\u5EFA\u4E2D", start_year: 780, end_year: 783 },
|
|
84
|
+
{ dynasty: "\u5510", reign_title: "\u8208\u5143", start_year: 784, end_year: 784 },
|
|
85
|
+
{ dynasty: "\u5510", reign_title: "\u8C9E\u5143", start_year: 785, end_year: 805 },
|
|
86
|
+
{ dynasty: "\u5510", reign_title: "\u6C38\u8C9E", start_year: 805, end_year: 805 },
|
|
87
|
+
{ dynasty: "\u5510", reign_title: "\u5143\u548C", start_year: 806, end_year: 820 },
|
|
88
|
+
{ dynasty: "\u5510", reign_title: "\u9577\u6176", start_year: 821, end_year: 824 },
|
|
89
|
+
{ dynasty: "\u5510", reign_title: "\u5BF6\u66C6", start_year: 825, end_year: 827 },
|
|
90
|
+
{ dynasty: "\u5510", reign_title: "\u5927\u548C", start_year: 827, end_year: 835 },
|
|
91
|
+
{ dynasty: "\u5510", reign_title: "\u958B\u6210", start_year: 836, end_year: 840 },
|
|
92
|
+
{ dynasty: "\u5510", reign_title: "\u6703\u660C", start_year: 841, end_year: 846 },
|
|
93
|
+
{ dynasty: "\u5510", reign_title: "\u5927\u4E2D", start_year: 847, end_year: 860 },
|
|
94
|
+
{ dynasty: "\u5510", reign_title: "\u54B8\u901A", start_year: 860, end_year: 874 },
|
|
95
|
+
{ dynasty: "\u5510", reign_title: "\u4E7E\u7B26", start_year: 874, end_year: 879 },
|
|
96
|
+
{ dynasty: "\u5510", reign_title: "\u5EE3\u660E", start_year: 880, end_year: 881 },
|
|
97
|
+
{ dynasty: "\u5510", reign_title: "\u4E2D\u548C", start_year: 881, end_year: 885 },
|
|
98
|
+
{ dynasty: "\u5510", reign_title: "\u5149\u555F", start_year: 885, end_year: 888 },
|
|
99
|
+
{ dynasty: "\u5510", reign_title: "\u6587\u5FB7", start_year: 888, end_year: 888 },
|
|
100
|
+
{ dynasty: "\u5510", reign_title: "\u9F8D\u7D00", start_year: 889, end_year: 889 },
|
|
101
|
+
{ dynasty: "\u5510", reign_title: "\u5927\u9806", start_year: 890, end_year: 891 },
|
|
102
|
+
{ dynasty: "\u5510", reign_title: "\u666F\u798F", start_year: 892, end_year: 893 },
|
|
103
|
+
{ dynasty: "\u5510", reign_title: "\u4E7E\u5BE7", start_year: 894, end_year: 898 },
|
|
104
|
+
{ dynasty: "\u5510", reign_title: "\u5149\u5316", start_year: 898, end_year: 901 },
|
|
105
|
+
{ dynasty: "\u5510", reign_title: "\u5929\u5FA9", start_year: 901, end_year: 904 },
|
|
106
|
+
{ dynasty: "\u5510", reign_title: "\u5929\u7950", start_year: 904, end_year: 907 }
|
|
107
|
+
// TODO: 需要补充更多朝代的年号数据
|
|
108
|
+
// 参考 DATA_TODO.md 文件获取详细的数据补充清单
|
|
109
|
+
//
|
|
110
|
+
// 优先级:
|
|
111
|
+
// 1. 高优先级(常用朝代):
|
|
112
|
+
// - 明朝 (1368~1644):洪武、建文、永乐、洪熙、宣德、正统、景泰、天顺等
|
|
113
|
+
// - 清朝 (1644~1911):順治、康熙、雍正、乾隆、嘉慶、道光、咸豐、同治、光緒、宣統
|
|
114
|
+
// - 唐朝(续 710~907):景雲、開元、天寶、至德、乾元等
|
|
115
|
+
// - 宋朝 (960~1279):建隆、乾德、開寶、太平興國等
|
|
116
|
+
//
|
|
117
|
+
// 2. 中优先级:
|
|
118
|
+
// - 隋朝 (581~618):開皇、仁壽、大業
|
|
119
|
+
// - 元朝 (1271~1368):中統、至元、大德、至大等
|
|
120
|
+
//
|
|
121
|
+
// 3. 低优先级:
|
|
122
|
+
// - 秦朝及之前:秦始皇時期無年號
|
|
123
|
+
// - 西漢 (-206~9):建元開始有年號
|
|
124
|
+
// - 東漢 (25~220)
|
|
125
|
+
// - 三國 (220~280):魏、蜀、吳
|
|
126
|
+
// - 西晉、東晉 (265~419)
|
|
127
|
+
// - 南北朝 (420~589):多政權並存
|
|
128
|
+
// - 五代十國 (907~960):多政權並存
|
|
129
|
+
// - 遼 (947~1125)、金 (1115~1234)、西夏 (1032~1227)
|
|
130
|
+
//
|
|
131
|
+
// 數據來源建議:
|
|
132
|
+
// - 維基百科「中國年號列表」
|
|
133
|
+
// - CBDB 年號數據
|
|
134
|
+
// - 《中國歷代年號考》
|
|
135
|
+
];
|
|
136
|
+
|
|
137
|
+
// src/utils/number.ts
|
|
138
|
+
var chineseNumbers = {
|
|
139
|
+
0: "\u96F6",
|
|
140
|
+
1: "\u4E00",
|
|
141
|
+
2: "\u4E8C",
|
|
142
|
+
3: "\u4E09",
|
|
143
|
+
4: "\u56DB",
|
|
144
|
+
5: "\u4E94",
|
|
145
|
+
6: "\u516D",
|
|
146
|
+
7: "\u4E03",
|
|
147
|
+
8: "\u516B",
|
|
148
|
+
9: "\u4E5D"
|
|
149
|
+
};
|
|
150
|
+
function numberToChinese(num) {
|
|
151
|
+
if (num === 1) {
|
|
152
|
+
return "\u5143\u5E74";
|
|
153
|
+
}
|
|
154
|
+
if (num < 1 || num > 9999) {
|
|
155
|
+
throw new Error("\u6570\u5B57\u5FC5\u987B\u5728 1-9999 \u4E4B\u95F4");
|
|
156
|
+
}
|
|
157
|
+
let result = "";
|
|
158
|
+
const thousands = Math.floor(num / 1e3);
|
|
159
|
+
if (thousands > 0) {
|
|
160
|
+
result += `${chineseNumbers[thousands]}\u5343`;
|
|
161
|
+
}
|
|
162
|
+
const hundreds = Math.floor(num % 1e3 / 100);
|
|
163
|
+
if (hundreds > 0) {
|
|
164
|
+
result += `${chineseNumbers[hundreds]}\u767E`;
|
|
165
|
+
} else if (thousands > 0 && num % 100 > 0) {
|
|
166
|
+
result += "\u96F6";
|
|
167
|
+
}
|
|
168
|
+
const tens = Math.floor(num % 100 / 10);
|
|
169
|
+
if (tens > 0) {
|
|
170
|
+
if (tens === 1 && num < 20 && num >= 10) {
|
|
171
|
+
result += "\u5341";
|
|
172
|
+
} else {
|
|
173
|
+
result += `${chineseNumbers[tens]}\u5341`;
|
|
174
|
+
}
|
|
175
|
+
} else if (hundreds > 0 && num % 10 > 0) {
|
|
176
|
+
if (!result.endsWith("\u96F6")) {
|
|
177
|
+
result += "\u96F6";
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
const ones = num % 10;
|
|
181
|
+
if (ones > 0) {
|
|
182
|
+
result += chineseNumbers[ones];
|
|
183
|
+
}
|
|
184
|
+
return `${result}\u5E74`;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// src/index.ts
|
|
188
|
+
function convertYear(year) {
|
|
189
|
+
if (year === 0) {
|
|
190
|
+
throw new Error("\u5E74\u4EFD\u4E0D\u80FD\u4E3A 0");
|
|
191
|
+
}
|
|
192
|
+
if (!Number.isInteger(year)) {
|
|
193
|
+
throw new Error("\u5E74\u4EFD\u5FC5\u987B\u662F\u6574\u6570");
|
|
194
|
+
}
|
|
195
|
+
if (year < -841 || year > 3e3) {
|
|
196
|
+
throw new Error("\u5E74\u4EFD\u8303\u56F4\u5FC5\u987B\u5728 -841 \u5230 3000 \u4E4B\u95F4");
|
|
197
|
+
}
|
|
198
|
+
const results = [];
|
|
199
|
+
if (year >= 1912) {
|
|
200
|
+
const mingguoYear = year - 1911;
|
|
201
|
+
results.push({
|
|
202
|
+
dynasty: "\u4E2D\u83EF\u6C11\u570B",
|
|
203
|
+
reign_title: "\u6C11\u570B",
|
|
204
|
+
year_num: numberToChinese(mingguoYear)
|
|
205
|
+
});
|
|
206
|
+
return results;
|
|
207
|
+
}
|
|
208
|
+
for (const era of eraData) {
|
|
209
|
+
if (year >= era.start_year && year <= era.end_year) {
|
|
210
|
+
const yearInEra = year - era.start_year + 1;
|
|
211
|
+
results.push({
|
|
212
|
+
dynasty: era.dynasty,
|
|
213
|
+
reign_title: era.reign_title,
|
|
214
|
+
year_num: numberToChinese(yearInEra)
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return results;
|
|
219
|
+
}
|
|
220
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
221
|
+
0 && (module.exports = {
|
|
222
|
+
convertYear
|
|
223
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 年号转换结果
|
|
3
|
+
*/
|
|
4
|
+
interface EraResult {
|
|
5
|
+
/** 朝代名称 */
|
|
6
|
+
dynasty: string;
|
|
7
|
+
/** 年号 */
|
|
8
|
+
reign_title: string;
|
|
9
|
+
/** 年份(如 "元年"、"三年") */
|
|
10
|
+
year_num: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 年号数据
|
|
14
|
+
*/
|
|
15
|
+
interface EraData {
|
|
16
|
+
/** 朝代名称 */
|
|
17
|
+
dynasty: string;
|
|
18
|
+
/** 年号 */
|
|
19
|
+
reign_title: string;
|
|
20
|
+
/** 开始年份(公元) */
|
|
21
|
+
start_year: number;
|
|
22
|
+
/** 结束年份(公元) */
|
|
23
|
+
end_year: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 将公元年份转换为中国历史年号纪年
|
|
28
|
+
* @param year 公元年份(范围:-841 到 3000,0 不合法)
|
|
29
|
+
* @returns 该年份对应的所有年号信息数组
|
|
30
|
+
* @throws {Error} 当年份不合法时抛出错误
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* convertYear(618); // [{ dynasty: '唐', reign_title: '武德', year_num: '元年' }]
|
|
35
|
+
* convertYear(2024); // [{ dynasty: '民国', reign_title: '民国', year_num: '一百一十三年' }]
|
|
36
|
+
* convertYear(0); // 抛出错误
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function convertYear(year: number): EraResult[];
|
|
40
|
+
|
|
41
|
+
export { type EraData, type EraResult, convertYear };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 年号转换结果
|
|
3
|
+
*/
|
|
4
|
+
interface EraResult {
|
|
5
|
+
/** 朝代名称 */
|
|
6
|
+
dynasty: string;
|
|
7
|
+
/** 年号 */
|
|
8
|
+
reign_title: string;
|
|
9
|
+
/** 年份(如 "元年"、"三年") */
|
|
10
|
+
year_num: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 年号数据
|
|
14
|
+
*/
|
|
15
|
+
interface EraData {
|
|
16
|
+
/** 朝代名称 */
|
|
17
|
+
dynasty: string;
|
|
18
|
+
/** 年号 */
|
|
19
|
+
reign_title: string;
|
|
20
|
+
/** 开始年份(公元) */
|
|
21
|
+
start_year: number;
|
|
22
|
+
/** 结束年份(公元) */
|
|
23
|
+
end_year: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 将公元年份转换为中国历史年号纪年
|
|
28
|
+
* @param year 公元年份(范围:-841 到 3000,0 不合法)
|
|
29
|
+
* @returns 该年份对应的所有年号信息数组
|
|
30
|
+
* @throws {Error} 当年份不合法时抛出错误
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* convertYear(618); // [{ dynasty: '唐', reign_title: '武德', year_num: '元年' }]
|
|
35
|
+
* convertYear(2024); // [{ dynasty: '民国', reign_title: '民国', year_num: '一百一十三年' }]
|
|
36
|
+
* convertYear(0); // 抛出错误
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function convertYear(year: number): EraResult[];
|
|
40
|
+
|
|
41
|
+
export { type EraData, type EraResult, convertYear };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// src/data/eras.ts
|
|
2
|
+
var eraData = [
|
|
3
|
+
// 唐朝
|
|
4
|
+
{ dynasty: "\u5510", reign_title: "\u6B66\u5FB7", start_year: 618, end_year: 626 },
|
|
5
|
+
{ dynasty: "\u5510", reign_title: "\u8C9E\u89C0", start_year: 627, end_year: 649 },
|
|
6
|
+
{ dynasty: "\u5510", reign_title: "\u6C38\u5FBD", start_year: 650, end_year: 655 },
|
|
7
|
+
{ dynasty: "\u5510", reign_title: "\u986F\u6176", start_year: 656, end_year: 661 },
|
|
8
|
+
{ dynasty: "\u5510", reign_title: "\u9F8D\u6714", start_year: 661, end_year: 663 },
|
|
9
|
+
{ dynasty: "\u5510", reign_title: "\u9E9F\u5FB7", start_year: 664, end_year: 665 },
|
|
10
|
+
{ dynasty: "\u5510", reign_title: "\u4E7E\u5C01", start_year: 666, end_year: 668 },
|
|
11
|
+
{ dynasty: "\u5510", reign_title: "\u7E3D\u7AE0", start_year: 668, end_year: 670 },
|
|
12
|
+
{ dynasty: "\u5510", reign_title: "\u54B8\u4EA8", start_year: 670, end_year: 674 },
|
|
13
|
+
{ dynasty: "\u5510", reign_title: "\u4E0A\u5143", start_year: 674, end_year: 676 },
|
|
14
|
+
{ dynasty: "\u5510", reign_title: "\u5100\u9CF3", start_year: 676, end_year: 679 },
|
|
15
|
+
{ dynasty: "\u5510", reign_title: "\u8ABF\u9732", start_year: 679, end_year: 680 },
|
|
16
|
+
{ dynasty: "\u5510", reign_title: "\u6C38\u9686", start_year: 680, end_year: 681 },
|
|
17
|
+
{ dynasty: "\u5510", reign_title: "\u958B\u8000", start_year: 681, end_year: 682 },
|
|
18
|
+
{ dynasty: "\u5510", reign_title: "\u6C38\u6DF3", start_year: 682, end_year: 683 },
|
|
19
|
+
{ dynasty: "\u5510", reign_title: "\u5F18\u9053", start_year: 683, end_year: 684 },
|
|
20
|
+
{ dynasty: "\u5510", reign_title: "\u55E3\u8056", start_year: 684, end_year: 684 },
|
|
21
|
+
{ dynasty: "\u5510", reign_title: "\u6587\u660E", start_year: 684, end_year: 684 },
|
|
22
|
+
{ dynasty: "\u5510", reign_title: "\u5149\u5B85", start_year: 684, end_year: 684 },
|
|
23
|
+
{ dynasty: "\u5510", reign_title: "\u5782\u62F1", start_year: 685, end_year: 688 },
|
|
24
|
+
{ dynasty: "\u5510", reign_title: "\u6C38\u660C", start_year: 689, end_year: 689 },
|
|
25
|
+
{ dynasty: "\u5510", reign_title: "\u8F09\u521D", start_year: 689, end_year: 690 },
|
|
26
|
+
// 武周
|
|
27
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u5929\u6388", start_year: 690, end_year: 692 },
|
|
28
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u5982\u610F", start_year: 692, end_year: 692 },
|
|
29
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u9577\u58FD", start_year: 692, end_year: 694 },
|
|
30
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u5EF6\u8F09", start_year: 694, end_year: 694 },
|
|
31
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u8B49\u8056", start_year: 695, end_year: 695 },
|
|
32
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u5929\u518A\u842C\u6B72", start_year: 695, end_year: 696 },
|
|
33
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u842C\u6B72\u767B\u5C01", start_year: 696, end_year: 696 },
|
|
34
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u842C\u6B72\u901A\u5929", start_year: 696, end_year: 697 },
|
|
35
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u795E\u529F", start_year: 697, end_year: 697 },
|
|
36
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u8056\u66C6", start_year: 698, end_year: 700 },
|
|
37
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u4E45\u8996", start_year: 700, end_year: 701 },
|
|
38
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u5927\u8DB3", start_year: 701, end_year: 701 },
|
|
39
|
+
{ dynasty: "\u6B66\u5468", reign_title: "\u9577\u5B89", start_year: 701, end_year: 705 },
|
|
40
|
+
// 唐中宗復辟後
|
|
41
|
+
{ dynasty: "\u5510", reign_title: "\u795E\u9F8D", start_year: 705, end_year: 707 },
|
|
42
|
+
{ dynasty: "\u5510", reign_title: "\u666F\u9F8D", start_year: 707, end_year: 710 },
|
|
43
|
+
{ dynasty: "\u5510", reign_title: "\u666F\u96F2", start_year: 710, end_year: 712 },
|
|
44
|
+
{ dynasty: "\u5510", reign_title: "\u592A\u6975", start_year: 712, end_year: 712 },
|
|
45
|
+
{ dynasty: "\u5510", reign_title: "\u5EF6\u548C", start_year: 712, end_year: 712 },
|
|
46
|
+
{ dynasty: "\u5510", reign_title: "\u5148\u5929", start_year: 712, end_year: 713 },
|
|
47
|
+
{ dynasty: "\u5510", reign_title: "\u958B\u5143", start_year: 713, end_year: 741 },
|
|
48
|
+
{ dynasty: "\u5510", reign_title: "\u5929\u5BF6", start_year: 742, end_year: 756 },
|
|
49
|
+
// 安史之亂時期
|
|
50
|
+
{ dynasty: "\u5510", reign_title: "\u81F3\u5FB7", start_year: 756, end_year: 758 },
|
|
51
|
+
{ dynasty: "\u5510", reign_title: "\u4E7E\u5143", start_year: 758, end_year: 760 },
|
|
52
|
+
{ dynasty: "\u5510", reign_title: "\u4E0A\u5143", start_year: 760, end_year: 762 },
|
|
53
|
+
{ dynasty: "\u5510", reign_title: "\u5BF6\u61C9", start_year: 762, end_year: 763 },
|
|
54
|
+
{ dynasty: "\u5510", reign_title: "\u5EE3\u5FB7", start_year: 763, end_year: 764 },
|
|
55
|
+
{ dynasty: "\u5510", reign_title: "\u6C38\u6CF0", start_year: 765, end_year: 766 },
|
|
56
|
+
{ dynasty: "\u5510", reign_title: "\u5927\u66C6", start_year: 766, end_year: 779 },
|
|
57
|
+
{ dynasty: "\u5510", reign_title: "\u5EFA\u4E2D", start_year: 780, end_year: 783 },
|
|
58
|
+
{ dynasty: "\u5510", reign_title: "\u8208\u5143", start_year: 784, end_year: 784 },
|
|
59
|
+
{ dynasty: "\u5510", reign_title: "\u8C9E\u5143", start_year: 785, end_year: 805 },
|
|
60
|
+
{ dynasty: "\u5510", reign_title: "\u6C38\u8C9E", start_year: 805, end_year: 805 },
|
|
61
|
+
{ dynasty: "\u5510", reign_title: "\u5143\u548C", start_year: 806, end_year: 820 },
|
|
62
|
+
{ dynasty: "\u5510", reign_title: "\u9577\u6176", start_year: 821, end_year: 824 },
|
|
63
|
+
{ dynasty: "\u5510", reign_title: "\u5BF6\u66C6", start_year: 825, end_year: 827 },
|
|
64
|
+
{ dynasty: "\u5510", reign_title: "\u5927\u548C", start_year: 827, end_year: 835 },
|
|
65
|
+
{ dynasty: "\u5510", reign_title: "\u958B\u6210", start_year: 836, end_year: 840 },
|
|
66
|
+
{ dynasty: "\u5510", reign_title: "\u6703\u660C", start_year: 841, end_year: 846 },
|
|
67
|
+
{ dynasty: "\u5510", reign_title: "\u5927\u4E2D", start_year: 847, end_year: 860 },
|
|
68
|
+
{ dynasty: "\u5510", reign_title: "\u54B8\u901A", start_year: 860, end_year: 874 },
|
|
69
|
+
{ dynasty: "\u5510", reign_title: "\u4E7E\u7B26", start_year: 874, end_year: 879 },
|
|
70
|
+
{ dynasty: "\u5510", reign_title: "\u5EE3\u660E", start_year: 880, end_year: 881 },
|
|
71
|
+
{ dynasty: "\u5510", reign_title: "\u4E2D\u548C", start_year: 881, end_year: 885 },
|
|
72
|
+
{ dynasty: "\u5510", reign_title: "\u5149\u555F", start_year: 885, end_year: 888 },
|
|
73
|
+
{ dynasty: "\u5510", reign_title: "\u6587\u5FB7", start_year: 888, end_year: 888 },
|
|
74
|
+
{ dynasty: "\u5510", reign_title: "\u9F8D\u7D00", start_year: 889, end_year: 889 },
|
|
75
|
+
{ dynasty: "\u5510", reign_title: "\u5927\u9806", start_year: 890, end_year: 891 },
|
|
76
|
+
{ dynasty: "\u5510", reign_title: "\u666F\u798F", start_year: 892, end_year: 893 },
|
|
77
|
+
{ dynasty: "\u5510", reign_title: "\u4E7E\u5BE7", start_year: 894, end_year: 898 },
|
|
78
|
+
{ dynasty: "\u5510", reign_title: "\u5149\u5316", start_year: 898, end_year: 901 },
|
|
79
|
+
{ dynasty: "\u5510", reign_title: "\u5929\u5FA9", start_year: 901, end_year: 904 },
|
|
80
|
+
{ dynasty: "\u5510", reign_title: "\u5929\u7950", start_year: 904, end_year: 907 }
|
|
81
|
+
// TODO: 需要补充更多朝代的年号数据
|
|
82
|
+
// 参考 DATA_TODO.md 文件获取详细的数据补充清单
|
|
83
|
+
//
|
|
84
|
+
// 优先级:
|
|
85
|
+
// 1. 高优先级(常用朝代):
|
|
86
|
+
// - 明朝 (1368~1644):洪武、建文、永乐、洪熙、宣德、正统、景泰、天顺等
|
|
87
|
+
// - 清朝 (1644~1911):順治、康熙、雍正、乾隆、嘉慶、道光、咸豐、同治、光緒、宣統
|
|
88
|
+
// - 唐朝(续 710~907):景雲、開元、天寶、至德、乾元等
|
|
89
|
+
// - 宋朝 (960~1279):建隆、乾德、開寶、太平興國等
|
|
90
|
+
//
|
|
91
|
+
// 2. 中优先级:
|
|
92
|
+
// - 隋朝 (581~618):開皇、仁壽、大業
|
|
93
|
+
// - 元朝 (1271~1368):中統、至元、大德、至大等
|
|
94
|
+
//
|
|
95
|
+
// 3. 低优先级:
|
|
96
|
+
// - 秦朝及之前:秦始皇時期無年號
|
|
97
|
+
// - 西漢 (-206~9):建元開始有年號
|
|
98
|
+
// - 東漢 (25~220)
|
|
99
|
+
// - 三國 (220~280):魏、蜀、吳
|
|
100
|
+
// - 西晉、東晉 (265~419)
|
|
101
|
+
// - 南北朝 (420~589):多政權並存
|
|
102
|
+
// - 五代十國 (907~960):多政權並存
|
|
103
|
+
// - 遼 (947~1125)、金 (1115~1234)、西夏 (1032~1227)
|
|
104
|
+
//
|
|
105
|
+
// 數據來源建議:
|
|
106
|
+
// - 維基百科「中國年號列表」
|
|
107
|
+
// - CBDB 年號數據
|
|
108
|
+
// - 《中國歷代年號考》
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
// src/utils/number.ts
|
|
112
|
+
var chineseNumbers = {
|
|
113
|
+
0: "\u96F6",
|
|
114
|
+
1: "\u4E00",
|
|
115
|
+
2: "\u4E8C",
|
|
116
|
+
3: "\u4E09",
|
|
117
|
+
4: "\u56DB",
|
|
118
|
+
5: "\u4E94",
|
|
119
|
+
6: "\u516D",
|
|
120
|
+
7: "\u4E03",
|
|
121
|
+
8: "\u516B",
|
|
122
|
+
9: "\u4E5D"
|
|
123
|
+
};
|
|
124
|
+
function numberToChinese(num) {
|
|
125
|
+
if (num === 1) {
|
|
126
|
+
return "\u5143\u5E74";
|
|
127
|
+
}
|
|
128
|
+
if (num < 1 || num > 9999) {
|
|
129
|
+
throw new Error("\u6570\u5B57\u5FC5\u987B\u5728 1-9999 \u4E4B\u95F4");
|
|
130
|
+
}
|
|
131
|
+
let result = "";
|
|
132
|
+
const thousands = Math.floor(num / 1e3);
|
|
133
|
+
if (thousands > 0) {
|
|
134
|
+
result += `${chineseNumbers[thousands]}\u5343`;
|
|
135
|
+
}
|
|
136
|
+
const hundreds = Math.floor(num % 1e3 / 100);
|
|
137
|
+
if (hundreds > 0) {
|
|
138
|
+
result += `${chineseNumbers[hundreds]}\u767E`;
|
|
139
|
+
} else if (thousands > 0 && num % 100 > 0) {
|
|
140
|
+
result += "\u96F6";
|
|
141
|
+
}
|
|
142
|
+
const tens = Math.floor(num % 100 / 10);
|
|
143
|
+
if (tens > 0) {
|
|
144
|
+
if (tens === 1 && num < 20 && num >= 10) {
|
|
145
|
+
result += "\u5341";
|
|
146
|
+
} else {
|
|
147
|
+
result += `${chineseNumbers[tens]}\u5341`;
|
|
148
|
+
}
|
|
149
|
+
} else if (hundreds > 0 && num % 10 > 0) {
|
|
150
|
+
if (!result.endsWith("\u96F6")) {
|
|
151
|
+
result += "\u96F6";
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const ones = num % 10;
|
|
155
|
+
if (ones > 0) {
|
|
156
|
+
result += chineseNumbers[ones];
|
|
157
|
+
}
|
|
158
|
+
return `${result}\u5E74`;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// src/index.ts
|
|
162
|
+
function convertYear(year) {
|
|
163
|
+
if (year === 0) {
|
|
164
|
+
throw new Error("\u5E74\u4EFD\u4E0D\u80FD\u4E3A 0");
|
|
165
|
+
}
|
|
166
|
+
if (!Number.isInteger(year)) {
|
|
167
|
+
throw new Error("\u5E74\u4EFD\u5FC5\u987B\u662F\u6574\u6570");
|
|
168
|
+
}
|
|
169
|
+
if (year < -841 || year > 3e3) {
|
|
170
|
+
throw new Error("\u5E74\u4EFD\u8303\u56F4\u5FC5\u987B\u5728 -841 \u5230 3000 \u4E4B\u95F4");
|
|
171
|
+
}
|
|
172
|
+
const results = [];
|
|
173
|
+
if (year >= 1912) {
|
|
174
|
+
const mingguoYear = year - 1911;
|
|
175
|
+
results.push({
|
|
176
|
+
dynasty: "\u4E2D\u83EF\u6C11\u570B",
|
|
177
|
+
reign_title: "\u6C11\u570B",
|
|
178
|
+
year_num: numberToChinese(mingguoYear)
|
|
179
|
+
});
|
|
180
|
+
return results;
|
|
181
|
+
}
|
|
182
|
+
for (const era of eraData) {
|
|
183
|
+
if (year >= era.start_year && year <= era.end_year) {
|
|
184
|
+
const yearInEra = year - era.start_year + 1;
|
|
185
|
+
results.push({
|
|
186
|
+
dynasty: era.dynasty,
|
|
187
|
+
reign_title: era.reign_title,
|
|
188
|
+
year_num: numberToChinese(yearInEra)
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return results;
|
|
193
|
+
}
|
|
194
|
+
export {
|
|
195
|
+
convertYear
|
|
196
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cn-era",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Convert Gregorian years into Chinese historical era names. 将公元年份转换为中国历史年号纪年",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.cjs",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
21
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
22
|
+
"test": "vitest",
|
|
23
|
+
"test:coverage": "vitest --coverage",
|
|
24
|
+
"lint": "eslint src --ext .ts",
|
|
25
|
+
"prepublishOnly": "npm run build"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"chinese",
|
|
29
|
+
"era",
|
|
30
|
+
"reign",
|
|
31
|
+
"dynasty",
|
|
32
|
+
"year",
|
|
33
|
+
"converter",
|
|
34
|
+
"历史",
|
|
35
|
+
"年号",
|
|
36
|
+
"朝代"
|
|
37
|
+
],
|
|
38
|
+
"author": "Frank Lin",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/node": "^20.0.0",
|
|
42
|
+
"@vitest/coverage-v8": "^1.0.0",
|
|
43
|
+
"tsup": "^8.0.0",
|
|
44
|
+
"typescript": "^5.0.0",
|
|
45
|
+
"vitest": "^1.0.0"
|
|
46
|
+
},
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git+https://github.com/frankslin/cn-era.git"
|
|
50
|
+
},
|
|
51
|
+
"bugs": {
|
|
52
|
+
"url": "https://github.com/frankslin/cn-era/issues"
|
|
53
|
+
},
|
|
54
|
+
"homepage": "https://github.com/frankslin/cn-era#readme"
|
|
55
|
+
}
|