Last active
May 6, 2021 14:08
-
-
Save tfb414/6be8ff6e719178cb001f059a981a6945 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const input = ` | |
timestamp="Wed Jun 19 09:35:36 PDT 2019" message="test 10" id=10 field_1="test 10" field_6="test 1" | |
timestamp="Wed Jun 19 09:35:37 PDT 2019" message="test 4" id=4 field_2="test 4" | |
timestamp="Wed Jun 19 09:35:38 PDT 2019" message="test 2" id=3 field_3="test 2" field_9="test 23" | |
timestamp="Wed Jun 19 09:35:39 PDT 2019" message="test 3" id=2 field_4="test 3" | |
timestamp="Wed Jun 19 09:35:40 PDT 2019" message="test 4" id=1 field_5="test 4" | |
timestamp="Wed Jun 19 09:35:37 PDT 2019" message="test 5" id=5 field_3="test 10" | |
timestamp="Wed Jun 19 09:35:40 PDT 2019" message="test 6" id=6 field_1="test 5" | |
` | |
const separateValues = (input) => { | |
const regex = /(["'])(?:(?=(\\?))\2.)*?\1/g | |
let arrayOfStringValues = input.match(regex); | |
const keyValuePairsArray = []; | |
arrayOfStringValues.forEach(string => { | |
if(nonStringValuesExist(string, input)) { | |
input = input.trim().split(' ') | |
keyValuePairsArray.push(input.shift()); | |
input = input.join(' ') | |
} | |
const lengthOfString = string.length; | |
const stringStartIndex = input.indexOf(string); | |
const keyValuePairs = input.split('').splice(0, stringStartIndex+lengthOfString).join(''); | |
input = input.split('') | |
input.splice(0, stringStartIndex+lengthOfString) | |
input = input.join('') | |
keyValuePairsArray.push(keyValuePairs); | |
}) | |
return keyValuePairsArray | |
} | |
const nonStringValuesExist = (string, input) => { | |
const lengthOfString = string.length; | |
const stringStartIndex = input.indexOf(string); | |
const testThis = input.slice(0, stringStartIndex+lengthOfString).trim(); | |
if(testThis.split('=').length > 2){ | |
return true; | |
} | |
} | |
const createObj = (keyValuePairs) => { | |
const obj = {}; | |
keyValuePairs.forEach(keyValuePair => { | |
const splitValues = keyValuePair.trim().split("="); | |
const key = splitValues.shift(); | |
const value = splitValues.join(''); | |
obj[key] = value; | |
}) | |
return obj; | |
} | |
const getKeys = (keyValuePairs) => { | |
const keys = []; | |
keyValuePairs.forEach(keyValuePair => { | |
Object.keys(keyValuePair).filter(kvp => !!kvp).forEach(key => { | |
if (keys.indexOf(key) === -1) { | |
keys.push(key); | |
} | |
}) | |
}) | |
return keys; | |
} | |
const compileCsv = (allKeys, keyValuePairs) => { | |
let finalString = allKeys.join(','); | |
keyValuePairs.sort((a, b) => a.id - b.id).forEach(keyValuePair => { | |
const formattedData = allKeys.map(k => { | |
if (keyValuePair[k]) { | |
return `${keyValuePair[k]}` | |
} | |
}) | |
finalString = finalString.concat('\n', formattedData) | |
}) | |
return finalString; | |
}; | |
const main = (input) => { | |
const splitInputsByNewLine = input.split('\n').filter(inp => !!inp); | |
const arrayOfKeyValuePairs = splitInputsByNewLine.map(splitInput => { | |
return createObj(separateValues(splitInput)); | |
}) | |
return compileCsv(getKeys(arrayOfKeyValuePairs), arrayOfKeyValuePairs) | |
} | |
console.log(main(input)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment