🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

use-wg

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-wg

A TypeScript library for converting Chinese characters to Wade-Giles romanization

latest
Source
npmnpm
Version
2.0.4
Version published
Weekly downloads
143
-77.66%
Maintainers
1
Weekly downloads
 
Created
Source

use-wg

將中文字轉換為威妥瑪拼音的 TypeScript 函式庫。

CI License: MIT

English

功能特色

  • 中文字(繁體及簡體)→ 威妥瑪拼音轉換
  • 可設定聲調格式(上標、數字、無)
  • 處理中英文混合文字
  • URL 安全輸出模式
  • 上下文感知的多音字處理
  • 完整的 TypeScript 型別定義
  • 命令列工具(CLI)

安裝

npm install use-wg

CLI 命令列工具

直接透過 npx 使用:

# 基本用法
npx use-wg "台灣"              # 輸出: t'ai²-wan¹

# URL 安全模式
npx use-wg "台灣" --url-safe    # 輸出: tai-wan

# 聲調格式
npx use-wg "高雄" --tone number # 輸出: kao1-hsiung2
npx use-wg "高雄" --tone none   # 輸出: kao-hsiung

# 從 stdin 讀取
echo "台灣" | npx use-wg

# JSON 輸出(含分段資訊)
npx use-wg "台灣" --json

# 拼音轉換模式
npx use-wg --pinyin "zhong1"    # 輸出: chung¹

CLI 選項

選項簡寫說明預設值
--url-safe-u輸出 URL 安全格式false
--tone <format>-t聲調格式: superscript, number, nonesuperscript
--separator <sep>-s音節分隔符號-
--capitalize-c首字母大寫false
--json-j輸出 JSON 格式false
--pinyin-p拼音轉威妥瑪模式false

程式庫使用方式

基本轉換

import { toWadeGiles } from "use-wg";

// 基本轉換
toWadeGiles("台灣").text; // "t'ai²-wan¹"
toWadeGiles("台北").text; // "t'ai²-pei³"
toWadeGiles("高雄").text; // "kao¹-hsiung²"

聲調格式

// 上標聲調(預設)
toWadeGiles("高雄", { toneFormat: "superscript" }).text; // "kao¹-hsiung²"

// 數字聲調
toWadeGiles("高雄", { toneFormat: "number" }).text; // "kao1-hsiung2"

// 無聲調
toWadeGiles("高雄", { toneFormat: "none" }).text; // "kao-hsiung"

URL 安全模式

產生僅包含 ASCII 字元的輸出,適用於網址、檔案名稱和識別碼:

toWadeGiles("台灣", { urlSafe: true }).text; // "tai-wan"
toWadeGiles("氣功", { urlSafe: true }).text; // "chi-kung"

URL 安全模式會自動:

  • 移除聲調標記
  • ü 轉換為 u
  • 移除撇號('
  • 輸出小寫
  • 將空格轉換為連字號(-

中英文混合文字

轉換器能智慧處理混合文字:

toWadeGiles("Hello 世界!").text; // "Hello shih⁴-chieh⁴!"
toWadeGiles("iPhone 手機 Pro").text; // "iPhone shou³-chi¹ Pro"
toWadeGiles("2024年").text; // "2024nien²"

選項設定

toWadeGiles("台北", {
  toneFormat: "superscript", // 'superscript' | 'number' | 'none'
  separator: "-", // 音節分隔符號
  preserveNonChinese: true, // 保留非中文字元
  capitalize: false, // 首字母大寫
  polyphoneMode: "auto", // 'auto' | 'all'
  urlSafe: false, // 僅 ASCII 輸出
});

直接拼音轉換

import { pinyinToWadeGiles } from "use-wg";

pinyinToWadeGiles("zhong1"); // "chung¹"
pinyinToWadeGiles("guo2"); // "kuo²"
pinyinToWadeGiles("qi4"); // "ch'i⁴"

工具函式

import { containsChinese } from "use-wg";

containsChinese("Hello 世界"); // true
containsChinese("Hello World"); // false

分段資訊

取得詳細的轉換資訊:

const result = toWadeGiles("台北");

console.log(result.text); // "t'ai²-pei³"
console.log(result.segments);
// [
//   { original: "台", pinyin: "tai2", wadeGiles: "t'ai", tone: 2 },
//   { original: "北", pinyin: "bei3", wadeGiles: "pei", tone: 3 }
// ]

威妥瑪拼音對照表

主要轉換規則:

漢語拼音威妥瑪拼音範例
b → pba → pa八 bā → pa¹
p → p'pa → p'a怕 pà → p'a⁴
d → tda → ta大 dà → ta⁴
t → t'ta → t'a他 tā → t'a¹
g → kga → ka高 gāo → kao¹
k → k'ka → k'a看 kàn → k'an⁴
j → chji → chi雞 jī → chi¹
q → ch'qi → ch'i氣 qì → ch'i⁴
x → hsxi → hsi西 xī → hsi¹
zh → chzhi → chih知 zhī → chih¹
z → tszi → tzu子 zǐ → tzu³
c → ts'ci → tz'u次 cì → tz'u⁴
r → jri → jih日 rì → jih⁴
si → sssi → ssu四 sì → ssu⁴

API 參考

toWadeGiles(text, options?)

將中文文字轉換為威妥瑪拼音。

參數:

  • text (string) - 要轉換的中文文字
  • options (WadeGilesOptions) - 選用的設定選項

回傳值: WadeGilesResult

pinyinToWadeGiles(pinyin, options?)

將拼音音節轉換為威妥瑪拼音。

參數:

  • pinyin (string) - 帶有選用聲調數字的拼音音節
  • options ({ toneFormat?: ToneFormat }) - 選用的聲調格式

回傳值: string

containsChinese(text)

檢查字串是否包含中文字元。

參數:

  • text (string) - 要檢查的文字

回傳值: boolean

型別定義

type ToneFormat = "superscript" | "number" | "none";

interface WadeGilesOptions {
  toneFormat?: ToneFormat; // 預設: 'superscript'
  separator?: string; // 預設: '-'
  preserveNonChinese?: boolean; // 預設: true
  capitalize?: boolean; // 預設: false
  polyphoneMode?: "auto" | "all"; // 預設: 'auto'
  urlSafe?: boolean; // 預設: false
}

interface WadeGilesResult {
  text: string;
  segments: WadeGilesSegment[];
}

interface WadeGilesSegment {
  original: string;
  pinyin: string;
  wadeGiles: string;
  tone?: number;
  alternatives?: string[];
}

效能

執行效能測試:npm run benchmark

測試輸入平均時間 (ms)每秒運算次數
短文字 (2 字)台灣0.0037273,321
中等長度 (8 字)這是一個測試句子0.010892,664
長文字 (11 字)台北市信義區忠孝東路四段0.015066,465
中英混合Hello 世界! This is a test 測試0.0063159,867
含數字2024年台灣之旅0.0064157,288
URL 安全(短)台灣0.0031318,489
URL 安全(混合)My 台灣 Trip 2024年0.0057176,106

平均:約 180,000 次/秒

系統需求

  • Node.js >= 22
  • npm >= 10.0.0

開發

# 安裝相依套件
npm install

# 執行測試
npm test

# 建置
npm run build

# 型別檢查
npm run type-check

授權

MIT License - 詳見 LICENSE 檔案。

作者

Gary Lai - @imgarylai

Keywords

chinese

FAQs

Package last updated on 09 Jun 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts