UNPKG

@jirimracek/conjugate-esp

Version:

Spanish verb conjugator, castellano, voseo, canarias, formal

192 lines 7.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.insertTags = exports.tagDiffs = exports.clearAccents = exports.clearLastAccent = exports.strongify = exports.esdrujula = exports.syllabify = void 0; const diff = require("fast-diff"); const Plain = ['a', 'e', 'i', 'o', 'u', 'ü']; const Accented = ['á', 'é', 'í', 'ó', 'ú']; const Vovels = [...Plain, ...Accented]; const Strong = ['a', 'e', 'o', 'á', 'é', 'í', 'ó', 'ú']; const Unbreakable = ['bl', 'cl', 'fl', 'gl', 'kl', 'll', 'pl', 'tl', 'br', 'cr', 'dr', 'fr', 'gr', 'kr', 'pr', 'rr', 'tr', 'ch']; function syllabify(phrase) { if (!phrase) { return ['']; } if (phrase.length < 2) { return [phrase]; } let current = 0; const result = []; while (phrase.length > (current + 1)) { result.push(phrase[current]); if (Vovels.includes(phrase[current])) { if (Strong.includes(phrase[current]) && Strong.includes(phrase[current + 1])) { result.push('-'); } if (!Vovels.includes(phrase[current + 1])) { if ((current + 2) < phrase.length) { if (Unbreakable.includes(`${phrase[current + 1]}${phrase[current + 2]}`) || Vovels.includes(phrase[current + 2])) { result.push('-'); } } } } else { if (!(Vovels.includes(phrase[current + 1]) || Unbreakable.includes(`${phrase[current]}${phrase[current + 1]}`))) { result.push('-'); } } ++current; } result.push(phrase[current]); return result.join('').split('-'); } exports.syllabify = syllabify; function esdrujula(phrase) { if (!phrase) return phrase; const expressions = phrase.split(' '); const last = expressions.pop(); if (/[áéíóú]/.test(last)) { return phrase; } const list = syllabify(last); if (list.length < 3) { return phrase; } const syllable = list[list.length - 3]; if (/[aeo]/.test(syllable)) { list[list.length - 3] = syllable.replace(/([aeo])/, (match, p1) => { return Accented[Plain.indexOf(p1)]; }); } else { list[list.length - 3] = syllable.replace(/(.*)([ui])/, (match, p1, p2) => { return `${p1}${Accented[Plain.indexOf(p2)]}`; }); } expressions.push(list.join('')); return expressions.join(' '); } exports.esdrujula = esdrujula; function strongify(phrase, nth) { if (nth < 1 || undefined === phrase || '' === phrase) { return phrase; } const syllables = syllabify(phrase); const index = syllables.length - nth; if (index < 0) { return phrase; } if (/[aeoáéíóú]/.test(syllables[index])) { return phrase; } syllables[index] = syllables[index].replace(/(.*)([ui])(.*)/, (match, p1, p2, p3) => { return `${p1}${Accented[Plain.indexOf(p2)]}${p3}`; }); return syllables.join(''); } exports.strongify = strongify; function clearLastAccent(word) { if (!word) return ''; return word.replace(/(.*)([áéíóú])/, (match, p1, p2) => { return `${p1}${Plain[Accented.indexOf(p2)]}`; }); } exports.clearLastAccent = clearLastAccent; function clearAccents(word) { if (!word) return ''; return word.replace(/([áéíóú])/g, (match, p1) => { return Plain[Accented.indexOf(p1)]; }); } exports.clearAccents = clearAccents; function tagDiffs(simulated, conjugated, tags, mode) { if (conjugated === '-' || mode === false) { return conjugated; } const conjugatedBase = conjugated.split(' '); const conjugatedWord = conjugatedBase.pop(); const simulatedWord = simulated.split(' ').pop(); if (mode === true) { if (simulatedWord !== conjugatedWord) { conjugatedBase.push(`${tags.start}${conjugatedWord}${tags.end}`); } else { conjugatedBase.push(conjugatedWord); } return conjugatedBase.join(' '); } const result = []; const chunks = diff(simulatedWord, conjugatedWord); chunks.forEach((chunk, index) => { switch (chunk[0]) { case diff.EQUAL: result.push(chunk[1]); break; case diff.INSERT: result.push(`${tags.start}${chunk[1]}${tags.end}`); break; case diff.DELETE: if (tags.del && (index === chunks.length - 1 || chunks[index + 1][0] === diff.EQUAL)) { result.push(`${tags.start}${tags.del}${tags.end}`); } break; } }); conjugatedBase.push(result.join('')); return conjugatedBase.join(' '); } exports.tagDiffs = tagDiffs; function insertTags(simulated, conjugated, tags, mode) { Object.keys(conjugated).forEach(key => { const modeKey = key; Object.keys(conjugated[modeKey]).forEach(subKey => { switch (modeKey) { case 'Impersonal': if (subKey === 'Gerundio') { conjugated[modeKey][subKey] = tagDiffs(simulated[modeKey][subKey], conjugated[modeKey][subKey], tags, mode); } else if (subKey === 'Participio') { const parts = conjugated[modeKey][subKey].split('/'); if (parts.length > 1) { conjugated[modeKey][subKey] = [tagDiffs(simulated[modeKey][subKey], parts[0], tags, mode), tagDiffs(simulated[modeKey][subKey], parts[1], tags, mode) ].join('/'); } else { conjugated[modeKey][subKey] = tagDiffs(simulated[modeKey][subKey], conjugated[modeKey][subKey], tags, mode); } } break; case 'Indicativo': conjugated[modeKey][subKey] = conjugated[modeKey][subKey].map((line, index) => { return tagDiffs(simulated[modeKey][subKey][index], line, tags, mode); }); break; case 'Subjuntivo': conjugated[modeKey][subKey] = conjugated[modeKey][subKey].map((line, index) => { return tagDiffs(simulated[modeKey][subKey][index], line, tags, mode); }); break; case 'Imperativo': conjugated[modeKey][subKey] = conjugated[modeKey][subKey].map((line, index) => { return tagDiffs(simulated[modeKey][subKey][index], line, tags, mode); }); break; } }); }); } exports.insertTags = insertTags; //# sourceMappingURL=stringutils.js.map