import XRegExp from 'xregexp' // Patterns for each field // Parser function const parseLog = (data: string) => { const patterns = [ XRegExp('^Index\\s+\\d+\\s+Feature:\\s+(?\\S+)'), XRegExp('^Period\\s+left:\\s+(?.+)'), XRegExp('^Period\\s+Used:\\s+(?.+)'), XRegExp('^License\\s+Type:\\s+(?.+)'), XRegExp('^License\\s+State:\\s+(?.+)'), XRegExp('^License\\s+Count:\\s+(?.+)'), XRegExp('^License\\s+Priority:\\s+(?.+)'), ] const lines = data.split('\n') const records = [] let currentRecord: any = null for (const line of lines) { if (XRegExp.test(line, XRegExp('^Index\\s+\\d+\\s+Feature:'))) { // Start a new record if (currentRecord) { records.push(currentRecord) } currentRecord = { FEATURE: '', PERIOD_LEFT: '', PERIOD_USED: '', LICENSE_TYPE: '', LICENSE_STATE: '', LICENSE_COUNT: '', LICENSE_PRIORITY: '', } } if (currentRecord) { for (const pattern of patterns) { const match = XRegExp.exec(line, pattern) if (match) { const item = match?.groups || {} Object.keys(item).forEach((key) => { if (item && item[key] !== undefined) { currentRecord[key] = item[key] } }) break // Stop processing this line once a pattern matches } } } } // Push the last record if it exists if (currentRecord) { records.push(currentRecord) } return records } export default parseLog