UNPKG

@rgglez/gettext-extractor

Version:

Gettext extractor for JavaScript, TypeScript, JSX, HTML and others

45 lines (44 loc) 1.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addConditionExtractor = void 0; const utils_1 = require("../../utils"); const validate_1 = require("../../../utils/validate"); function addConditionExtractor({ regex, text, textPlural }) { validate_1.Validate.required.argument({ regex }); validate_1.Validate.required.argument({ text }); validate_1.Validate.required.regexProperty({ regex }, 'arguments[0].regex'); validate_1.Validate.required.numberProperty({ text }, 'arguments[0].text'); validate_1.Validate.optional.numberProperty({ textPlural }, 'arguments[0].textPlural'); return (sourceFileContent, sourceFilePath, messages) => { // Check if the flags have a global flag if (regex.flags.indexOf('g') === -1) { // If not, add it. regex = new RegExp(regex.source, regex.flags + 'g'); } // Check if the flags have a multiline flag if (regex.flags.indexOf('m') === -1) { // If not, add it. regex = new RegExp(regex.source, regex.flags + 'm'); } const matches = sourceFileContent.match(regex); if (matches !== null) { for (let match of matches) { const matchGroups = regex.exec(match); if (matchGroups !== null) { const message = { text: matchGroups[text], references: [`${sourceFilePath}:${utils_1.RegexUtils.getLineNumber(sourceFileContent, match)}`], comments: [] }; if (textPlural) { message.textPlural = matchGroups[textPlural]; } messages.push(message); } // Reset last index for next string regex.lastIndex = 0; } } }; } exports.addConditionExtractor = addConditionExtractor;