henkan 2.4.11 → 2.4.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -2512,17 +2512,34 @@ function getWord(searchedWord, dict, kanjiDic, examples, definitions, noteTypeNa
2512
2512
  ...dictReading.commonness !== void 0 && dictReading.commonness.length > 0 ? { common: true } : {}
2513
2513
  }));
2514
2514
  word.translations = [];
2515
+ const meanings = dictWord.hasPhrases === true && examples !== void 0 ? [] : void 0;
2516
+ const seenPhrases = /* @__PURE__ */ new Set();
2515
2517
  for (const dictMeaning of dictWord.meanings) {
2516
2518
  const translationTypes = [];
2517
2519
  const translations = dictMeaning.translations.map(
2518
2520
  (translation) => {
2519
- if (typeof translation === "string") return translation;
2520
- else {
2521
+ if (typeof translation === "string") {
2522
+ if (meanings !== void 0) {
2523
+ const cleanTranslation = translation.replaceAll(/\([^)]*\)|\[[^\]]*\]|\{[^}]*\}/g, "").trim();
2524
+ if (!seenPhrases.has(cleanTranslation)) {
2525
+ seenPhrases.add(cleanTranslation);
2526
+ meanings.push(cleanTranslation);
2527
+ }
2528
+ }
2529
+ return translation;
2530
+ } else {
2521
2531
  const translationNoteAndTag = noteMap.get(
2522
2532
  translation.type
2523
2533
  );
2524
2534
  translationTypes.push(translationNoteAndTag[1]);
2525
2535
  word.tags.push(`word::${translationNoteAndTag[0]}`);
2536
+ if (meanings !== void 0) {
2537
+ const cleanTranslation = translation.translation.replaceAll(/\([^)]*\)|\[[^\]]*\]|\{[^}]*\}/g, "").trim();
2538
+ if (!seenPhrases.has(cleanTranslation)) {
2539
+ seenPhrases.add(cleanTranslation);
2540
+ meanings.push(cleanTranslation);
2541
+ }
2542
+ }
2526
2543
  return translation.translation;
2527
2544
  }
2528
2545
  }
@@ -2576,6 +2593,7 @@ function getWord(searchedWord, dict, kanjiDic, examples, definitions, noteTypeNa
2576
2593
  word.usuallyInKana = true;
2577
2594
  word.tags.push("word::usually_in_kana_for_all_senses");
2578
2595
  }
2596
+ seenPhrases.clear();
2579
2597
  if (kanjiDic !== void 0 && word.kanjiForms !== void 0) {
2580
2598
  const kanji = [];
2581
2599
  const seenChars = /* @__PURE__ */ new Set();
@@ -2596,7 +2614,7 @@ function getWord(searchedWord, dict, kanjiDic, examples, definitions, noteTypeNa
2596
2614
  }
2597
2615
  if (kanji.length > 0) word.kanji = kanji;
2598
2616
  }
2599
- if (dictWord.hasPhrases === true && examples !== void 0) {
2617
+ if (meanings !== void 0) {
2600
2618
  const exampleList = examples instanceof Map ? examples.get(dictWord.id) ?? [] : examples;
2601
2619
  const rkf = getValidForms(
2602
2620
  dictWord.readings,
@@ -2607,22 +2625,10 @@ function getWord(searchedWord, dict, kanjiDic, examples, definitions, noteTypeNa
2607
2625
  rkf.readings.map((r) => r.reading)
2608
2626
  );
2609
2627
  const kanjiForms = rkf.kanjiForms !== void 0 ? new Set(rkf.kanjiForms.map((kf) => kf.form)) : void 0;
2610
- const meanings = new Set(
2611
- dictWord.meanings.flatMap(
2612
- (m) => m.translations.map((t) => {
2613
- if (typeof t === "string")
2614
- return t.replaceAll(/\([^)]*\)|\[[^\]]*\]|\{[^}]*\}/g, "").trim();
2615
- else
2616
- return t.translation.replaceAll(/\([^)]*\)|\[[^\]]*\]|\{[^}]*\}/g, "").trim();
2617
- })
2618
- )
2619
- ).values().toArray();
2620
- let kanjiFormExamples = [];
2621
2628
  let readingMatchingKanjiFormExamples = [];
2629
+ let kanjiFormExamples = [];
2622
2630
  let readingExamples = [];
2623
- let readingMatchingKanjiForms = /* @__PURE__ */ new Set();
2624
- const readingMatchingKanjiFormsWithTranslations = /* @__PURE__ */ new Set();
2625
- const seenPhrases = /* @__PURE__ */ new Set();
2631
+ let hasReadingMatchingKanjiFormWithTranslation = false;
2626
2632
  for (const example of exampleList)
2627
2633
  for (let i = 0; i < example.parts.length; i++) {
2628
2634
  if (seenPhrases.has(example.phrase)) break;
@@ -2639,11 +2645,11 @@ function getWord(searchedWord, dict, kanjiDic, examples, definitions, noteTypeNa
2639
2645
  ex: example,
2640
2646
  partIndex: i,
2641
2647
  form: part.baseForm,
2648
+ ...referenceIDMatch ? { referenceIDMatch: true } : {},
2642
2649
  ...includesTranslation ? { includesTranslation: true } : {}
2643
2650
  });
2644
- readingMatchingKanjiForms.add(part.baseForm);
2645
- if (includesTranslation)
2646
- readingMatchingKanjiFormsWithTranslations.add(part.baseForm);
2651
+ if (!hasReadingMatchingKanjiFormWithTranslation && includesTranslation)
2652
+ hasReadingMatchingKanjiFormWithTranslation = true;
2647
2653
  } else
2648
2654
  kanjiFormExamples.push({
2649
2655
  ex: example,
@@ -2660,28 +2666,28 @@ function getWord(searchedWord, dict, kanjiDic, examples, definitions, noteTypeNa
2660
2666
  ex: example,
2661
2667
  partIndex: i,
2662
2668
  ...referenceIDMatch ? { referenceIDMatch: true } : {},
2663
- ...!referenceIDMatch && includesTranslation ? { includesTranslation: true } : {}
2669
+ ...includesTranslation ? { includesTranslation: true } : {}
2664
2670
  });
2665
2671
  seenPhrases.add(example.phrase);
2666
2672
  break;
2667
2673
  }
2668
2674
  }
2669
- if (readingMatchingKanjiFormsWithTranslations.size > 0) {
2670
- readingMatchingKanjiForms = readingMatchingKanjiFormsWithTranslations;
2671
- readingMatchingKanjiFormExamples = readingMatchingKanjiFormExamples.filter(
2672
- (ex) => ex.includesTranslation === true || ex.ex.parts.some(
2673
- (part) => readingMatchingKanjiForms.has(part.baseForm) && part.glossNumber !== void 0
2674
- )
2675
- );
2675
+ if (readingMatchingKanjiFormExamples.length > 0) {
2676
+ if (hasReadingMatchingKanjiFormWithTranslation)
2677
+ readingMatchingKanjiFormExamples = readingMatchingKanjiFormExamples.filter(
2678
+ (ex) => ex.includesTranslation === true || ex.referenceIDMatch === true || ex.ex.parts.some(
2679
+ (part) => ex.form === part.baseForm && part.glossNumber !== void 0
2680
+ )
2681
+ );
2682
+ kanjiFormExamples.length = 0;
2676
2683
  }
2677
- const hasKfExamplesWithTranslation = kanjiFormExamples.some(
2684
+ if (kanjiFormExamples.length > 0 && kanjiFormExamples.some(
2678
2685
  (ex) => ex.includesTranslation === true
2679
- );
2680
- if (kanjiFormExamples.length > 0 && readingMatchingKanjiForms.size > 0)
2686
+ ))
2681
2687
  kanjiFormExamples = kanjiFormExamples.filter(
2682
- (ex) => readingMatchingKanjiForms.has(ex.form) && (!hasKfExamplesWithTranslation || ex.includesTranslation === true || ex.ex.parts.some(
2683
- (part) => readingMatchingKanjiForms.has(part.baseForm) && part.glossNumber !== void 0
2684
- ))
2688
+ (ex) => ex.includesTranslation === true || ex.ex.parts.some(
2689
+ (part) => ex.form === part.baseForm && part.glossNumber !== void 0
2690
+ )
2685
2691
  );
2686
2692
  if (readingExamples.length > 0 && readingExamples.some(
2687
2693
  (ex) => ex.includesTranslation === true || ex.referenceIDMatch === true
@@ -2691,9 +2697,7 @@ function getWord(searchedWord, dict, kanjiDic, examples, definitions, noteTypeNa
2691
2697
  );
2692
2698
  let wordExamples = [
2693
2699
  ...word.kanjiForms !== void 0 ? [...readingMatchingKanjiFormExamples, ...kanjiFormExamples] : readingExamples
2694
- ].toSorted(
2695
- (a, b) => a.ex.phrase.length - b.ex.phrase.length
2696
- );
2700
+ ];
2697
2701
  seenPhrases.clear();
2698
2702
  const glossSpecificExamples = [];
2699
2703
  for (let i = 0; i < word.translations.length; i++)
@@ -2726,12 +2730,14 @@ function getWord(searchedWord, dict, kanjiDic, examples, definitions, noteTypeNa
2726
2730
  wordExamples = glossSpecificExamples;
2727
2731
  }
2728
2732
  if (wordExamples.length > 0) {
2729
- word.phrases = (glossSpecificExamples.length === 0 ? wordExamples.slice(0, 5) : wordExamples).map((ex) => ({
2730
- phrase: ex.ex.furigana ?? ex.ex.phrase,
2731
- translation: ex.ex.translation,
2732
- originalPhrase: ex.ex.phrase,
2733
- ...ex.ex.glossNumber !== void 0 ? { glossNumber: ex.ex.glossNumber } : {}
2734
- }));
2733
+ word.phrases = (glossSpecificExamples.length === 0 ? wordExamples.slice(0, 5) : wordExamples).map(
2734
+ (ex) => ({
2735
+ phrase: ex.ex.furigana ?? ex.ex.phrase,
2736
+ translation: ex.ex.translation,
2737
+ originalPhrase: ex.ex.phrase,
2738
+ ...ex.ex.glossNumber !== void 0 ? { glossNumber: ex.ex.glossNumber } : {}
2739
+ })
2740
+ );
2735
2741
  word.tags.push("word::has_phrases");
2736
2742
  if (glossSpecificExamples.length > 0)
2737
2743
  word.tags.push("word::has_meaning-specific_phrases");