Skip to content

Commit 842e9f6

Browse files
committed
update
1 parent c918134 commit 842e9f6

File tree

5 files changed

+384
-20
lines changed

5 files changed

+384
-20
lines changed

Switch Traditional Chinese and Simplified Chinese/lib/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "switch-chinese",
3-
"version": "1.0.10",
4-
"description": "簡繁轉換,支援簡繁雙向轉換、智慧分詞、自訂詞庫、文字偵測及多種輸出格式,零依賴。 Lightweight Chinese converter library for conversion between Simplified and Traditional Chinese. 轻量级简繁体中文智能转换库,支持简繁双向转换、智能分词、自定义词库、文本检测及多种输出格式,零依赖。",
3+
"version": "1.0.12",
4+
"description": "繁簡轉換,支援簡繁雙向轉換、智慧分詞、自訂詞庫、文字偵測及多種輸出格式,零依賴。 Lightweight Chinese converter library for conversion between Simplified and Traditional Chinese. 轻量级简繁体中文智能转换库,支持简繁双向转换、智能分词、自定义词库、文本检测及多种输出格式,零依赖。",
55
"main": "stcasc.lib.js",
66
"types": "stcasc.d.ts",
77
"type": "module",

Switch Traditional Chinese and Simplified Chinese/lib/readme.md

Lines changed: 171 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
- **轻量级**:零依赖,体积小,性能优异
1616
- **智能转换**:支持基于词组的智能分词和「一简多繁」精准转换
17+
- **多种数据类型**:支持字符串、数组、对象的转换,自动递归处理嵌套结构
1718
- **自定义词库**:允许用户自定义简繁转换词汇
1819
- **缓存机制**:支持字典缓存,避免重复初始化
1920
- **简繁检测**:自动检测文本是简体中文、繁体中文还是未知类型
@@ -82,6 +83,54 @@ ChineseType 枚举值:
8283
- `ChineseType.TRADITIONAL` (1): 繁体中文
8384
- `ChineseType.UNKNOWN` (2): 未知类型
8485

86+
#### 转换数组和对象
87+
88+
库支持转换数组和对象中的所有字符串,非字符串值保持原样:
89+
90+
```javascript
91+
import stcasc from 'switch-chinese';
92+
93+
const { traditionalized, simplized } = stcasc();
94+
95+
// 转换数组
96+
const arr = ['简体中文', '软件', '网络', 123, true, null];
97+
const arrTc = traditionalized(arr);
98+
console.log(arrTc);
99+
// 输出: ['簡體中文', '軟體', '網路', 123, true, null]
100+
101+
// 转换对象
102+
const obj = {
103+
title: '简体中文标题',
104+
description: '这是一个简体中文描述',
105+
count: 100,
106+
active: true,
107+
tags: ['软件', '网络', '服务器']
108+
};
109+
const objTc = traditionalized(obj);
110+
console.log(objTc);
111+
// 输出: {
112+
// title: '簡體中文標題',
113+
// description: '這是一個簡體中文描述',
114+
// count: 100,
115+
// active: true,
116+
// tags: ['軟體', '網路', '伺服器']
117+
// }
118+
119+
// 转换嵌套结构
120+
const nested = {
121+
user: {
122+
name: '简体名称',
123+
profile: {
124+
bio: '这是简体中文简介',
125+
skills: ['软件开发', '网络管理']
126+
}
127+
},
128+
count: 42
129+
};
130+
const nestedTc = traditionalized(nested);
131+
// 所有字符串属性值都会被转换,数字等其他类型保持不变
132+
```
133+
85134
### 高级用法
86135

87136
#### 使用缓存优化性能
@@ -186,8 +235,14 @@ OutputFormat 枚举值:
186235

187236
返回包含以下方法的对象:
188237

189-
- `traditionalized(text, options?)`: 将简体中文转换为繁体中文
190-
- `simplized(text, options?)`: 将繁体中文转换为简体中文
238+
- `traditionalized(input, options?)`: 将简体中文转换为繁体中文
239+
- `input`: 可以是字符串、数组或对象
240+
- 字符串:直接转换返回新字符串
241+
- 数组:转换所有字符串元素,其他类型保持不变
242+
- 对象:递归转换所有字符串属性值,其他类型保持不变
243+
- `simplized(input, options?)`: 将繁体中文转换为简体中文
244+
- `input`: 可以是字符串、数组或对象
245+
- 支持的数据类型同 `traditionalized`
191246
- `detect(text)`: 检测文本的中文类型,返回 ChineseType 枚举值
192247
- `cache`: 字典缓存对象
193248

@@ -318,6 +373,7 @@ Lightweight Chinese converter library for bidirectional conversion between Simpl
318373

319374
- **Lightweight**: Zero dependencies, small footprint, excellent performance
320375
- **Intelligent Conversion**: Context-aware word segmentation and accurate one-to-many character mapping
376+
- **Multiple Data Types**: Supports string, array, and object conversion with automatic recursive processing
321377
- **Custom Dictionary**: User-defined conversion rules support
322378
- **Caching Mechanism**: Dictionary caching to avoid repeated initialization
323379
- **Text Detection**: Automatic detection of Simplified Chinese, Traditional Chinese, or unknown text
@@ -386,6 +442,54 @@ ChineseType enumeration values:
386442
- `ChineseType.TRADITIONAL` (1): Traditional Chinese
387443
- `ChineseType.UNKNOWN` (2): Unknown type
388444

445+
#### Converting Arrays and Objects
446+
447+
The library supports converting all strings in arrays and objects, while preserving non-string values:
448+
449+
```javascript
450+
import stcasc from 'switch-chinese';
451+
452+
const { traditionalized, simplized } = stcasc();
453+
454+
// Convert array
455+
const arr = ['简体中文', '软件', '网络', 123, true, null];
456+
const arrTc = traditionalized(arr);
457+
console.log(arrTc);
458+
// Output: ['簡體中文', '軟體', '網路', 123, true, null]
459+
460+
// Convert object
461+
const obj = {
462+
title: '简体中文标题',
463+
description: '这是一个简体中文描述',
464+
count: 100,
465+
active: true,
466+
tags: ['软件', '网络', '服务器']
467+
};
468+
const objTc = traditionalized(obj);
469+
console.log(objTc);
470+
// Output: {
471+
// title: '簡體中文標題',
472+
// description: '這是一個簡體中文描述',
473+
// count: 100,
474+
// active: true,
475+
// tags: ['軟體', '網路', '伺服器']
476+
// }
477+
478+
// Convert nested structures
479+
const nested = {
480+
user: {
481+
name: '简体名称',
482+
profile: {
483+
bio: '这是简体中文简介',
484+
skills: ['软件开发', '网络管理']
485+
}
486+
},
487+
count: 42
488+
};
489+
const nestedTc = traditionalized(nested);
490+
// All string property values will be converted, other types remain unchanged
491+
```
492+
389493
### Advanced Usage
390494

391495
#### Performance Optimization with Caching
@@ -490,8 +594,14 @@ Main function to create a converter instance.
490594

491595
An object containing the following methods:
492596

493-
- `traditionalized(text, options?)`: Convert Simplified Chinese to Traditional Chinese
494-
- `simplized(text, options?)`: Convert Traditional Chinese to Simplified Chinese
597+
- `traditionalized(input, options?)`: Convert Simplified Chinese to Traditional Chinese
598+
- `input`: Can be a string, array, or object
599+
- String: Directly converts and returns a new string
600+
- Array: Converts all string elements, other types remain unchanged
601+
- Object: Recursively converts all string property values, other types remain unchanged
602+
- `simplized(input, options?)`: Convert Traditional Chinese to Simplified Chinese
603+
- `input`: Can be a string, array, or object
604+
- Supports the same data types as `traditionalized`
495605
- `detect(text)`: Detect Chinese text type, returns ChineseType enumeration value
496606
- `cache`: Dictionary cache object
497607

@@ -622,6 +732,7 @@ Chinese Converter, Simplified Chinese, Traditional Chinese, Chinese Translation,
622732

623733
- **輕量級**:零依賴,體積小,效能優異
624734
- **智能轉換**:支援基於詞組的智能分詞和「一簡多繁」精準轉換
735+
- **多種資料類型**:支援字串、陣列、物件的轉換,自動遞迴處理巢狀結構
625736
- **自訂詞庫**:允許使用者自訂簡繁轉換詞彙
626737
- **快取機制**:支援字典快取,避免重複初始化
627738
- **簡繁檢測**:自動檢測文字是簡體中文、繁體中文還是未知類型
@@ -690,6 +801,54 @@ ChineseType 列舉值:
690801
- `ChineseType.TRADITIONAL` (1): 繁體中文
691802
- `ChineseType.UNKNOWN` (2): 未知類型
692803

804+
#### 轉換陣列和物件
805+
806+
函式庫支援轉換陣列和物件中的所有字串,非字串值保持原樣:
807+
808+
```javascript
809+
import stcasc from 'switch-chinese';
810+
811+
const { traditionalized, simplized } = stcasc();
812+
813+
// 轉換陣列
814+
const arr = ['简体中文', '软件', '网络', 123, true, null];
815+
const arrTc = traditionalized(arr);
816+
console.log(arrTc);
817+
// 輸出: ['簡體中文', '軟體', '網路', 123, true, null]
818+
819+
// 轉換物件
820+
const obj = {
821+
title: '简体中文标题',
822+
description: '这是一个简体中文描述',
823+
count: 100,
824+
active: true,
825+
tags: ['软件', '网络', '服务器']
826+
};
827+
const objTc = traditionalized(obj);
828+
console.log(objTc);
829+
// 輸出: {
830+
// title: '簡體中文標題',
831+
// description: '這是一個簡體中文描述',
832+
// count: 100,
833+
// active: true,
834+
// tags: ['軟體', '網路', '伺服器']
835+
// }
836+
837+
// 轉換巢狀結構
838+
const nested = {
839+
user: {
840+
name: '简体名称',
841+
profile: {
842+
bio: '这是简体中文简介',
843+
skills: ['软件开发', '网络管理']
844+
}
845+
},
846+
count: 42
847+
};
848+
const nestedTc = traditionalized(nested);
849+
// 所有字串屬性值都會被轉換,數字等其他類型保持不變
850+
```
851+
693852
### 進階用法
694853

695854
#### 使用快取最佳化效能
@@ -794,8 +953,14 @@ OutputFormat 列舉值:
794953

795954
回傳包含以下方法的物件:
796955

797-
- `traditionalized(text, options?)`: 將簡體中文轉換為繁體中文
798-
- `simplized(text, options?)`: 將繁體中文轉換為簡體中文
956+
- `traditionalized(input, options?)`: 將簡體中文轉換為繁體中文
957+
- `input`: 可以是字串、陣列或物件
958+
- 字串:直接轉換並回傳新字串
959+
- 陣列:轉換所有字串元素,其他類型保持不變
960+
- 物件:遞迴轉換所有字串屬性值,其他類型保持不變
961+
- `simplized(input, options?)`: 將繁體中文轉換為簡體中文
962+
- `input`: 可以是字串、陣列或物件
963+
- 支援的資料類型同 `traditionalized`
799964
- `detect(text)`: 檢測文字的中文類型,回傳 ChineseType 列舉值
800965
- `cache`: 字典快取物件
801966

Switch Traditional Chinese and Simplified Chinese/lib/stcasc.d.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,20 @@ export interface ConversionOptions {
3333
format?: 0 | 1 | 2;
3434
}
3535

36+
/**
37+
* Tree node for combination dictionary
38+
*/
39+
interface CombTreeNode {
40+
end?: string;
41+
[key: string]: CombTreeNode | string | undefined;
42+
}
43+
3644
/**
3745
* Cache object for storing conversion dictionaries
3846
*/
3947
export interface ConversionCache {
40-
sc2tcCombTree?: Record<string, any>;
41-
tc2scCombTree?: Record<string, any>;
48+
sc2tcCombTree?: Record<string, CombTreeNode>;
49+
tc2scCombTree?: Record<string, CombTreeNode>;
4250
stDict?: Record<string, string>;
4351
tsDict?: Record<string, string>;
4452
}
@@ -56,20 +64,52 @@ export type CustomDictionary = Record<string, string | string[]>;
5664
export interface StcascConverter {
5765
/**
5866
* Convert traditional Chinese to simplified Chinese
59-
* @param text - Text to convert
67+
* @param text - String to convert
6068
* @param options - Conversion options
61-
* @returns Converted simplified Chinese text
69+
* @returns Converted simplified Chinese string
6270
*/
6371
simplized(text: string, options?: ConversionOptions): string;
6472

73+
/**
74+
* Convert traditional Chinese to simplified Chinese (array version)
75+
* @param data - Array to convert (converts all strings in the array)
76+
* @param options - Conversion options
77+
* @returns Array with converted strings
78+
*/
79+
simplized<T extends unknown[]>(data: T, options?: ConversionOptions): T;
80+
81+
/**
82+
* Convert traditional Chinese to simplified Chinese (object version)
83+
* @param data - Object to convert (converts all string property values)
84+
* @param options - Conversion options
85+
* @returns Object with converted string values
86+
*/
87+
simplized<T extends Record<string, unknown>>(data: T, options?: ConversionOptions): T;
88+
6589
/**
6690
* Convert simplified Chinese to traditional Chinese
67-
* @param text - Text to convert
91+
* @param text - String to convert
6892
* @param options - Conversion options
69-
* @returns Converted traditional Chinese text
93+
* @returns Converted traditional Chinese string
7094
*/
7195
traditionalized(text: string, options?: ConversionOptions): string;
7296

97+
/**
98+
* Convert simplified Chinese to traditional Chinese (array version)
99+
* @param data - Array to convert (converts all strings in the array)
100+
* @param options - Conversion options
101+
* @returns Array with converted strings
102+
*/
103+
traditionalized<T extends unknown[]>(data: T, options?: ConversionOptions): T;
104+
105+
/**
106+
* Convert simplified Chinese to traditional Chinese (object version)
107+
* @param data - Object to convert (converts all string property values)
108+
* @param options - Conversion options
109+
* @returns Object with converted string values
110+
*/
111+
traditionalized<T extends Record<string, unknown>>(data: T, options?: ConversionOptions): T;
112+
73113
/**
74114
* Detect Chinese text type
75115
* @param text - Text to detect

0 commit comments

Comments
 (0)