/** * @typedef {{ * requestId: string, * department: string, * ownerEmail: string, * title: string, * referencedIds: string[], * contentSha256: string, * recordUuid: string, * summaryLine: string * }} ParsedIntakeTextRecord */ /** * @typedef {{ ok: true, record: ParsedIntakeTextRecord } | { ok: false, rawLine: string, reason: string }} TextParseOutcome */ const RAW_INTAKE_TEXT_LINES = [ ' [REQ-1042]\u00a0dept: Facilities | owner: Avery.Quinn@Example.com | Replace lobby badge reader (blocks REQ-1008 and REQ-1019) ', '[REQ-1043] dept: Finance\u200b | owner: morgan.lee@example.com | Close Q1 travel ledger', '[TICKET-99] dept: Legal | owner: sam.chen@example.com | Review vendor addendum', '[REQ-1045] dept: HR | owner: jo.patel-at-example.com | Publish onboarding calendar', ]; // Zero-width formatting characters (\u200B..\u200D, \uFEFF) survive String.prototype.trim(). const ZERO_WIDTH_CHARACTERS = /[\u200b-\u200d\ufeff]/g; // Named capture groups extract each field in one pass after invisible characters and spaces normalize. const INTAKE_TEXT_PATTERN = /^\[(?REQ-\d{4})\]\s*dept:\s*(?[A-Za-z][A-Za-z &/-]{0,29})\s*\|\s*owner:\s*(?\S+)\s*\|\s*(?.+)$/; const EMAIL_TOKEN_PATTERN = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/; const REQUEST_CODE_GLOBAL_PATTERN = /REQ-\d{4}/g; /** * Parses four raw intake lines, strips invisible formatting characters, extracts * named regex groups and secondary request mentions, and computes deterministic * SHA-256 digests alongside random v4 UUIDs. * * @returns {void} */ function inspectParsedIntakeText() { logZeroWidthDemo_(); /** @type {ParsedIntakeTextRecord[]} */ const accepted = []; /** @type {Array<{ rawLine: string, reason: string }>} */ const rejected = []; for (const rawLine of RAW_INTAKE_TEXT_LINES) { const outcome = parseIntakeTextLine(rawLine); if (outcome.ok) { accepted.push(outcome.record); } else { rejected.push({ rawLine: outcome.rawLine, reason: outcome.reason }); } } Logger.log('Accepted records: ' + accepted.length + '; rejected lines: ' + rejected.length); for (const record of accepted) { Logger.log( record.summaryLine + ' | sha256=' + record.contentSha256.slice(0, 12) + ' | uuid=' + record.recordUuid, ); } for (const item of rejected) { Logger.log('Rejected: ' + item.reason + ' | input="' + cleanInputText(item.rawLine) + '"'); } } /** * Shows why String.prototype.trim() removes non-breaking spaces (\u00A0) while * leaving zero-width spaces (\u200B) attached to copied text. * * @returns {void} */ function logZeroWidthDemo_() { const pastedStatus = 'Active\u200b'; Logger.log( 'pastedStatus.trim() === "Active": ' + String(pastedStatus.trim() === 'Active') + ' (length=' + pastedStatus.trim().length + ')', ); Logger.log( 'cleanInputText(pastedStatus) === "Active": ' + String(cleanInputText(pastedStatus) === 'Active'), ); } /** * Removes zero-width characters, normalizes Unicode composition to NFC, trims * outer whitespace (including \u00A0), and collapses interior whitespace runs. * * @param {string} rawText * @returns {string} */ function cleanInputText(rawText) { if (typeof rawText !== 'string') { throw new Error('cleanInputText requires a string argument. Select inspectParsedIntakeText to run the sample.'); } return rawText .normalize('NFC') .replace(ZERO_WIDTH_CHARACTERS, '') .trim() .replace(/\s+/g, ' '); } /** * Parses one raw intake line into a validated record or an explicit rejection reason. * * @param {string} rawLine * @returns {TextParseOutcome} */ function parseIntakeTextLine(rawLine) { if (typeof rawLine !== 'string') { throw new Error('parseIntakeTextLine requires a string argument. Select inspectParsedIntakeText to run the sample.'); } const normalized = cleanInputText(rawLine); const match = INTAKE_TEXT_PATTERN.exec(normalized); if (!match || !match.groups) { return { ok: false, rawLine: rawLine, reason: 'Line does not match [REQ-dddd] dept: ... | owner: ... | title', }; } const requestId = match.groups.requestId; const department = match.groups.department.trim(); const ownerToken = match.groups.ownerToken; const title = match.groups.title.trim(); if (!EMAIL_TOKEN_PATTERN.test(ownerToken)) { return { ok: false, rawLine: rawLine, reason: 'Owner "' + ownerToken + '" is not a valid email token', }; } const ownerEmail = ownerToken.toLowerCase(); const referencedIds = extractReferencedRequestIds(title, requestId); const canonicalKey = [requestId, department, ownerEmail, title].join('|'); const contentSha256 = sha256Hex(canonicalKey); const recordUuid = Utilities.getUuid(); const refsSummary = referencedIds.length > 0 ? referencedIds.join(',') : 'none'; const summaryLine = requestId + ' | dept=' + department + ' | owner=' + ownerEmail + ' | refs=' + refsSummary + ' | title="' + title + '"'; return { ok: true, record: { requestId: requestId, department: department, ownerEmail: ownerEmail, title: title, referencedIds: referencedIds, contentSha256: contentSha256, recordUuid: recordUuid, summaryLine: summaryLine, }, }; } /** * Extracts unique REQ-dddd mentions from free text without mutating a shared lastIndex. * * @param {string} text * @param {string} ownRequestId * @returns {string[]} */ function extractReferencedRequestIds(text, ownRequestId) { if (typeof text !== 'string' || typeof ownRequestId !== 'string') { throw new Error( 'extractReferencedRequestIds requires (text, ownRequestId) string arguments. Select inspectParsedIntakeText to run the sample.', ); } /** @type {Set<string>} */ const uniqueIds = new Set(); for (const match of text.matchAll(REQUEST_CODE_GLOBAL_PATTERN)) { const candidate = match[0]; if (candidate !== ownRequestId) { uniqueIds.add(candidate); } } return Array.from(uniqueIds); } /** * Computes a lowercase 64-character hexadecimal SHA-256 digest for UTF-8 text. * Masks signed Java bytes (-128..127) with 0xff before hexadecimal formatting. * * @param {string} text * @returns {string} */ function sha256Hex(text) { if (typeof text !== 'string') { throw new Error('sha256Hex requires a string argument. Select inspectParsedIntakeText to run the sample.'); } const signedBytes = Utilities.computeDigest( Utilities.DigestAlgorithm.SHA_256, text, Utilities.Charset.UTF_8, ); return signedBytes.map((byte) => (byte & 0xff).toString(16).padStart(2, '0')).join(''); } /** * Escapes regular expression metacharacters so dynamic text matches literally. * * @param {string} literalText * @returns {string} */ function escapeRegExpLiteral(literalText) { if (typeof literalText !== 'string') { throw new Error('escapeRegExpLiteral requires a string argument. Select inspectParsedIntakeText to run the sample.'); } return literalText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }