0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Deno] 全角半角相互変換ライブラリだ!!!

Posted at

概要

Denoで文字列の全角と半角の相互変換するライブラリをつくりました!
一括変換だけでなく部分変換もできます。

deno.landとjsrにも公開しています!
deno.land: https://deno.land/x/zhconv
jsr.io: https://jsr.io/@suwakei/zhconv

Goバージョンもあります!
https://github.com/suwakei/go-zhconv
記事もあります!
https://zenn.dev/keitosuwahara/articles/77a57c54381abd

ぜひREADME代わりに当記事を読んでみてください!

インストール

主にインストール方法は3種類あります。
ソースコード中にこのようなimport文でdeno.land経由でインポートするか

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

jsr経由でインポートするか

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

denoコマンドを使ってローカルにインストールしてください。

deno add jsr:@suwakei/zhconv

機能

/**
 * h2z は、文字列中の半角文字を全角文字に変換します。
 * ASCII, カタカナ, 数字, 濁点/半濁点付きカタカナを扱います。
 * @param "str" The input string.
 * @returns The converted string.
 */
function h2z(str: string): string


/**
 * h2zAtは半角から全角に変換した文字列を返します。
 * 変換文字列は第2引数で選択できます。
 * @param "str" The input string.
 * @param "at" Indices of characters to convert.
 * @returns The converted string.
 */
function h2zAt(str: string, ...at: number[]): string


/**
 * z2h は、文字列中の全角文字を半角文字に変換します。
 * ASCII, カタカナ, 数字, 濁点/半濁点付きカタカナを扱います。
 * @param "str" The input string.
 * @returns The converted string.
 */
function z2h(str: string): string


/**
 * z2hAtは全角から半角に変換した文字列を返します。
 * 変換文字列は第2引数で選択できます。
 * @param "str" The input string.
 * @param "at" Indices of characters to convert.
 * @returns The converted string.
 */
function Z2hAt(str: tring, at: number[]): string

使い方

h2z関数

h2z は、文字列中の半角文字を全角文字に変換します。
ASCII, カタカナ, 数字, 濁点/半濁点付きカタカナを扱います。

import { h2z } from "https://deno.land/x/zhconv@1.1.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 ガギグパピプ!?.

z2h関数

z2h は、文字列中の全角文字を半角文字に変換します。
ASCII, カタカナ, 数字, 濁点/半濁点付きカタカナを扱います

import { z2h } from "https://deno.land/x/zhconv@1.1.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) // ①②③㈱㈲

h2zAt関数

h2zAtは半角から全角に変換した文字列を返します。
変換文字列は第2引数で選択できます。

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


let result = h2zAt("Hello, world!", 0, 7)
console.log(result) // Hello, world!.

let result = h2zAt("", 0) // Empty string.
console.log(result) // "".

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

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

let result = h2zAt("0123456789",0, 5) // Hankaku Digits to Zenkaku.
console.log(result) // 0123456789.

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

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

z2hAt関数

z2hAtは全角から半角に変換した文字列を返します。
変換文字列は第2引数で選択できます。

import { z2hAt } from "https://deno.land/x/zhconv@1.1.2/mod.ts


let result = z2hAt("Hello world", 0, 7)
console.log(result) // Hello, world!.

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

let result = z2hAt("0123456789", 0, 5)
console.log(result) // 0123456789.

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

let result = z2hAt("ァィゥェォッャュョ", 4)
console.log(result) // ァィゥェォッャュョ.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?