switch-chinese 1.0.10 → 1.0.11
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 +1 -1
- package/readme.md +171 -6
- package/stcasc.d.ts +46 -6
- package/stcasc.lib.js +63 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "switch-chinese",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"description": "簡繁轉換,支援簡繁雙向轉換、智慧分詞、自訂詞庫、文字偵測及多種輸出格式,零依賴。 Lightweight Chinese converter library for conversion between Simplified and Traditional Chinese. 轻量级简繁体中文智能转换库,支持简繁双向转换、智能分词、自定义词库、文本检测及多种输出格式,零依赖。",
|
|
5
5
|
"main": "stcasc.lib.js",
|
|
6
6
|
"types": "stcasc.d.ts",
|
package/readme.md
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
- **轻量级**:零依赖,体积小,性能优异
|
|
16
16
|
- **智能转换**:支持基于词组的智能分词和「一简多繁」精准转换
|
|
17
|
+
- **多种数据类型**:支持字符串、数组、对象的转换,自动递归处理嵌套结构
|
|
17
18
|
- **自定义词库**:允许用户自定义简繁转换词汇
|
|
18
19
|
- **缓存机制**:支持字典缓存,避免重复初始化
|
|
19
20
|
- **简繁检测**:自动检测文本是简体中文、繁体中文还是未知类型
|
|
@@ -82,6 +83,54 @@ ChineseType 枚举值:
|
|
|
82
83
|
- `ChineseType.TRADITIONAL` (1): 繁体中文
|
|
83
84
|
- `ChineseType.UNKNOWN` (2): 未知类型
|
|
84
85
|
|
|
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
|
+
|
|
85
134
|
### 高级用法
|
|
86
135
|
|
|
87
136
|
#### 使用缓存优化性能
|
|
@@ -186,8 +235,14 @@ OutputFormat 枚举值:
|
|
|
186
235
|
|
|
187
236
|
返回包含以下方法的对象:
|
|
188
237
|
|
|
189
|
-
- `traditionalized(
|
|
190
|
-
- `
|
|
238
|
+
- `traditionalized(input, options?)`: 将简体中文转换为繁体中文
|
|
239
|
+
- `input`: 可以是字符串、数组或对象
|
|
240
|
+
- 字符串:直接转换返回新字符串
|
|
241
|
+
- 数组:转换所有字符串元素,其他类型保持不变
|
|
242
|
+
- 对象:递归转换所有字符串属性值,其他类型保持不变
|
|
243
|
+
- `simplized(input, options?)`: 将繁体中文转换为简体中文
|
|
244
|
+
- `input`: 可以是字符串、数组或对象
|
|
245
|
+
- 支持的数据类型同 `traditionalized`
|
|
191
246
|
- `detect(text)`: 检测文本的中文类型,返回 ChineseType 枚举值
|
|
192
247
|
- `cache`: 字典缓存对象
|
|
193
248
|
|
|
@@ -318,6 +373,7 @@ Lightweight Chinese converter library for bidirectional conversion between Simpl
|
|
|
318
373
|
|
|
319
374
|
- **Lightweight**: Zero dependencies, small footprint, excellent performance
|
|
320
375
|
- **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
|
|
321
377
|
- **Custom Dictionary**: User-defined conversion rules support
|
|
322
378
|
- **Caching Mechanism**: Dictionary caching to avoid repeated initialization
|
|
323
379
|
- **Text Detection**: Automatic detection of Simplified Chinese, Traditional Chinese, or unknown text
|
|
@@ -386,6 +442,54 @@ ChineseType enumeration values:
|
|
|
386
442
|
- `ChineseType.TRADITIONAL` (1): Traditional Chinese
|
|
387
443
|
- `ChineseType.UNKNOWN` (2): Unknown type
|
|
388
444
|
|
|
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
|
+
|
|
389
493
|
### Advanced Usage
|
|
390
494
|
|
|
391
495
|
#### Performance Optimization with Caching
|
|
@@ -490,8 +594,14 @@ Main function to create a converter instance.
|
|
|
490
594
|
|
|
491
595
|
An object containing the following methods:
|
|
492
596
|
|
|
493
|
-
- `traditionalized(
|
|
494
|
-
- `
|
|
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`
|
|
495
605
|
- `detect(text)`: Detect Chinese text type, returns ChineseType enumeration value
|
|
496
606
|
- `cache`: Dictionary cache object
|
|
497
607
|
|
|
@@ -622,6 +732,7 @@ Chinese Converter, Simplified Chinese, Traditional Chinese, Chinese Translation,
|
|
|
622
732
|
|
|
623
733
|
- **輕量級**:零依賴,體積小,效能優異
|
|
624
734
|
- **智能轉換**:支援基於詞組的智能分詞和「一簡多繁」精準轉換
|
|
735
|
+
- **多種資料類型**:支援字串、陣列、物件的轉換,自動遞迴處理巢狀結構
|
|
625
736
|
- **自訂詞庫**:允許使用者自訂簡繁轉換詞彙
|
|
626
737
|
- **快取機制**:支援字典快取,避免重複初始化
|
|
627
738
|
- **簡繁檢測**:自動檢測文字是簡體中文、繁體中文還是未知類型
|
|
@@ -690,6 +801,54 @@ ChineseType 列舉值:
|
|
|
690
801
|
- `ChineseType.TRADITIONAL` (1): 繁體中文
|
|
691
802
|
- `ChineseType.UNKNOWN` (2): 未知類型
|
|
692
803
|
|
|
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
|
+
|
|
693
852
|
### 進階用法
|
|
694
853
|
|
|
695
854
|
#### 使用快取最佳化效能
|
|
@@ -794,8 +953,14 @@ OutputFormat 列舉值:
|
|
|
794
953
|
|
|
795
954
|
回傳包含以下方法的物件:
|
|
796
955
|
|
|
797
|
-
- `traditionalized(
|
|
798
|
-
- `
|
|
956
|
+
- `traditionalized(input, options?)`: 將簡體中文轉換為繁體中文
|
|
957
|
+
- `input`: 可以是字串、陣列或物件
|
|
958
|
+
- 字串:直接轉換並回傳新字串
|
|
959
|
+
- 陣列:轉換所有字串元素,其他類型保持不變
|
|
960
|
+
- 物件:遞迴轉換所有字串屬性值,其他類型保持不變
|
|
961
|
+
- `simplized(input, options?)`: 將繁體中文轉換為簡體中文
|
|
962
|
+
- `input`: 可以是字串、陣列或物件
|
|
963
|
+
- 支援的資料類型同 `traditionalized`
|
|
799
964
|
- `detect(text)`: 檢測文字的中文類型,回傳 ChineseType 列舉值
|
|
800
965
|
- `cache`: 字典快取物件
|
|
801
966
|
|
package/stcasc.d.ts
CHANGED
|
@@ -33,12 +33,20 @@ export interface ConversionOptions {
|
|
|
33
33
|
format?: 0 | 1 | 2;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Tree node for combination dictionary
|
|
38
|
+
*/
|
|
39
|
+
interface CombTreeNode {
|
|
40
|
+
end?: string;
|
|
41
|
+
[key: string]: CombTreeNode | string | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
36
44
|
/**
|
|
37
45
|
* Cache object for storing conversion dictionaries
|
|
38
46
|
*/
|
|
39
47
|
export interface ConversionCache {
|
|
40
|
-
sc2tcCombTree?: Record<string,
|
|
41
|
-
tc2scCombTree?: Record<string,
|
|
48
|
+
sc2tcCombTree?: Record<string, CombTreeNode>;
|
|
49
|
+
tc2scCombTree?: Record<string, CombTreeNode>;
|
|
42
50
|
stDict?: Record<string, string>;
|
|
43
51
|
tsDict?: Record<string, string>;
|
|
44
52
|
}
|
|
@@ -56,20 +64,52 @@ export type CustomDictionary = Record<string, string | string[]>;
|
|
|
56
64
|
export interface StcascConverter {
|
|
57
65
|
/**
|
|
58
66
|
* Convert traditional Chinese to simplified Chinese
|
|
59
|
-
* @param text -
|
|
67
|
+
* @param text - String to convert
|
|
60
68
|
* @param options - Conversion options
|
|
61
|
-
* @returns Converted simplified Chinese
|
|
69
|
+
* @returns Converted simplified Chinese string
|
|
62
70
|
*/
|
|
63
71
|
simplized(text: string, options?: ConversionOptions): string;
|
|
64
72
|
|
|
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
|
+
|
|
65
89
|
/**
|
|
66
90
|
* Convert simplified Chinese to traditional Chinese
|
|
67
|
-
* @param text -
|
|
91
|
+
* @param text - String to convert
|
|
68
92
|
* @param options - Conversion options
|
|
69
|
-
* @returns Converted traditional Chinese
|
|
93
|
+
* @returns Converted traditional Chinese string
|
|
70
94
|
*/
|
|
71
95
|
traditionalized(text: string, options?: ConversionOptions): string;
|
|
72
96
|
|
|
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
|
+
|
|
73
113
|
/**
|
|
74
114
|
* Detect Chinese text type
|
|
75
115
|
* @param text - Text to detect
|
package/stcasc.lib.js
CHANGED
|
@@ -622,10 +622,7 @@ let sc2tcComb = {
|
|
|
622
622
|
var stDict = {}, tsDict = {};
|
|
623
623
|
var sc2tcCombTree = {}, tc2scCombTree = {};
|
|
624
624
|
|
|
625
|
-
function
|
|
626
|
-
options = options || {};
|
|
627
|
-
const format = options.format !== undefined ? options.format : OutputFormat.NORMAL;
|
|
628
|
-
|
|
625
|
+
function traditionalizedString(orgStr, format) {
|
|
629
626
|
if (!orgStr) return "";
|
|
630
627
|
var str = '', char;
|
|
631
628
|
for (var i = 0; i < orgStr.length; i++) {
|
|
@@ -716,10 +713,39 @@ function traditionalized(orgStr, options) {
|
|
|
716
713
|
return str;
|
|
717
714
|
}
|
|
718
715
|
|
|
719
|
-
function
|
|
716
|
+
function traditionalized(input, options) {
|
|
720
717
|
options = options || {};
|
|
721
718
|
const format = options.format !== undefined ? options.format : OutputFormat.NORMAL;
|
|
722
719
|
|
|
720
|
+
// Handle null/undefined
|
|
721
|
+
if (input == null) return input;
|
|
722
|
+
|
|
723
|
+
// Handle string
|
|
724
|
+
if (typeof input === 'string') {
|
|
725
|
+
return traditionalizedString(input, format);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// Handle array
|
|
729
|
+
if (Array.isArray(input)) {
|
|
730
|
+
return input.map(item => traditionalized(item, options));
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// Handle object
|
|
734
|
+
if (typeof input === 'object') {
|
|
735
|
+
const result = {};
|
|
736
|
+
for (const key in input) {
|
|
737
|
+
if (input.hasOwnProperty(key)) {
|
|
738
|
+
result[key] = traditionalized(input[key], options);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
return result;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
// Return other types as-is (number, boolean, etc.)
|
|
745
|
+
return input;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
function simplizedString(orgStr, format) {
|
|
723
749
|
if (!orgStr) return "";
|
|
724
750
|
var str = '', char;
|
|
725
751
|
for (var i = 0; i < orgStr.length; i++) {
|
|
@@ -810,6 +836,38 @@ function simplized(orgStr, options) {
|
|
|
810
836
|
return str;
|
|
811
837
|
}
|
|
812
838
|
|
|
839
|
+
function simplized(input, options) {
|
|
840
|
+
options = options || {};
|
|
841
|
+
const format = options.format !== undefined ? options.format : OutputFormat.NORMAL;
|
|
842
|
+
|
|
843
|
+
// Handle null/undefined
|
|
844
|
+
if (input == null) return input;
|
|
845
|
+
|
|
846
|
+
// Handle string
|
|
847
|
+
if (typeof input === 'string') {
|
|
848
|
+
return simplizedString(input, format);
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
// Handle array
|
|
852
|
+
if (Array.isArray(input)) {
|
|
853
|
+
return input.map(item => simplized(item, options));
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
// Handle object
|
|
857
|
+
if (typeof input === 'object') {
|
|
858
|
+
const result = {};
|
|
859
|
+
for (const key in input) {
|
|
860
|
+
if (input.hasOwnProperty(key)) {
|
|
861
|
+
result[key] = simplized(input[key], options);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
return result;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
// Return other types as-is (number, boolean, etc.)
|
|
868
|
+
return input;
|
|
869
|
+
}
|
|
870
|
+
|
|
813
871
|
function detect(text) {
|
|
814
872
|
if (!text) return ChineseType.UNKNOWN;
|
|
815
873
|
|