Skip to content

Instantly share code, notes, and snippets.

@Lewwwk
Last active November 13, 2015 19:16
Show Gist options
  • Save Lewwwk/feca922f5c68175c4bf4 to your computer and use it in GitHub Desktop.
Save Lewwwk/feca922f5c68175c4bf4 to your computer and use it in GitHub Desktop.
parseEmail - simple superscript plugin to parse and return an email address from an input string
var debug = require("debug")("parseEmail Plugin");
var emailRegex = require("email-regex");
exports.parseEmail = function(star, cb) {
// function to parse either a passed string or the entire message
var input;
if (typeof(star) === 'function') {
cb = star;
star = null;
// get 'original' message so that we don't lose .tld due to ss message processing
debug("No input provided. Processing original message: %s", this.message.original);
input = this.message.original;
} else {
debug("Using input provided: %s", star);
input = star;
}
var result = input.match(emailRegex());
if (!result) {
debug("No email found.");
cb(null, "");
} else {
debug("Found email: %s. Adding to message props", result);
this.message.props["email"] = result;
cb(null, "");
}
};
@silentrob
Copy link

That looks good. I'm courious how you would use this in the actuall reply it seems a little restrictive.

@Lewwwk
Copy link
Author

Lewwwk commented Nov 13, 2015

I was using that as a generic helper that I can call whenever I need to grab an email. What I'm now trying to do is create a saveEmail plugin that calls this when I actually want to save an email. Definitely open to suggestions on better ways!

@Lewwwk
Copy link
Author

Lewwwk commented Nov 13, 2015

updated to handle either passed input (for if/when we can pass a wildcard) or the original message. Now adds the email to the message props instead of returning it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment