Skip to main content
Deno 2 is finally here 🎉️
Learn more

deno-zhconv

Test Status codecov

Overview

deno-zhconv is a library that supports character conversion in Deno. It performs mutual conversion between full-width and half-width characters and kana.

deno other repository

zhconv written in other langage

Installation

import {h2z, z2h, h2zAt, z2hAt} from "https://deno.land/x/zhconv@1.1.1/mod.ts"

or

import {h2z, z2h, h2zAt, z2hAt} from "https://jsr.io/@suwakei/zhconv";

Features

/**
 * h2z converts half-width characters (hankaku) in a string to full-width characters (zenkaku).
 * It handles ASCII, Katakana, digits, and Katakana with dakuten/handakuten.
 * @param "str" The input string.
 * @returns The converted string.
 */
function h2z(str: string): string


/**
 * h2zAt returns string that converted from half width to full width.
 * Conversion string can be selected with the second argument.
 * @param "str" The input string.
 * @param "at" Indices of characters to convert.
 * @returns The converted string.
 */
function h2zAt(str: string, ...at: number[]): string


/**
 * z2h converts full-width characters (zenkaku) in a string to half-width characters (hankaku).
 * It handles ASCII, Katakana, digits, and Katakana with dakuten/handakuten.
 * @param "str" The input string.
 * @returns The converted string.
 */
function z2h(str: string): string


/**
 * z2hAt returns string that converted from full-width to half-width.
 * Conversion string can be selected with the second argument.
 * @param "str" The input string.
 * @param "at" Indices of characters to convert.
 * @returns The converted string.
 */
function Z2hAt(str: tring, at: number[]): string

Usage

convert from HalfWidth to FullWidth

import { h2z } from "https://deno.land/x/zhconv@1.0.2/mod.ts"; // or import { h2z } from "https://jsr.io/@suwakei/zhconv";


let result = h2z("Hello, world!")
console.log(result) // Hello, world!.

let result = h2z("") // Empty string.
console.log(result) // "".

let result = h2z("ABC123アイウガパ") // No conversion needed (Zenkaku).
console.log(result) // ABC123アイウガパ.

let result = h2z("ABCdef XYZ!#$%&'()*+,-./:;<=>?@[¥]^_`{|}~ \\")
console.log(result) // ABCdef XYZ!#$%&’()*+,-./:;<=>?@[¥]^_‘{|}~ \.

let result = h2z("0123456789") // Hankaku Digits to Zenkaku.
console.log(result) // 0123456789.

let result = h2z("アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン") // Hankaku Katakana to Zenkaku.
console.log(result) // アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン.

let result = h2z("ァィゥェォッャュョ") // Hankaku Katakana (Small) to Zenkaku.
console.log(result) // ァィゥェォッャュョ.

let result = h2z("。、・ー「」")// Hankaku Katakana (Symbols) to Zenkaku.
console.log(result) // 。、・ー「」.

let result = h2z("ガギグゲゴザジズゼゾダヂヅデドバビブベボヴ") // Hankaku Katakana with Dakuten to Zenkaku".
console.log(result) // ガギグゲゴザジズゼゾダヂヅデドバビブベボヴ.

let result = h2z( "パピプペポ") // Hankaku Katakana with Handakuten to Zenkaku.
console.log(result) // パピプペポ.

let result = h2z("これはテストです。123 ABC アイウ ガギグ パピプ!") // Mixed Hankaku/Zenkaku/Other.
console.log(result) // これはテストです。123 ABC アイウ ガギグ パピプ!.

let result = h2z(" スペース ") // convert from Half width space to Full width space.
console.log(result) //   スペース .

let result = h2z("ア゙イ゚ン゙") // Dakuten/Handakuten cannot be applied.
console.log(result) // ア゛イ゜ン゛ Converted to full-width characters as separated( (ア->ア, ゙->゙).

let result = h2z("①②③㈱㈲") // Not convertible symbols.
console.log(result) // It is assumed that environment dependent characters will not be converted.

let result = h2z("1バイト文字と2バイト文字が混在するテキスト。 ABC 123 ガギグパピプ!?") // Long string with various conversions.
console.log(result) // 1バイト文字と2バイト文字が混在するテキスト。 ABC 123 ガギグパピプ!?.

convert from HalfWidth to FullWidth

import { z2h } from "https://deno.land/x/zhconv@1.0.2/mod.ts";


let result = z2h("ABCdef XYZ!#$%&’()*+,-./:;<=>?@[¥]^_‘{|}~")
console.log(result) // ABCdef XYZ!#$%&'()*+,-./:;<=>?@[¥]^_`{|}~

let result = z2h("0123456789")
console.log(result) // 0123456789

let result = z2h("アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン")
console.log(result) // アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン

let result = z2h("ァィゥェォッャュョ")
console.log(result) // ァィゥェォッャュョ ヮ is not converted because there is no corresponding character for half-width.

let result = z2h("。、・ー「」")
console.log(result) // 。、・ー「」

let result = z2h("ガギグゲゴザジズゼゾダヂヅデドバビブベボヴ")
console.log(result) // ガギグゲゴザジズゼゾダヂヅデドバビブベボヴ

let result = z2h("パピプペポ")
console.log(result) // パピプペポ

let result = z2h(" スペース ") // convert from Full width space to half width space
console.log(result) //  スペース 

let result = z2h("①②③㈱㈲") // It is assumed that environment dependent characters will not be converted.
console.log(result) // ①②③㈱㈲