UNPKG

open-agreements

Version:

Open-source legal template filling CLI and library

59 lines • 2.95 kB
const DOUBLE_DOLLAR = /\$\s*\$/; const DOUBLE_PERCENT = /%\s*%/; export function evaluatePostconditions(input) { const { outputText, manifests, fieldValues, migratedAnchorsByField } = input; const checks = []; for (const manifest of manifests) { const fieldId = manifest.field_id; for (const postcondition of manifest.postconditions) { if (postcondition === 'no_unresolved_placeholder') { const tag = `{${fieldId}}`; const present = outputText.includes(tag); checks.push({ name: `selector:${fieldId}:no_unresolved_placeholder`, passed: !present, details: present ? `Unrendered tag ${tag} remains in output` : undefined, }); } else if (postcondition === 'all_occurrences_identical') { const value = fieldValues[fieldId]; if (typeof value !== 'string' || value.trim() === '') { // Nothing to compare when no value was supplied. checks.push({ name: `selector:${fieldId}:all_occurrences_identical`, passed: true }); continue; } const anchors = migratedAnchorsByField[fieldId] ?? []; const leftover = anchors.filter((a) => outputText.includes(a)); const valuePresent = outputText.includes(value); const passed = valuePresent && leftover.length === 0; checks.push({ name: `selector:${fieldId}:all_occurrences_identical`, passed, details: passed ? undefined : leftover.length > 0 ? `Divergent occurrence(s): source anchor(s) still present: ${leftover.join(', ')}` : `Field value "${value}" not found in output`, }); } else if (postcondition === 'no_double_dollar') { const lines = outputText.split('\n').filter((l) => DOUBLE_DOLLAR.test(l)); checks.push({ name: `selector:${fieldId}:no_double_dollar`, passed: lines.length === 0, details: lines.length > 0 ? `Double-dollar artifact on ${lines.length} line(s)` : undefined, }); } else if (postcondition === 'no_double_percent') { const lines = outputText.split('\n').filter((l) => DOUBLE_PERCENT.test(l)); checks.push({ name: `selector:${fieldId}:no_double_percent`, passed: lines.length === 0, details: lines.length > 0 ? `Double-percent artifact on ${lines.length} line(s)` : undefined, }); } } } return checks; } //# sourceMappingURL=postconditions.js.map