summaryrefslogtreecommitdiff
path: root/test/utilities/translator.js
blob: e288c8df2e7ea742b296878add58f1d2f2a91fa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright (C) 2023-2024  Yomitan Authors
 * Copyright (C) 2020-2022  Yomichan Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

// eslint-disable-next-line no-template-curly-in-string
const placeholder = '${title}';

/**
 * @template {import('test/translator').OptionsType} T
 * @param {T} type
 * @param {import('test/translator').OptionsPresetObject} optionsPresets
 * @param {import('test/translator').OptionsList} optionsArray
 * @returns {import('test/translator').OptionsPresetGeneric<T>}
 * @throws {Error}
 */
function getCompositePreset(type, optionsPresets, optionsArray) {
    const preset = /** @type {import('test/translator').OptionsPresetGeneric<T>} */ ({type});
    if (!Array.isArray(optionsArray)) { optionsArray = [optionsArray]; }
    for (const entry of optionsArray) {
        switch (typeof entry) {
            case 'string':
                {
                    if (!Object.prototype.hasOwnProperty.call(optionsPresets, entry)) {
                        throw new Error('Options preset not found');
                    }
                    const preset2 = optionsPresets[entry];
                    if (preset2.type !== type) {
                        throw new Error('Invalid options preset type');
                    }
                    Object.assign(preset, structuredClone(preset2));
                }
                break;
            case 'object':
                if (entry.type !== type) {
                    throw new Error('Invalid options preset type');
                }
                Object.assign(preset, structuredClone(entry));
                break;
            default:
                throw new Error('Invalid options type');
        }
    }
    return preset;
}


/**
 * @param {string} dictionaryName
 * @param {import('test/translator').OptionsPresetObject} optionsPresets
 * @param {import('test/translator').OptionsList} optionsArray
 * @returns {import('translation').FindKanjiOptions}
 */
export function createFindKanjiOptions(dictionaryName, optionsPresets, optionsArray) {
    const preset = getCompositePreset('kanji', optionsPresets, optionsArray);

    /** @type {import('translation').KanjiEnabledDictionaryMap} */
    const enabledDictionaryMap = new Map();
    const presetEnabledDictionaryMap = preset.enabledDictionaryMap;
    if (Array.isArray(presetEnabledDictionaryMap)) {
        for (const [key, value] of presetEnabledDictionaryMap) {
            enabledDictionaryMap.set(key === placeholder ? dictionaryName : key, value);
        }
    }

    return {
        enabledDictionaryMap,
        removeNonJapaneseCharacters: !!preset.removeNonJapaneseCharacters,
    };
}

/**
 * @param {string} dictionaryName
 * @param {import('test/translator').OptionsPresetObject} optionsPresets
 * @param {import('test/translator').OptionsList} optionsArray
 * @returns {import('translation').FindTermsOptions}
 */
export function createFindTermsOptions(dictionaryName, optionsPresets, optionsArray) {
    const preset = getCompositePreset('terms', optionsPresets, optionsArray);

    /** @type {import('translation').TermEnabledDictionaryMap} */
    const enabledDictionaryMap = new Map();
    const presetEnabledDictionaryMap = preset.enabledDictionaryMap;
    if (Array.isArray(presetEnabledDictionaryMap)) {
        for (const [key, value] of presetEnabledDictionaryMap) {
            enabledDictionaryMap.set(key === placeholder ? dictionaryName : key, value);
        }
    }

    /** @type {import('translation').FindTermsTextReplacements} */
    const textReplacements = [];
    if (Array.isArray(preset.textReplacements)) {
        for (const value of preset.textReplacements) {
            if (Array.isArray(value)) {
                const array = [];
                for (const {pattern, flags, replacement} of value) {
                    array.push({pattern: new RegExp(pattern, flags), replacement});
                }
                textReplacements.push(array);
            } else {
                // Null
                textReplacements.push(value);
            }
        }
    }

    const {
        matchType,
        deinflect,
        mainDictionary,
        sortFrequencyDictionary,
        sortFrequencyDictionaryOrder,
        removeNonJapaneseCharacters,
        excludeDictionaryDefinitions,
        searchResolution,
        language,
    } = preset;

    return {
        matchType: typeof matchType !== 'undefined' ? matchType : 'exact',
        deinflect: typeof deinflect !== 'undefined' ? deinflect : true,
        mainDictionary: typeof mainDictionary !== 'undefined' && mainDictionary !== placeholder ? mainDictionary : dictionaryName,
        sortFrequencyDictionary: typeof sortFrequencyDictionary !== 'undefined' ? sortFrequencyDictionary : null,
        sortFrequencyDictionaryOrder: typeof sortFrequencyDictionaryOrder !== 'undefined' ? sortFrequencyDictionaryOrder : 'ascending',
        removeNonJapaneseCharacters: typeof removeNonJapaneseCharacters !== 'undefined' ? removeNonJapaneseCharacters : false,
        textReplacements,
        enabledDictionaryMap,
        excludeDictionaryDefinitions: Array.isArray(excludeDictionaryDefinitions) ? new Set(excludeDictionaryDefinitions) : null,
        searchResolution: typeof searchResolution !== 'undefined' ? searchResolution : 'letter',
        language: typeof language !== 'undefined' ? language : 'ja',
    };
}