Combine the two Javascripts below [code] [code]
- User
Combine the two Javascripts below
function parseInputSections(inputString, existingParsedData = {}) { // Define a regular expression pattern to match each section with headings and key/value pairs const sectionPattern = /##\s+(.*?)\s+([\s\S]+?)(?=\n##|\n\n##|$)/g; // Define a regular expression pattern to match key/value pairs const keyValuePattern = /^(.+?):\s*(.+)$/gm; // Object to store the parsed information, initialized to existingParsedData const parsedSections = existingParsedData; // Object to store the inboxes for roles and team const roleInboxes = existingParsedData.roleInboxes || {}; const teamInbox = existingParsedData.teamInbox || []; // Match sections and iterate through them let sectionMatch; while ((sectionMatch = sectionPattern.exec(inputString)) !== null) { // Retrieve the section name and content const sectionName = sectionMatch[1].trim(); const sectionContent = sectionMatch[2].trim(); // Match key/value pairs within the section's content let keyValueMatch; // Create an object to store the key/value pairs const sectionData = {}; while ((keyValueMatch = keyValuePattern.exec(sectionContent)) !== null) { // Store the key/value pairs in the object using the first captured group as key and second as value const key = keyValueMatch[1].trim(); const value = keyValueMatch[2].trim(); sectionData[key] = value; } // Special handling for 'Metadata' section if (sectionName === 'Metadata') { // ... (previous code for handling Metadata remains unchanged) } else if (sectionName === 'message') { if (sectionData.to) { const recipients = sectionData.to.split(',').map(role => role.trim()); // Check if the message is addressed to the 'team' if (recipients.includes('team')) { // Initialize 'viewedBy' array to keep track of who has viewed the message sectionData.viewedBy = sectionData.viewedBy || []; teamInbox.push(sectionData); } else { // Sort the message into inboxes based on 'to' property recipients.forEach(role => { if (!roleInboxes[role]) { roleInboxes[role] = []; } roleInboxes[role].push(sectionData); }); } } } else { // ... (existing code for handling other sections remains unchanged) } } // Add the role inboxes and team inbox to the parsedSections object parsedSections.roleInboxes = roleInboxes; parsedSections.teamInbox = teamInbox; return parsedSections; } // Function to view messages by a role and update the view log function viewMessagesByRole(parsedData, roleName) { // Filter out messages that have already been viewed by the role const unseenMessages = parsedData.teamInbox.filter(message => !message.viewedBy.includes(roleName)); // Update the view log for the messages that are being returned unseenMessages.forEach(message => { if (!message.viewedBy.includes(roleName)) { message.viewedBy.push(roleName); } }); // Return the unseen messages return unseenMessages; } // Example usage: const inputString = ` ## Metadata project: Tesla Market Research Report role: Project Manager task: Begin Project details: You have received the project details. Begin by taking the first step in this project. ## message to: team from: Project Manager message: This is a general announcement to the entire team. ## message to: Research Analyst from: Project Manager message: Please ensure all research is aligned with our objectives and report any significant findings to the team immediately. `; // Parse the input string without any existing parsed data const parsedData = parseInputSections(inputString); // Role viewing the messages const role = 'Project Manager'; // View unseen team messages for the 'Project Manager' role and update the view log const unseenTeamMessages = viewMessagesByRole(parsedData, role); console.log(unseenTeamMessages); // This will log the unseen team messages for 'Project Manager' and update the view log - Assistant