Created
September 25, 2018 21:47
-
-
Save Exellin/4cbf9c78742d0ab3d0aede68d296b63a to your computer and use it in GitHub Desktop.
ClickHook Zaper Mail Parser
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 transformKey = (key) => { | |
if (!key) return; | |
return key | |
.trim() | |
.replace(new RegExp("-", 'g'), "") | |
.replace(new RegExp(" ", 'g'), "") | |
.toLowerCase(); | |
} | |
let lines = inputData.body.split("\n"); | |
let info = { | |
custom_fields: {} | |
}; | |
lines.forEach(line => { | |
let parts = line.split(":"); | |
if (parts.length <= 1) return; | |
let key = parts[0]; | |
let value = parts[1]; | |
key = transformKey(key); | |
let contactKeys = ['name', 'email', 'phone']; | |
if (contactKeys.indexOf(key) > -1) { | |
if (info[key]) return; | |
info[key] = value.trim(); | |
} else { | |
if (info.custom_fields[key]) return; | |
info.custom_fields[key] = value.trim(); | |
} | |
}) | |
return info; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment