text-aligner
v1.1.0
Published
Align text by adding spaces to each string so that all strings have the same number of English and Chinese characters.
Maintainers
Readme
text-align
A utility for aligning mixed English and CJK (Chinese, Japanese, Korean) text by adding appropriate padding spaces.
Features
- Supports mixed English and CJK text alignment
- Handles various CJK character ranges including:
- Chinese (Hanzi)
- Japanese (Hiragana, Katakana)
- Korean (Hangul)
- Customizable padding characters
- Zero dependencies
- TypeScript support
Installation
npm install text-alignerUsage
import {alignText} from 'text-aligner'
const strings = ['Hello 你好', 'Hi 早上好', 'Good morning 晚安']
const aligned = alignText(strings)
console.log(aligned)
// [
// 'Hello 你好\u2007\u2007\u2007\u2007\u2007\u2007\u2007\u2001',
// 'Hi 早上好\u2007\u2007\u2007\u2007\u2007\u2007\u2007\u2007\u2007\u2007',
// 'Good morning 晚安\u2001',
// ]API
alignText
function alignText(
strings: string[],
paddingMap?: Record<string, PaddingRule | string>
): string[]
type PaddingRule = {
test: RegExp | ((char: string) => boolean) | typeof FallbackKey
placeholder: string
}Aligns an array of strings by adding padding spaces to make all strings visually aligned.
Parameters
strings: Array of strings to be alignedpaddingMap(optional): Custom padding characters- Default values:
- CJK:
\u2001(Em Quad) - Fallback:
\u2007(Figure Space)
- CJK:
- Default values:
Returns
An array of aligned strings with appropriate padding spaces added.
License
MIT
