UNPKG

stepwright

Version:

A powerful web scraping library built with Playwright

110 lines 3.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.replaceIndexPlaceholders = replaceIndexPlaceholders; exports.replaceDataPlaceholders = replaceDataPlaceholders; exports.locatorFor = locatorFor; exports.cloneStepWithIndex = cloneStepWithIndex; // ---------------------------- // Internal helpers // ---------------------------- /** * Replace index placeholders in a string. * * @param {string} text - The text to replace placeholders in. * @param {number} i - The index to replace placeholders with. * * @since v1.0.0 * @author Muhammad Umer Farooq <umer@lablnet.com> * * @returns {string} - The text with placeholders replaced. * @since v1.0.0 * @company Framework Island */ function replaceIndexPlaceholders(text, i) { if (!text) return text; return text.replace(/\{\{\s*i\s*\}\}/g, i.toString()).replace(/\{\{\s*i_plus1\s*\}\}/g, (i + 1).toString()); } /** * Replace data placeholders like {{key}} with values from collector. * * @param {string} text - The text to replace placeholders in. * @param {object} collector - The collector object. * * @since v1.0.0 * @author Muhammad Umer Farooq <umer@lablnet.com> * * @returns {string} - The text with placeholders replaced. * @since v1.0.0 * @company Framework Island */ function replaceDataPlaceholders(text, collector) { if (!text) return text; let result = text; // Replace any {{key}} placeholders with values from collector const placeholderRegex = /\{\{\s*([^}]+)\s*\}\}/g; result = result.replace(placeholderRegex, (match, key) => { const value = collector[key]; if (value !== undefined && value !== null) { // Clean the value for filename use (remove special chars, trim whitespace) return String(value).trim().replace(/[^a-zA-Z0-9\s\-_]/g, '').replace(/\s+/g, '_'); } return match; // Keep original if not found }); return result; } /** * Get the locator for the given selector. * * @param {object} page - The page object. * @param {string} type - The type of selector. * @param {string} selector - The selector. * * @since v1.0.0 * @author Muhammad Umer Farooq <umer@lablnet.com> * * @returns {object} - The locator. * @since v1.0.0 * @company Framework Island */ function locatorFor(page, type, selector) { if (!type) return page.locator(selector); switch (type) { case 'id': return page.locator(`#${selector}`); case 'class': return page.locator(`.${selector}`); case 'tag': return page.locator(selector); case 'xpath': return page.locator(`xpath=${selector}`); default: return page.locator(selector); } } /** * Clone a step and apply index placeholders recursively. * * @param {object} step - The step object. * @param {number} idx - The index to replace placeholders with. * * @since v1.0.0 * @author Muhammad Umer Farooq <umer@lablnet.com> * * @returns {object} - The cloned step. * @since v1.0.0 * @company Framework Island */ function cloneStepWithIndex(step, idx) { const cloned = { ...step }; cloned.object = replaceIndexPlaceholders(cloned.object, idx); cloned.value = replaceIndexPlaceholders(cloned.value, idx); cloned.key = replaceIndexPlaceholders(cloned.key, idx); if (cloned.subSteps && cloned.subSteps.length > 0) { cloned.subSteps = cloned.subSteps.map((sub) => cloneStepWithIndex(sub, idx)); } return cloned; } //# sourceMappingURL=utils.js.map