japanese-fullhalf-convert
v1.0.1
Published
Convert Japanese characters between full-width (全角) and half-width (半角) including Katakana, numbers, and punctuation
Maintainers
Readme
japanese-fullhalf-convert
A JavaScript package for converting Japanese characters between full-width (全角) and half-width (半角) formats.
Features
- Convert full-width Katakana to half-width Katakana (カタカナ ↔ カタカナ)
- Convert full-width numbers to half-width numbers (123 ↔ 123)
- Convert full-width punctuation and symbols to half-width
- Convert full-width spaces to half-width spaces
- Bidirectional conversion support
Installation
Method 1: npm install (Recommended)
npm install japanese-fullhalf-convertMethod 2: Import from CDN (Browser)
<!-- Using unpkg -->
<script src="https://unpkg.com/japanese-fullhalf-convert@latest/index.js"></script>
<script>
// Available as global variable: jpFullhalfConvert
const { toHalfWidth, toFullWidth } = jpFullhalfConvert;
</script>
<!-- Using jsdelivr -->
<script src="https://cdn.jsdelivr.net/npm/japanese-fullhalf-convert@latest/index.js"></script>Method 3: ES6 Import (with bundler)
import { toHalfWidth, toFullWidth } from 'japanese-fullhalf-convert';Usage
CommonJS (Node.js)
const { toHalfWidth, toFullWidth } = require('japanese-fullhalf-convert');
// Convert full-width to half-width
const fullWidth = 'カタカナ123';
const halfWidth = toHalfWidth(fullWidth);
console.log(halfWidth); // Output: カタカナ123
// Convert half-width to full-width
const halfWidth2 = 'カタカナ123';
const fullWidth2 = toFullWidth(halfWidth2);
console.log(fullWidth2); // Output: カタカナ123
// Examples with different character types
console.log(toHalfWidth('HELLO')); // Output: HELLO
console.log(toFullWidth('hello')); // Output: hello
console.log(toHalfWidth(' ')); // Full-width space -> half-width space
console.log(toFullWidth(' ')); // Half-width space -> full-width spaceES6 Modules
import { toHalfWidth, toFullWidth } from 'japanese-fullhalf-convert';
const result = toHalfWidth('カタカナ123');
console.log(result); // Output: カタカナ123Browser (CDN)
<script src="https://unpkg.com/japanese-fullhalf-convert@latest/index.js"></script>
<script>
// Access via global variable
const { toHalfWidth, toFullWidth } = jpFullhalfConvert;
// Or use window object
const result = window.jpFullhalfConvert.toHalfWidth('カタカナ123');
console.log(result); // Output: カタカナ123
</script>Direct File Import (Browser)
<!-- Download index.js from npm and include locally -->
<script src="./path/to/index.js"></script>
<script>
const { toHalfWidth, toFullWidth } = jpFullhalfConvert;
</script>API
toHalfWidth(str)
Converts full-width Japanese characters to half-width.
Parameters:
str(string): Input string with full-width characters
Returns:
- (string): String with half-width characters
Example:
toHalfWidth('カタカナ123'); // Returns: 'カタカナ123'toFullWidth(str)
Converts half-width Japanese characters to full-width.
Parameters:
str(string): Input string with half-width characters
Returns:
- (string): String with full-width characters
Example:
toFullWidth('カタカナ123'); // Returns: 'カタカナ123'Supported Conversions
Katakana
- Full-width Katakana (ア-ン, ァ-ォ, ャ-ョ, ッ, ー) ↔ Half-width Katakana (ア-ン, ァ-ォ, ャ-ョ, ッ, ー)
Numbers
- Full-width numbers (0-9) ↔ Half-width numbers (0-9)
Letters
- Full-width uppercase (A-Z) ↔ Half-width uppercase (A-Z)
- Full-width lowercase (a-z) ↔ Half-width lowercase (a-z)
Punctuation and Symbols
- Full-width punctuation and symbols ↔ Half-width equivalents
Spaces
- Full-width space ( ) ↔ Half-width space ( )
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
