|
| 1 | +/** |
| 2 | + * OpenCC WASM - WebAssembly backend for OpenCC |
| 3 | + * |
| 4 | + * @packageDocumentation |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Options for creating a converter |
| 9 | + */ |
| 10 | +export interface ConverterOptions { |
| 11 | + /** |
| 12 | + * Source locale: 'cn' (Simplified Chinese), 'tw' (Traditional Taiwan), |
| 13 | + * 'hk' (Traditional Hong Kong), 't' (Traditional), 'jp' (Japanese) |
| 14 | + */ |
| 15 | + from?: string; |
| 16 | + |
| 17 | + /** |
| 18 | + * Target locale: 'cn' (Simplified Chinese), 'tw' (Traditional Taiwan), |
| 19 | + * 'hk' (Traditional Hong Kong), 't' (Traditional), 'jp' (Japanese) |
| 20 | + */ |
| 21 | + to?: string; |
| 22 | + |
| 23 | + /** |
| 24 | + * Config file name (e.g., 's2t.json', 't2s.json') |
| 25 | + * If specified, 'from' and 'to' will be ignored |
| 26 | + */ |
| 27 | + config?: string; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Async converter function that transforms text |
| 32 | + */ |
| 33 | +export type ConverterFunction = (text: string) => Promise<string>; |
| 34 | + |
| 35 | +/** |
| 36 | + * Synchronous custom converter function (for custom dictionaries) |
| 37 | + */ |
| 38 | +export type CustomConverterFunction = (text: string) => string; |
| 39 | + |
| 40 | +/** |
| 41 | + * Custom dictionary entry: [source, target] |
| 42 | + */ |
| 43 | +export type DictEntry = [string, string]; |
| 44 | + |
| 45 | +/** |
| 46 | + * Custom dictionary: array of entries or pipe-separated string |
| 47 | + */ |
| 48 | +export type CustomDict = DictEntry[] | string; |
| 49 | + |
| 50 | +/** |
| 51 | + * Locale mappings |
| 52 | + */ |
| 53 | +export interface LocaleMap { |
| 54 | + cn: string; |
| 55 | + tw: string; |
| 56 | + hk: string; |
| 57 | + jp: string; |
| 58 | + t: string; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * OpenCC namespace with all conversion functions |
| 63 | + */ |
| 64 | +export interface OpenCCNamespace { |
| 65 | + /** |
| 66 | + * Create a converter with the given options |
| 67 | + * |
| 68 | + * @example |
| 69 | + * ```typescript |
| 70 | + * const converter = OpenCC.Converter({ from: 'cn', to: 'tw' }); |
| 71 | + * const result = await converter('简体中文'); |
| 72 | + * ``` |
| 73 | + */ |
| 74 | + Converter(opts: ConverterOptions): ConverterFunction; |
| 75 | + |
| 76 | + /** |
| 77 | + * Create a custom converter with user-defined dictionary |
| 78 | + * |
| 79 | + * @param dict - Array of [source, target] pairs or pipe-separated string |
| 80 | + * |
| 81 | + * @example |
| 82 | + * ```typescript |
| 83 | + * const custom = OpenCC.CustomConverter([ |
| 84 | + * ['"', '「'], |
| 85 | + * ['"', '」'], |
| 86 | + * ]); |
| 87 | + * const result = custom('He said "hello"'); |
| 88 | + * ``` |
| 89 | + */ |
| 90 | + CustomConverter(dict: CustomDict): CustomConverterFunction; |
| 91 | + |
| 92 | + /** |
| 93 | + * Create a converter with additional custom dictionaries |
| 94 | + * |
| 95 | + * @param fromLocale - Source locale |
| 96 | + * @param toLocale - Target locale |
| 97 | + * @param extraDicts - Additional custom dictionaries to apply after conversion |
| 98 | + * |
| 99 | + * @example |
| 100 | + * ```typescript |
| 101 | + * const converter = OpenCC.ConverterFactory('cn', 'tw', [ |
| 102 | + * [['"', '「'], ['"', '」']] |
| 103 | + * ]); |
| 104 | + * const result = await converter('简体中文 "test"'); |
| 105 | + * ``` |
| 106 | + */ |
| 107 | + ConverterFactory( |
| 108 | + fromLocale: string, |
| 109 | + toLocale: string, |
| 110 | + extraDicts?: CustomDict[] |
| 111 | + ): ConverterFunction; |
| 112 | + |
| 113 | + /** |
| 114 | + * Locale constants for 'from' and 'to' options |
| 115 | + */ |
| 116 | + Locale: { |
| 117 | + from: LocaleMap; |
| 118 | + to: LocaleMap; |
| 119 | + }; |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * OpenCC main export |
| 124 | + */ |
| 125 | +export const OpenCC: OpenCCNamespace; |
| 126 | + |
| 127 | +/** |
| 128 | + * Default export |
| 129 | + */ |
| 130 | +export default OpenCC; |
0 commit comments