tcy
Version:
A JavaScript library for processing Japanese vertical text, handling Tate-Chu-Yoko and text orientation adjustments
107 lines (92 loc) • 5.14 kB
JavaScript
import { expect } from 'chai';
import TCY from '../src/index.mjs';
describe('TCY', () => {
describe('transformText', () => {
it('should convert numbers with tcyDigit: 3', () => {
const sourceText = '12ああああ34ああ457あああ89';
const options = { tcyDigit: 3 };
const result = TCY.transformText(sourceText, options);
expect(result).to.equal('<span class="tcy">12</span>ああああ<span class="tcy">34</span>ああ<span class="tcy">457</span>あああ<span class="tcy">89</span>');
});
it('should convert exclamation and question marks', () => {
const sourceText = '!!ああああ!!!ああ!?あああ??';
const result = TCY.transformText(sourceText);
expect(result).to.equal('<span class="tcy">!!</span>ああああ!!!ああ<span class="tcy">!?</span>あああ<span class="tcy">??</span>');
});
it('should adjust orientation of special characters', () => {
const sourceText = '÷∴≠≦≧∧∨<>‐-';
const result = TCY.transformText(sourceText);
expect(result).to.equal(
'<span class="sideways">÷</span>' +
'<span class="sideways">∴</span>' +
'<span class="sideways">≠</span>' +
'<span class="sideways">≦</span>' +
'<span class="sideways">≧</span>' +
'<span class="sideways">∧</span>' +
'<span class="sideways">∨</span>' +
'<span class="sideways"><</span>' +
'<span class="sideways">></span>' +
'<span class="sideways">‐</span>' +
'<span class="sideways">-</span>'
);
});
it('should exclude email addresses from conversion', () => {
const sourceText = '連絡先はinfo@example21.comです。';
const result = TCY.transformText(sourceText);
expect(result).to.equal('連絡先はinfo@example21.comです。');
});
it('should process text within HTML tags', () => {
const sourceText = '<html><head><title>テスト</title></head><body>12ああああ34ああ457あああ89</body></html>';
const result = TCY.transformText(sourceText);
expect(result).to.equal('<span class="tcy">12</span>ああああ<span class="tcy">34</span>ああ457あああ<span class="tcy">89</span>');
});
it('should preserve character references in email addresses', () => {
const sourceText = '<a href="mailto:info@example.com">info@example.com</a>';
const result = TCY.transformText(sourceText);
expect(result).to.equal(sourceText);
});
it('should handle mixed character references and numbers', () => {
const sourceText = 'あい12うえ34';
const result = TCY.transformText(sourceText);
expect(result).to.equal('あい<span class="tcy">12</span>うえ<span class="tcy">34</span>');
});
});
describe('Options', () => {
it('should not convert numbers when tcyDigit is 0', () => {
const sourceText = '12ああああ34';
const options = { tcyDigit: 0 };
const result = TCY.transformText(sourceText, options);
expect(result).to.equal('12ああああ34');
});
it('should not adjust text orientation when autoTextOrientation is false', () => {
const sourceText = '÷∴≠≦≧';
const options = { autoTextOrientation: false };
const result = TCY.transformText(sourceText, options);
expect(result).to.equal('÷∴≠≦≧');
});
});
describe('Error handling', () => {
it('should return original text for invalid HTML', () => {
const sourceText = '<div>テスト</div'; // Incomplete closing tag
const result = TCY.transformText(sourceText);
expect(result).to.equal(sourceText);
});
it('should return empty string for empty input', () => {
const result = TCY.transformText('');
expect(result).to.equal('');
});
});
describe('tcyDigit: 0 behavior', () => {
const options = { tcyDigit: 0 };
it('should not convert any numbers regardless of digit count', () => {
const sourceText = '1桁:1、2桁:12、3桁:123、4桁:1234';
const result = TCY.transformText(sourceText, options);
expect(result).to.equal('1桁:1、2桁:12、3桁:123、4桁:1234');
});
it('should not convert numbers within HTML tags', () => {
const sourceText = '<div>12ああああ34</div>';
const result = TCY.transformText(sourceText, options);
expect(result).to.equal('<div>12ああああ34</div>');
});
});
});