import XRegExp from 'xregexp' // Parse the log data const parseLog = (data: string) => { const patterns = [ XRegExp('^NAME:\\s+"(?.*)",\\s+DESCR:\\s+"(?.*)"'), XRegExp('^PID:\\s+(?[\\S+]+|.*),.*VID:\\s+(?.*),.*SN:\\s+(?[\\w+\\d+]*)'), XRegExp('^PID:\\s+,.*VID:\\s+(?.*),.*SN:\\s+(?[\\w+\\d+]*)'), XRegExp('^PID:\\s+(?[\\S+]+|.*),.*VID:\\s+(?.*),.*SN:'), ] const lines = data.split('\n') const records = [] let currentRecord: any = { name: '', descr: '', pid: '', vid: '', sn: '' } for (const line of lines) { for (const pattern of patterns) { const match = XRegExp.exec(line, pattern) if (match) { const item = match?.groups // Update current record with matched fields Object.keys(currentRecord).forEach((key) => { if (item && item[key] !== undefined) { currentRecord[key] = item[key] } }) // If "pid", "vid", or "sn" are matched, push a completed record if (currentRecord.pid || currentRecord.vid || currentRecord.sn) { records.push({ ...currentRecord }) currentRecord = { name: '', descr: '', pid: '', vid: '', sn: '' } // Reset for the next record } break // Stop checking other patterns for this line } } } return records } export default parseLog