@util.js/emails
Version:
JavaScript utility methods for emails
179 lines (168 loc) • 4.58 kB
JavaScript
;
/**
* Wrapper of email headers that makes header lookup easy.
*
* This class makes it so that the case of header names is not important.
* For example, Gmail might use "In-Reply-To" whereas iCloud might use "In-reply-to".
*
* @public
* @class
*/
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
var _weakMap = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/weak-map"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
var EmailHeaders = /*#__PURE__*/function () {
function EmailHeaders(options) {
(0, _classCallCheck2["default"])(this, EmailHeaders);
dependencies.set(this, options);
}
/**
* Returns the value of the Delivered-To header
*
* @return {undefined|string} The value of the Date header as a string or undefined
* @public
* @instance
* @function
*/
(0, _createClass2["default"])(EmailHeaders, [{
key: "deliveredTo",
value: function deliveredTo() {
return this.get("delivered-to");
}
/**
* Returns the value of the Date header
*
* @return {undefined|string} The value of the Date header as a string or undefined
* @public
* @instance
* @function
*/
}, {
key: "date",
value: function date() {
return this.get("date");
}
/**
* Returns the value of the From header
*
* @return {undefined|string} The value of the From header as a string or undefined
* @public
* @instance
* @function
*/
}, {
key: "from",
value: function from() {
return this.get("from");
}
/**
* Returns the value of the given headerName
*
* @param {string} headerName The header to look up a value for
* @return {undefined|string} The value of the given header name as a string or undefined
* @throws {TypeError} If headerName does not have a toLowerCase function
* @public
* @instance
* @function
*/
}, {
key: "get",
value: function get(headerName) {
return lowercaseHeaderMap(this)[headerName.toLowerCase()];
}
/**
* Returns the value of the In-Reply-To header
*
* @return {undefined|string} The value of the In-Reply-To header as a string or undefined
* @public
* @instance
* @function
*/
}, {
key: "inReplyTo",
value: function inReplyTo() {
return this.get("in-reply-to");
}
/**
* Returns the value of the Message-Id header
*
* @return {undefined|string} The value of the Message-Id header as a string or undefined
* @public
* @instance
* @function
*/
}, {
key: "messageId",
value: function messageId() {
return this.get("message-id");
}
/**
* Returns the value of the References header
*
* @return {undefined|string} The value of the References header as a string or undefined
* @public
* @instance
* @function
*/
}, {
key: "references",
value: function references() {
return this.get("references");
}
/**
* Returns the value of the Subject header
*
* @return {undefined|string} The value of the Subject header as a string or undefined
* @public
* @instance
* @function
*/
}, {
key: "subject",
value: function subject() {
return this.get("subject");
}
/**
* Returns the value of the To header
*
* @return {undefined|string} The value of the To header as a string or undefined
* @public
* @instance
* @function
*/
}, {
key: "to",
value: function to() {
return this.get("to");
}
/**
* Returns a string listing the email-header names and values of this object
*
* @return {string} A string representation of this object
* @public
* @instance
* @function
*/
}, {
key: "toString",
value: function toString() {
return "EmailHeaders " + json().stringify(headerMap(this), null, " ");
}
}]);
return EmailHeaders;
}();
module.exports = EmailHeaders;
var dependencies = new _weakMap["default"]();
function get(thiz, dependency) {
return dependencies.get(thiz)[dependency];
}
function headerMap(thiz) {
return get(thiz, "headerMap");
}
function json() {
return JSON;
}
function lowercaseHeaderMap(thiz) {
return get(thiz, "lowercaseHeaderMap");
}