Skip to content

Instantly share code, notes, and snippets.

@Exellin
Created September 25, 2018 21:47
Show Gist options
  • Save Exellin/4cbf9c78742d0ab3d0aede68d296b63a to your computer and use it in GitHub Desktop.
Save Exellin/4cbf9c78742d0ab3d0aede68d296b63a to your computer and use it in GitHub Desktop.
ClickHook Zaper Mail Parser
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