cn-era 0.1.1 → 0.3.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/README.md CHANGED
@@ -11,28 +11,62 @@ A lightweight JavaScript/TypeScript library for converting Gregorian calendar ye
11
11
 
12
12
  ## Current Status
13
13
 
14
- **Early Development (v0.1.1)**
14
+ **Beta Release (v0.3.0)**
15
15
 
16
- This library is in early development. Currently includes:
17
- - **Tang Dynasty** - Complete (618-907 CE)
18
- - **Wu Zhou** - Complete (684-705 CE)
19
- - **Song Dynasty** - Complete (960-1279 CE)
20
- - **Yuan Dynasty** - Complete (1260-1368 CE)
21
- - **Ming Dynasty** - Complete (1368-1644 CE)
22
- - **Qing Dynasty** - Complete (1644-1912 CE)
23
- - **Republic of China** - Complete (1912-present)
16
+ This library now includes **complete era name coverage from 140 BCE to present**, covering all major Chinese dynasties and regimes:
24
17
 
25
- We are actively working on adding complete historical era data for other dynasties. Contributions are welcome! See [TODO.md](TODO.md) for the data completion roadmap.
18
+ **Ancient Period:**
19
+ - **Western Han** (140 BCE - 8 CE) - Complete
20
+ - **Xin Dynasty** (9-23 CE) - Complete
21
+ - **Eastern Han** (25-220 CE) - Complete
22
+
23
+ **Three Kingdoms & Jin:**
24
+ - **Cao Wei** (220-265 CE) - Complete
25
+ - **Shu Han** (221-263 CE) - Complete
26
+ - **Eastern Wu** (222-280 CE) - Complete
27
+ - **Western Jin** (265-316 CE) - Complete
28
+ - **Eastern Jin** (317-420 CE) - Complete
29
+
30
+ **Northern and Southern Dynasties:**
31
+ - **Southern Dynasties**: Liu Song, Southern Qi, Liang, Chen (420-589 CE) - Complete
32
+ - **Northern Dynasties**: Northern Wei, Eastern Wei, Western Wei, Northern Qi, Northern Zhou (386-581 CE) - Complete
33
+
34
+ **Unified Dynasties:**
35
+ - **Sui Dynasty** (581-618 CE) - Complete
36
+ - **Tang Dynasty** (618-907 CE) - Complete
37
+ - **Wu Zhou** (690-705 CE) - Complete
38
+
39
+ **Five Dynasties and Ten Kingdoms:**
40
+ - **Five Dynasties**: Later Liang, Later Tang, Later Jin, Later Han, Later Zhou (907-960 CE) - Complete
41
+
42
+ **Medieval Period:**
43
+ - **Song Dynasty** (960-1279 CE) - Complete
44
+ - **Liao Dynasty** (916-1125 CE) - Complete (concurrent with Song)
45
+ - **Western Xia** (1038-1227 CE) - Complete (concurrent with Song)
46
+ - **Jin Dynasty** (1115-1234 CE) - Complete (concurrent with Song)
47
+ - **Yuan Dynasty** (1271-1368 CE) - Complete
48
+
49
+ **Late Imperial:**
50
+ - **Ming Dynasty** (1368-1644 CE) - Complete
51
+ - **Qing Dynasty** (1644-1912 CE) - Complete
52
+
53
+ **Modern:**
54
+ - **Republic of China** (1912-present) - Complete
55
+
56
+ **Total Coverage**: 498 historical era names spanning 2,164 years (140 BCE - 2024 CE)
26
57
 
27
58
  ## Features
28
59
 
29
- - Accurate era conversion: Supports historical era names from Qin Dynasty to Qing Dynasty
30
- - Comprehensive historical data: Covers all major Chinese dynasties
31
- - Multiple era support: Handles cases where multiple era names coexist in the same year (e.g., era changes)
32
- - Zero dependencies: No additional packages required
33
- - TypeScript support: Complete type definitions included
34
- - Lightweight: Small bundle size, minimal impact on your project
35
- - Traditional Chinese output: All era names in Traditional Chinese characters
60
+ - **Smart Mainline Mode**: Automatically returns the orthodox/mainline dynasty for practical use (new in v0.3.0)
61
+ - **Dynasty Enum System**: Uses standardized CBDB (China Biographical Database) dynasty codes for reliable identification (new in v0.3.0)
62
+ - **Flexible Querying**: Filter by specific dynasties or view all concurrent regimes (new in v0.3.0)
63
+ - **Accurate Era Conversion**: Supports historical era names from 140 BCE to present
64
+ - **Comprehensive Historical Data**: Covers all major Chinese dynasties (498 era names)
65
+ - **Multiple Era Support**: Handles cases where multiple era names coexist in the same year
66
+ - **Zero Dependencies**: No additional packages required
67
+ - **TypeScript Support**: Complete type definitions included
68
+ - **Lightweight**: Small bundle size (~37KB minified), minimal impact on your project
69
+ - **Traditional Chinese Output**: All era names in Traditional Chinese characters
36
70
 
37
71
  ## Installation
38
72
 
@@ -57,93 +91,148 @@ pnpm add cn-era
57
91
  **ESM (ES Modules)**
58
92
 
59
93
  ```javascript
60
- import { convertYear } from 'cn-era';
94
+ import { convertYear, Dynasty } from 'cn-era';
61
95
 
62
- // Basic usage
96
+ // Basic usage - returns mainline dynasty by default
63
97
  const result = convertYear(618);
64
98
  console.log(result);
65
- // Output: [{ dynasty: '唐', reign_title: '武德', year_num: '元年' }]
99
+ // Output: [{ dynasty: 6, dynasty_name: '唐', reign_title: '武德', year_num: '元年' }]
66
100
  ```
67
101
 
68
102
  **CommonJS**
69
103
 
70
104
  ```javascript
71
- const { convertYear } = require('cn-era');
105
+ const { convertYear, Dynasty } = require('cn-era');
72
106
 
73
107
  const result = convertYear(618);
74
108
  console.log(result);
75
- // Output: [{ dynasty: '唐', reign_title: '武德', year_num: '元年' }]
109
+ // Output: [{ dynasty: 6, dynasty_name: '唐', reign_title: '武德', year_num: '元年' }]
76
110
  ```
77
111
 
78
112
  **More Examples**
79
113
 
80
114
  ```javascript
81
- import { convertYear } from 'cn-era';
115
+ import { convertYear, Dynasty } from 'cn-era';
82
116
 
83
- // Multiple eras in one year (era change)
117
+ // Default mode: returns only the mainline/orthodox dynasty
84
118
  convertYear(690);
119
+ // [{ dynasty: 6, dynasty_name: '唐', reign_title: '載初', year_num: '二年' }]
120
+
121
+ // Get all concurrent eras (including Wu Zhou)
122
+ convertYear(690, { mode: 'all' });
123
+ // [
124
+ // { dynasty: 6, dynasty_name: '唐', reign_title: '載初', year_num: '二年' },
125
+ // { dynasty: 77, dynasty_name: '武周', reign_title: '天授', year_num: '元年' }
126
+ // ]
127
+
128
+ // Filter by specific dynasty using Dynasty enum
129
+ convertYear(618, { dynasty: Dynasty.SUI });
85
130
  // [
86
- // { dynasty: '', reign_title: '載初', year_num: '二年' },
87
- // { dynasty: '武周', reign_title: '天授', year_num: '元年' }
131
+ // { dynasty: 5, dynasty_name: '', reign_title: '大業', year_num: '十四年' },
132
+ // { dynasty: 5, dynasty_name: '', reign_title: '義寧', year_num: '二年' },
133
+ // { dynasty: 5, dynasty_name: '隋', reign_title: '皇泰', year_num: '元年' }
88
134
  // ]
89
135
 
90
136
  // Republic of China era (post-1912)
91
137
  convertYear(2024);
92
- // [{ dynasty: '中華民國', reign_title: '民國', year_num: '一百一十三年' }]
138
+ // [{ dynasty: 21, dynasty_name: '中華民國', reign_title: '民國', year_num: '一百一十三年' }]
93
139
  ```
94
140
 
95
141
  ## API Documentation
96
142
 
97
- ### `convertYear(year: number): EraResult[]`
143
+ ### `convertYear(year: number, opts?: ConvertYearOptions): EraResult[]`
98
144
 
99
145
  Convert a Gregorian calendar year to Chinese historical era name(s).
100
146
 
101
147
  #### Parameters
102
148
 
103
149
  - `year` (number): Gregorian calendar year (range: -841 to 3000, 0 is invalid)
150
+ - `opts` (ConvertYearOptions, optional): Conversion options
151
+ - `mode?: 'mainline' | 'all'` - Conversion mode (default: `'mainline'`)
152
+ - `'mainline'`: Returns only the orthodox/mainline dynasty (recommended for most use cases)
153
+ - `'all'`: Returns all concurrent eras including non-orthodox regimes
154
+ - `dynasty?: Dynasty` - Filter by specific dynasty using Dynasty enum
104
155
 
105
156
  #### Returns
106
157
 
107
- Returns an array containing all era information for that year (some years may have multiple eras).
158
+ Returns an array containing all era information for that year.
108
159
 
109
160
  Each result object contains the following fields:
110
161
 
111
162
  ```typescript
112
163
  interface EraResult {
113
- dynasty: string; // Dynasty name, e.g., "唐", "宋"
164
+ dynasty: Dynasty; // Dynasty enum value (e.g., Dynasty.TANG = 6)
165
+ dynasty_name: string; // Dynasty name in Chinese, e.g., "唐", "宋"
114
166
  reign_title: string; // Era name, e.g., "武德", "貞觀"
115
167
  year_num: string; // Year in era, e.g., "元年", "三年"
116
168
  }
169
+
170
+ enum Dynasty {
171
+ XI_HAN = 29, // Western Han
172
+ DONG_HAN = 25, // Eastern Han
173
+ SUI = 5, // Sui
174
+ TANG = 6, // Tang
175
+ SONG = 15, // Song
176
+ YUAN = 18, // Yuan
177
+ MING = 19, // Ming
178
+ QING = 20, // Qing
179
+ ROC = 21, // Republic of China
180
+ // ... and more (see src/dynasty.ts for full list)
181
+ }
117
182
  ```
118
183
 
119
184
  #### Examples
120
185
 
121
186
  ```javascript
122
- // Regular year
187
+ import { convertYear, Dynasty } from 'cn-era';
188
+
189
+ // Regular year - mainline mode (default)
123
190
  convertYear(627);
124
- // [{ dynasty: '唐', reign_title: '貞觀', year_num: '元年' }]
191
+ // [{ dynasty: 6, dynasty_name: '唐', reign_title: '貞觀', year_num: '元年' }]
192
+
193
+ // Era change within same dynasty
194
+ convertYear(626, { mode: 'all' });
195
+ // [
196
+ // { dynasty: 6, dynasty_name: '唐', reign_title: '武德', year_num: '九年' },
197
+ // { dynasty: 6, dynasty_name: '唐', reign_title: '貞觀', year_num: '元年' }
198
+ // ]
199
+
200
+ // Mainline mode filters to orthodox dynasty (Song, not Liao)
201
+ convertYear(1000);
202
+ // [{ dynasty: 15, dynasty_name: '宋', reign_title: '咸平', year_num: '三年' }]
125
203
 
126
- // Era change year (multiple eras in one year)
127
- convertYear(626);
204
+ // All mode shows concurrent regimes
205
+ convertYear(1000, { mode: 'all' });
128
206
  // [
129
- // { dynasty: '', reign_title: '武德', year_num: '九年' },
130
- // { dynasty: '', reign_title: '貞觀', year_num: '元年' }
207
+ // { dynasty: 16, dynasty_name: '', reign_title: '統和', year_num: '十八年' },
208
+ // { dynasty: 15, dynasty_name: '', reign_title: '咸平', year_num: '三年' }
131
209
  // ]
132
210
 
133
- // Coexisting regimes (e.g., Northern and Southern Dynasties)
134
- convertYear(420);
211
+ // Filter by specific dynasty
212
+ convertYear(1000, { dynasty: Dynasty.LIAO });
213
+ // [{ dynasty: 16, dynasty_name: '遼', reign_title: '統和', year_num: '十八年' }]
214
+
215
+ // Three Kingdoms - mainline returns Wei (orthodox)
216
+ convertYear(221);
217
+ // [{ dynasty: 26, dynasty_name: '魏', reign_title: '黃初', year_num: '二年' }]
218
+
219
+ // Three Kingdoms - all mode shows concurrent states
220
+ convertYear(221, { mode: 'all' });
135
221
  // [
136
- // { dynasty: '南朝宋', reign_title: '永初', year_num: '元年' },
137
- // { dynasty: '北魏', reign_title: '泰常', year_num: '五年' }
222
+ // { dynasty: 26, dynasty_name: '', reign_title: '黃初', year_num: '二年' },
223
+ // { dynasty: 53, dynasty_name: '蜀漢', reign_title: '章武', year_num: '元年' }
138
224
  // ]
139
225
 
140
226
  // Republic of China
141
- convertYear(1912);
142
- // [{ dynasty: '中華民國', reign_title: '民國', year_num: '元年' }]
227
+ convertYear(1912, { mode: 'all' });
228
+ // [
229
+ // { dynasty: 20, dynasty_name: '清', reign_title: '宣統', year_num: '四年' },
230
+ // { dynasty: 21, dynasty_name: '中華民國', reign_title: '民國', year_num: '元年' }
231
+ // ]
143
232
 
144
233
  // BCE year
145
234
  convertYear(-140);
146
- // [{ dynasty: '西漢', reign_title: '建元', year_num: '元年' }]
235
+ // [{ dynasty: 29, dynasty_name: '西漢', reign_title: '建元', year_num: '元年' }]
147
236
  ```
148
237
 
149
238
  ## Supported Historical Periods
@@ -171,18 +260,28 @@ This library supports era name conversion from **Western Zhou Gonghe Regency (84
171
260
 
172
261
  ## Important Notes
173
262
 
174
- 1. **Era changes**: Some years return multiple results, which typically occurs when:
263
+ 1. **Mainline vs All Mode** (New in v0.3.0):
264
+ - **`mode: 'mainline'`** (default): Returns only the orthodox/mainline dynasty. Best for most use cases where you want the "standard" historical reference.
265
+ - **`mode: 'all'`**: Returns all concurrent eras including non-orthodox regimes. Useful for detailed historical research.
266
+ - Example: In year 1000, mainline mode returns Song (宋), while all mode returns both Song and Liao (遼).
267
+
268
+ 2. **Dynasty Enum System** (New in v0.3.0):
269
+ - Dynasty values are now represented by `Dynasty` enum using CBDB entity IDs
270
+ - Each result includes both `dynasty` (enum value) and `dynasty_name` (Chinese name)
271
+ - Use `Dynasty.TANG`, `Dynasty.SONG`, etc. for type-safe dynasty filtering
272
+
273
+ 3. **Era Changes**: Years with multiple results typically occur when:
175
274
  - Emperor changes era name (multiple era names within the same year)
176
275
  - Dynasty transitions
177
- - Coexisting regimes during periods of division
276
+ - Coexisting regimes during periods of division (use `mode: 'all'` to see all)
178
277
 
179
- 2. **Year range**: Currently supports 841 BCE to 3000 CE. Years outside this range will throw an error.
278
+ 4. **Year Range**: Currently supports -140 to 3000 CE. Years outside this range will throw an error.
180
279
 
181
- 3. **Historical accuracy**: Era name data is based on mainstream historical records. Disputed years follow standard histories.
280
+ 5. **Historical Accuracy**: Era name data is based on mainstream historical records. Disputed years follow standard histories. Dynasty priorities follow orthodox historical tradition.
182
281
 
183
- 4. **BCE years**: Negative numbers represent BCE years (e.g., -221 = 221 BCE)
282
+ 6. **BCE Years**: Negative numbers represent BCE years (e.g., -140 = 140 BCE, first year with era names)
184
283
 
185
- 5. **Traditional Chinese**: All output is in Traditional Chinese characters, which matches historical documents. Users can convert to Simplified Chinese as needed.
284
+ 7. **Traditional Chinese**: All output is in Traditional Chinese characters, which matches historical documents. Users can convert to Simplified Chinese as needed.
186
285
 
187
286
  ## Development
188
287
 
package/README.zh-CN.md CHANGED
@@ -11,28 +11,62 @@
11
11
 
12
12
  ## 当前状态
13
13
 
14
- **早期开发版本 (v0.1.1)**
14
+ **测试版本 (v0.3.0)**
15
15
 
16
- 本库目前处于早期开发阶段,已包含数据:
17
- - **唐朝** - 已完成(618-907 年)
18
- - **武周** - 已完成(684-705 年)
19
- - **宋朝** - 已完成(960-1279 年)
20
- - **元朝** - 已完成(1260-1368 年)
21
- - **明朝** - 已完成(1368-1644 年)
22
- - **清朝** - 已完成(1644-1912 年)
23
- - **中华民国** - 已完成(1912 年至今)
16
+ 本库现已包含**从公元前140年至今的完整年号覆盖**,涵盖中国历史所有主要朝代和政权:
24
17
 
25
- 我们正在积极补充其他朝代的完整历史年号数据。欢迎贡献!请查看 [TODO.md](TODO.md) 了解数据补充计划。
18
+ **上古时期:**
19
+ - **西汉**(公元前140年 - 公元8年)- 已完成
20
+ - **新朝**(9-23年)- 已完成
21
+ - **东汉**(25-220年)- 已完成
22
+
23
+ **三国两晋:**
24
+ - **曹魏**(220-265年)- 已完成
25
+ - **蜀汉**(221-263年)- 已完成
26
+ - **孙吴**(222-280年)- 已完成
27
+ - **西晋**(265-316年)- 已完成
28
+ - **东晋**(317-420年)- 已完成
29
+
30
+ **南北朝:**
31
+ - **南朝**:刘宋、南齐、梁、陈(420-589年)- 已完成
32
+ - **北朝**:北魏、东魏、西魏、北齐、北周(386-581年)- 已完成
33
+
34
+ **大一统王朝:**
35
+ - **隋朝**(581-618年)- 已完成
36
+ - **唐朝**(618-907年)- 已完成
37
+ - **武周**(690-705年)- 已完成
38
+
39
+ **五代十国:**
40
+ - **五代**:后梁、后唐、后晋、后汉、后周(907-960年)- 已完成
41
+
42
+ **中古时期:**
43
+ - **宋朝**(960-1279年)- 已完成
44
+ - **辽朝**(916-1125年)- 已完成(与宋并存)
45
+ - **西夏**(1038-1227年)- 已完成(与宋并存)
46
+ - **金朝**(1115-1234年)- 已完成(与宋并存)
47
+ - **元朝**(1271-1368年)- 已完成
48
+
49
+ **近古时期:**
50
+ - **明朝**(1368-1644年)- 已完成
51
+ - **清朝**(1644-1912年)- 已完成
52
+
53
+ **近现代:**
54
+ - **中华民国**(1912年至今)- 已完成
55
+
56
+ **总覆盖范围**:498个历史年号,跨越2164年(公元前140年 - 公元2024年)
26
57
 
27
58
  ## 特性
28
59
 
29
- - 精确的年号转换:支持从秦朝到清朝的历史年号
30
- - 完整的历史数据:涵盖中国历史各主要朝代
31
- - 多年号支持:处理同一年份多个年号并存的情况(如改元)
32
- - 零依赖:无需额外安装其他包
33
- - TypeScript 支持:提供完整的类型定义
34
- - 轻量化:体积小巧,不影响项目大小
35
- - 繁体输出:所有年号使用繁体中文,符合历史文献原貌
60
+ - **智能主线模式**:自动返回正统朝代年号,实用性更强(v0.3.0 新增)
61
+ - **朝代枚举系统**:使用标准化的 CBDB(中国历代人物传记数据库)朝代编码,可靠识别(v0.3.0 新增)
62
+ - **灵活查询**:可按朝代筛选或查看所有并存政权(v0.3.0 新增)
63
+ - **精确的年号转换**:支持从公元前140年至今的历史年号
64
+ - **完整的历史数据**:涵盖中国历史各主要朝代(498个年号)
65
+ - **多年号支持**:处理同一年份多个年号并存的情况
66
+ - **零依赖**:无需额外安装其他包
67
+ - **TypeScript 支持**:提供完整的类型定义
68
+ - **轻量化**:体积小巧(~37KB minified),不影响项目大小
69
+ - **繁体输出**:所有年号使用繁体中文,符合历史文献原貌
36
70
 
37
71
  ## 安装
38
72
 
@@ -57,85 +91,148 @@ pnpm add cn-era
57
91
  **ESM (ES Modules)**
58
92
 
59
93
  ```javascript
60
- import { convertYear } from 'cn-era';
94
+ import { convertYear, Dynasty } from 'cn-era';
61
95
 
62
- // 基础用法
96
+ // 基础用法 - 默认返回主线朝代
63
97
  const result = convertYear(618);
64
98
  console.log(result);
65
- // 输出: [{ dynasty: '唐', reign_title: '武德', year_num: '元年' }]
99
+ // 输出: [{ dynasty: 6, dynasty_name: '唐', reign_title: '武德', year_num: '元年' }]
66
100
  ```
67
101
 
68
102
  **CommonJS**
69
103
 
70
104
  ```javascript
71
- const { convertYear } = require('cn-era');
105
+ const { convertYear, Dynasty } = require('cn-era');
72
106
 
73
107
  const result = convertYear(618);
74
108
  console.log(result);
75
- // 输出: [{ dynasty: '唐', reign_title: '武德', year_num: '元年' }]
109
+ // 输出: [{ dynasty: 6, dynasty_name: '唐', reign_title: '武德', year_num: '元年' }]
76
110
  ```
77
111
 
78
112
  **更多示例**
79
113
 
80
114
  ```javascript
81
- import { convertYear } from 'cn-era';
115
+ import { convertYear, Dynasty } from 'cn-era';
82
116
 
83
- // 多个年号的情况(改元年份)
117
+ // 默认模式:只返回主线/正统朝代
84
118
  convertYear(690);
119
+ // [{ dynasty: 6, dynasty_name: '唐', reign_title: '載初', year_num: '二年' }]
120
+
121
+ // 查看所有并存年号(包括武周)
122
+ convertYear(690, { mode: 'all' });
123
+ // [
124
+ // { dynasty: 6, dynasty_name: '唐', reign_title: '載初', year_num: '二年' },
125
+ // { dynasty: 77, dynasty_name: '武周', reign_title: '天授', year_num: '元年' }
126
+ // ]
127
+
128
+ // 使用朝代枚举筛选特定朝代
129
+ convertYear(618, { dynasty: Dynasty.SUI });
85
130
  // [
86
- // { dynasty: '', reign_title: '載初', year_num: '二年' },
87
- // { dynasty: '武周', reign_title: '天授', year_num: '元年' }
131
+ // { dynasty: 5, dynasty_name: '', reign_title: '大業', year_num: '十四年' },
132
+ // { dynasty: 5, dynasty_name: '', reign_title: '義寧', year_num: '二年' },
133
+ // { dynasty: 5, dynasty_name: '隋', reign_title: '皇泰', year_num: '元年' }
88
134
  // ]
89
135
 
90
136
  // 民国纪年
91
137
  convertYear(2024);
92
- // [{ dynasty: '中華民國', reign_title: '民國', year_num: '一百一十三年' }]
138
+ // [{ dynasty: 21, dynasty_name: '中華民國', reign_title: '民國', year_num: '一百一十三年' }]
93
139
  ```
94
140
 
95
141
  ## API 文档
96
142
 
97
- ### `convertYear(year: number): EraResult[]`
143
+ ### `convertYear(year: number, opts?: ConvertYearOptions): EraResult[]`
98
144
 
99
145
  将公元年份转换为中国历史年号纪年。
100
146
 
101
147
  #### 参数
102
148
 
103
- - `year` (number): 公元年份,必须是正整数
149
+ - `year` (number): 公元年份(范围:-841 到 3000,0 不合法)
150
+ - `opts` (ConvertYearOptions, 可选): 转换选项
151
+ - `mode?: 'mainline' | 'all'` - 转换模式(默认:`'mainline'`)
152
+ - `'mainline'`:只返回正统/主线朝代(推荐用于大多数场景)
153
+ - `'all'`:返回所有并存的年号,包括非正统政权
154
+ - `dynasty?: Dynasty` - 使用朝代枚举筛选特定朝代
104
155
 
105
156
  #### 返回值
106
157
 
107
- 返回一个数组,包含该年份对应的所有年号信息(某些年份可能有多个年号)。
158
+ 返回一个数组,包含该年份对应的所有年号信息。
108
159
 
109
160
  每个结果对象包含以下字段:
110
161
 
111
162
  ```typescript
112
163
  interface EraResult {
113
- dynasty: string; // 朝代名称,如 "唐"、"宋"
114
- reign_title: string; // 年号,如 "武德"、"贞观"
164
+ dynasty: Dynasty; // 朝代枚举值(如 Dynasty.TANG = 6)
165
+ dynasty_name: string; // 朝代中文名称,如 ""、""
166
+ reign_title: string; // 年号,如 "武德"、"貞觀"
115
167
  year_num: string; // 年份,如 "元年"、"三年"
116
168
  }
169
+
170
+ enum Dynasty {
171
+ XI_HAN = 29, // 西汉
172
+ DONG_HAN = 25, // 东汉
173
+ SUI = 5, // 隋
174
+ TANG = 6, // 唐
175
+ SONG = 15, // 宋
176
+ YUAN = 18, // 元
177
+ MING = 19, // 明
178
+ QING = 20, // 清
179
+ ROC = 21, // 中华民国
180
+ // ... 更多朝代(详见 src/dynasty.ts)
181
+ }
117
182
  ```
118
183
 
119
184
  #### 示例
120
185
 
121
186
  ```javascript
122
- // 普通年份
187
+ import { convertYear, Dynasty } from 'cn-era';
188
+
189
+ // 普通年份 - 主线模式(默认)
123
190
  convertYear(627);
124
- // [{ dynasty: '唐', reign_title: '贞观', year_num: '元年' }]
191
+ // [{ dynasty: 6, dynasty_name: '唐', reign_title: '貞觀', year_num: '元年' }]
125
192
 
126
- // 改元年份(一年内有多个年号)
127
- convertYear(626);
193
+ // 同一朝代改元
194
+ convertYear(626, { mode: 'all' });
128
195
  // [
129
- // { dynasty: '唐', reign_title: '武德', year_num: '九年' },
130
- // { dynasty: '唐', reign_title: '贞观', year_num: '元年' }
196
+ // { dynasty: 6, dynasty_name: '唐', reign_title: '武德', year_num: '九年' },
197
+ // { dynasty: 6, dynasty_name: '唐', reign_title: '貞觀', year_num: '元年' }
131
198
  // ]
132
199
 
133
- // 南北朝等分裂时期(多个政权并存)
134
- convertYear(420);
200
+ // 主线模式过滤到正统朝代(宋而非辽)
201
+ convertYear(1000);
202
+ // [{ dynasty: 15, dynasty_name: '宋', reign_title: '咸平', year_num: '三年' }]
203
+
204
+ // all 模式显示并存政权
205
+ convertYear(1000, { mode: 'all' });
206
+ // [
207
+ // { dynasty: 16, dynasty_name: '遼', reign_title: '統和', year_num: '十八年' },
208
+ // { dynasty: 15, dynasty_name: '宋', reign_title: '咸平', year_num: '三年' }
209
+ // ]
210
+
211
+ // 按朝代筛选
212
+ convertYear(1000, { dynasty: Dynasty.LIAO });
213
+ // [{ dynasty: 16, dynasty_name: '遼', reign_title: '統和', year_num: '十八年' }]
214
+
215
+ // 三国 - 主线模式返回魏(正统)
216
+ convertYear(221);
217
+ // [{ dynasty: 26, dynasty_name: '魏', reign_title: '黃初', year_num: '二年' }]
218
+
219
+ // 三国 - all 模式显示并存政权
220
+ convertYear(221, { mode: 'all' });
221
+ // [
222
+ // { dynasty: 26, dynasty_name: '魏', reign_title: '黃初', year_num: '二年' },
223
+ // { dynasty: 53, dynasty_name: '蜀漢', reign_title: '章武', year_num: '元年' }
224
+ // ]
225
+
226
+ // 民国元年
227
+ convertYear(1912, { mode: 'all' });
135
228
  // [
136
- // { dynasty: '南朝宋', reign_title: '永初', year_num: '元年' },
137
- // { dynasty: '北魏', reign_title: '泰常', year_num: '五年' }
229
+ // { dynasty: 20, dynasty_name: '', reign_title: '宣統', year_num: '四年' },
230
+ // { dynasty: 21, dynasty_name: '中華民國', reign_title: '民國', year_num: '元年' }
138
231
  // ]
232
+
233
+ // 公元前年份
234
+ convertYear(-140);
235
+ // [{ dynasty: 29, dynasty_name: '西漢', reign_title: '建元', year_num: '元年' }]
139
236
  ```
140
237
 
141
238
  ## 使用场景
@@ -162,14 +259,28 @@ convertYear(420);
162
259
 
163
260
  ## 注意事项
164
261
 
165
- 1. **改元情况**:某些年份会返回多个结果,这通常发生在:
262
+ 1. **主线模式 vs All 模式**(v0.3.0 新增):
263
+ - **`mode: 'mainline'`**(默认):只返回正统/主线朝代。适合大多数需要"标准"历史参考的场景。
264
+ - **`mode: 'all'`**:返回所有并存的年号,包括非正统政权。适合详细的历史研究。
265
+ - 示例:公元 1000 年,主线模式返回宋(宋),all 模式同时返回宋和辽(遼)。
266
+
267
+ 2. **朝代枚举系统**(v0.3.0 新增):
268
+ - 朝代值现在使用 `Dynasty` 枚举表示,采用 CBDB 实体 ID
269
+ - 每个结果同时包含 `dynasty`(枚举值)和 `dynasty_name`(中文名称)
270
+ - 使用 `Dynasty.TANG`、`Dynasty.SONG` 等进行类型安全的朝代筛选
271
+
272
+ 3. **改元情况**:某些年份会返回多个结果,通常发生在:
166
273
  - 皇帝改元(同一年内更换年号)
167
274
  - 朝代更替
168
- - 历史分裂时期的多个政权并存
275
+ - 历史分裂时期的多个政权并存(使用 `mode: 'all'` 查看全部)
276
+
277
+ 4. **年份范围**:目前支持 -140 到 3000 年,超出范围会抛出错误
278
+
279
+ 5. **历史准确性**:年号数据基于主流历史记载,个别争议年份以通史为准。朝代优先级遵循正统历史传统。
169
280
 
170
- 2. **年份范围**:目前支持公元前 221 年到公元 1912 年,超出范围会返回空数组
281
+ 6. **公元前年份**:负数表示公元前年份(如 -140 = 公元前140年,年号制度开始)
171
282
 
172
- 3. **历史准确性**:年号数据基于主流历史记载,个别争议年份以通史为准
283
+ 7. **繁体中文**:所有输出使用繁体中文字符,符合历史文献原貌。用户可根据需要转换为简体中文。
173
284
 
174
285
  ## 开发
175
286