Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into add-popup-selecte…
Browse files Browse the repository at this point in the history
…d-text
  • Loading branch information
khaitruong922 committed Jul 12, 2024
2 parents 3bbb1ba + 751acba commit 611cb88
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
29 changes: 22 additions & 7 deletions ext/js/language/ja/japanese.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,18 @@ export function distributeFuriganaInflected(term, reading, source) {

// Miscellaneous

/**
* @param {number} codePoint
* @returns {boolean}
*/
export function isEmphaticCodePoint(codePoint) {
return (
codePoint === HIRAGANA_SMALL_TSU_CODE_POINT ||
codePoint === KATAKANA_SMALL_TSU_CODE_POINT ||
codePoint === KANA_PROLONGED_SOUND_MARK_CODE_POINT
);
}

/**
* @param {string} text
* @param {boolean} fullCollapse
Expand All @@ -737,13 +749,16 @@ export function distributeFuriganaInflected(term, reading, source) {
export function collapseEmphaticSequences(text, fullCollapse) {
let result = '';
let collapseCodePoint = -1;
for (const char of text) {
const c = char.codePointAt(0);
if (
c === HIRAGANA_SMALL_TSU_CODE_POINT ||
c === KATAKANA_SMALL_TSU_CODE_POINT ||
c === KANA_PROLONGED_SOUND_MARK_CODE_POINT
) {
for (let i = 0; i < text.length; ++i) {
const char = text[i];
const c = char.codePointAt(0) ?? -1;
if (isEmphaticCodePoint(c)) {
// Prevent match trailing emphatic
if (i === text.length - 1) {
result += char;
continue;
}

if (collapseCodePoint !== c) {
collapseCodePoint = c;
if (!fullCollapse) {
Expand Down
17 changes: 12 additions & 5 deletions test/japanese-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,11 +790,18 @@ describe('Japanese utility functions', () => {
[['', false], ''],
[['', true], ''],
[['っ', false], 'っ'],
[['っ', true], ''],
[['っっ', false], 'っ'],
[['っっ', true], ''],
[['っっっ', false], 'っ'],
[['っっっ', true], ''],
[['っ', true], 'っ'],
[['っっ', false], 'っっ'],
[['っっ', true], 'っ'],
[['っっっ', false], 'っっ'],
[['っっっ', true], 'っ'],

[['かっこいいっ', false], 'かっこいいっ'],
[['かっこいいっ', true], 'かこいいっ'],
[['かっこいいっっ', false], 'かっこいいっっ'],
[['かっこいいっっ', true], 'かこいいっ'],
[['かっこいいっっっ', false], 'かっこいいっっ'],
[['かっこいいっっっ', true], 'かこいいっ'],
];

test.each(data)('%o -> %o', (input, output) => {
Expand Down

0 comments on commit 611cb88

Please sign in to comment.