<commit_before>'use strict';

const jp = require('jsonpath');

const indent = node => {
    const indent = node.parent().html().match(/^\s+/);
    return indent !== null ? indent[0] : '';
};

/**
 * Fills in translations in XLIFF files based on 'meaning' metadata as a key.
 *
 * @param doc an XLIFF document parsed with cheerio
 * @param lang a tree of keys and their corresponding translations
 * @return statistics the messages will be filled in-place
 */
const translate = (doc, lang, force) => {
    const units = doc('trans-unit').toArray();
    let stats = { count: 0, skip: 0 };

    units
        .map(unit => doc(unit))
        .filter(unit => unit.find('note').length > 0)
        .map(unit => {
            const source = unit.find('source');
            let target = unit.find('target');
            if (target.length === 0 && source.length === 1) {
                target = doc('<target />');
                source.after(indent(source), target);
            }

            return {
                target: target,
                id: doc(unit).find('note[from=meaning]').text()
            };
        })
        .filter(d => d.id && isKey(d.id))
        .forEach(d => {
            const parts = d.id.split('.').map(part => `['${part}']`);
            const query = '$' + parts.join('');
            const match = jp.query(lang, query);

            if (match.length === 1) {
                if (d.target.text() === '' || force) {
                    d.target.text(match[0]);
                    stats.count++;
                } else if (!force) {
                    stats.skip++;
                }
            }
        });

    return stats;
};


const isKey = val => !!val.match(/^[a-zA-Z0-9.]*$/);

module.exports = translate;
<commit_msg>fix: allow dash and underscore in keys (#5)<commit_after>'use strict';

const jp = require('jsonpath');

const indent = node => {
    const indent = node.parent().html().match(/^\s+/);
    return indent !== null ? indent[0] : '';
};

/**
 * Fills in translations in XLIFF files based on 'meaning' metadata as a key.
 *
 * @param doc an XLIFF document parsed with cheerio
 * @param lang a tree of keys and their corresponding translations
 * @return statistics the messages will be filled in-place
 */
const translate = (doc, lang, force) => {
    const units = doc('trans-unit').toArray();
    let stats = { count: 0, skip: 0 };

    units
        .map(unit => doc(unit))
        .filter(unit => unit.find('note').length > 0)
        .map(unit => {
            const source = unit.find('source');
            let target = unit.find('target');
            if (target.length === 0 && source.length === 1) {
                target = doc('<target />');
                source.after(indent(source), target);
            }

            return {
                target: target,
                id: doc(unit).find('note[from=meaning]').text()
            };
        })
        .filter(d => d.id && isKey(d.id))
        .forEach(d => {
            const parts = d.id.split('.').map(part => `['${part}']`);
            const query = '$' + parts.join('');
            const match = jp.query(lang, query);

            if (match.length === 1) {
                if (d.target.text() === '' || force) {
                    d.target.text(match[0]);
                    stats.count++;
                } else if (!force) {
                    stats.skip++;
                }
            }
        });

    return stats;
};


const isKey = val => !!val.match(/^[a-zA-Z0-9.\-\_]*$/);

module.exports = translate;

{'fraction_non_alphanumeric': 0.17257882882882883, 'fraction_numerical': 0.0059121621621621625, 'mean_word_length': 1.7267843438219495, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1620469', 'n_tokens_mistral': 1065, 'n_tokens_neox': 1006, 'n_words': 267}