japanese-text-utils
v1.0.3
Published
Convert between full-width (Zenkaku) and half-width (Hankaku) characters in Japanese text. Supports Katakana, ASCII letters/digits, spaces, punctuation, and symbols.
Maintainers
Readme
japanese-text-utils
Convert between full-width (Zenkaku) and half-width (Hankaku) characters in Japanese text.
Supports Katakana, ASCII letters/digits, spaces, punctuation, and symbols.
✨ Features
- Convert full-width → half-width (
ガラス→ガラス) - Convert half-width → full-width (
カタカナ→カタカナ) - Handles:
- Katakana (with dakuten/handakuten, e.g. ガ ↔ ガ, パ ↔ パ, ヴ ↔ ヴ)
- ASCII letters and digits (A → A, 7 → 7, etc.)
- Punctuation (! ↔ !, $ ↔ $, etc.)
- Spaces ( ↔ " ")
- Yen sign / backslash (¥ ↔ \)
📦 Installation
npm install japanese-text-utils
# or
yarn add japanese-text-utils
# or
pnpm add japanese-text-utils🚀 Usage
ES Module / TypeScript
import { toHalfSizeCharacters, toFullSizeCharacters } from 'japanese-text-utils';
console.log(toHalfSizeCharacters('ガラス-17個「テスト」'));
// => 'ガラス-17個「テスト」'
console.log(toFullSizeCharacters('カタカナ 123 ABC ~ \\\"'));
// => 'カタカナ 123 ABC ~ ¥"'CommonJS
const { toHalfSizeCharacters, toFullSizeCharacters } = require('japanese-text-utils');
console.log(toHalfSizeCharacters('"'~ ')); // => '"\'~ '
console.log(toFullSizeCharacters('"\'~ ')); // => '"'~ '📚 API
toHalfSizeCharacters(str: string): string
Convert a string from full-width to half-width characters.
- Katakana: ガ → ガ, ア → ア
- ASCII letters/digits: A → A, 7 → 7
- Punctuation: ! → !, $ → $
- Spaces: → " "
- ¥ → \
toFullSizeCharacters(str: string): string
Convert a string from half-width to full-width characters.
- Katakana: ガ → ガ, ア → ア
- ASCII letters/digits: A → A, 7 → 7
- Punctuation: ! → !, $ → $
- Spaces: " " →
- \ → ¥
🧪 Testing
You can run tests with Vitest or Jest. Example with Vitest:
npm install -D vitestimport { describe, it, expect } from 'vitest';
import { toHalfSizeCharacters, toFullSizeCharacters } from 'japanese-text-utils';
describe('Kana conversion', () => {
it('converts full-width to half-width', () => {
expect(toHalfSizeCharacters('ガ')).toBe('ガ');
});
it('converts half-width to full-width', () => {
expect(toFullSizeCharacters('ヴ')).toBe('ヴ');
});
});