@util.js/emails 0.41.5 → 0.41.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -1
- package/lib/EmailHeaders.js +0 -155
- package/lib/Emails.js +0 -71
- package/test/EmailsTest.js +0 -73
package/package.json
CHANGED
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
"@babel/runtime-corejs3 is required by files in the dist directory to ",
|
|
9
9
|
"work in https://npm.runkit.com."
|
|
10
10
|
],
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"index.js"
|
|
14
|
+
],
|
|
11
15
|
"keywords": [
|
|
12
16
|
"email",
|
|
13
17
|
"emails",
|
|
@@ -28,5 +32,5 @@
|
|
|
28
32
|
"type": "git",
|
|
29
33
|
"url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-emails"
|
|
30
34
|
},
|
|
31
|
-
"version": "0.41.
|
|
35
|
+
"version": "0.41.7"
|
|
32
36
|
}
|
package/lib/EmailHeaders.js
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Wrapper of email headers that makes header lookup easy.
|
|
5
|
-
*
|
|
6
|
-
* This class makes it so that the case of header names is not important.
|
|
7
|
-
* For example, Gmail might use "In-Reply-To" whereas iCloud might use "In-reply-to".
|
|
8
|
-
*
|
|
9
|
-
* @public
|
|
10
|
-
* @class
|
|
11
|
-
*/
|
|
12
|
-
class EmailHeaders {
|
|
13
|
-
constructor(options) {
|
|
14
|
-
dependencies.set(this, options);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Returns the value of the Delivered-To header
|
|
19
|
-
*
|
|
20
|
-
* @return {undefined|string} The value of the Date header as a string or undefined
|
|
21
|
-
* @public
|
|
22
|
-
* @instance
|
|
23
|
-
* @function
|
|
24
|
-
*/
|
|
25
|
-
deliveredTo() {
|
|
26
|
-
return this.get("delivered-to");
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Returns the value of the Date header
|
|
31
|
-
*
|
|
32
|
-
* @return {undefined|string} The value of the Date header as a string or undefined
|
|
33
|
-
* @public
|
|
34
|
-
* @instance
|
|
35
|
-
* @function
|
|
36
|
-
*/
|
|
37
|
-
date() {
|
|
38
|
-
return this.get("date");
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Returns the value of the From header
|
|
43
|
-
*
|
|
44
|
-
* @return {undefined|string} The value of the From header as a string or undefined
|
|
45
|
-
* @public
|
|
46
|
-
* @instance
|
|
47
|
-
* @function
|
|
48
|
-
*/
|
|
49
|
-
from() {
|
|
50
|
-
return this.get("from");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Returns the value of the given headerName
|
|
55
|
-
*
|
|
56
|
-
* @param {string} headerName The header to look up a value for
|
|
57
|
-
* @return {undefined|string} The value of the given header name as a string or undefined
|
|
58
|
-
* @throws {TypeError} If headerName does not have a toLowerCase function
|
|
59
|
-
* @public
|
|
60
|
-
* @instance
|
|
61
|
-
* @function
|
|
62
|
-
*/
|
|
63
|
-
get(headerName) {
|
|
64
|
-
return lowercaseHeaderMap(this)[headerName.toLowerCase()];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Returns the value of the In-Reply-To header
|
|
69
|
-
*
|
|
70
|
-
* @return {undefined|string} The value of the In-Reply-To header as a string or undefined
|
|
71
|
-
* @public
|
|
72
|
-
* @instance
|
|
73
|
-
* @function
|
|
74
|
-
*/
|
|
75
|
-
inReplyTo() {
|
|
76
|
-
return this.get("in-reply-to");
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Returns the value of the Message-Id header
|
|
81
|
-
*
|
|
82
|
-
* @return {undefined|string} The value of the Message-Id header as a string or undefined
|
|
83
|
-
* @public
|
|
84
|
-
* @instance
|
|
85
|
-
* @function
|
|
86
|
-
*/
|
|
87
|
-
messageId() {
|
|
88
|
-
return this.get("message-id");
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Returns the value of the References header
|
|
93
|
-
*
|
|
94
|
-
* @return {undefined|string} The value of the References header as a string or undefined
|
|
95
|
-
* @public
|
|
96
|
-
* @instance
|
|
97
|
-
* @function
|
|
98
|
-
*/
|
|
99
|
-
references() {
|
|
100
|
-
return this.get("references");
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Returns the value of the Subject header
|
|
105
|
-
*
|
|
106
|
-
* @return {undefined|string} The value of the Subject header as a string or undefined
|
|
107
|
-
* @public
|
|
108
|
-
* @instance
|
|
109
|
-
* @function
|
|
110
|
-
*/
|
|
111
|
-
subject() {
|
|
112
|
-
return this.get("subject");
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Returns the value of the To header
|
|
117
|
-
*
|
|
118
|
-
* @return {undefined|string} The value of the To header as a string or undefined
|
|
119
|
-
* @public
|
|
120
|
-
* @instance
|
|
121
|
-
* @function
|
|
122
|
-
*/
|
|
123
|
-
to() {
|
|
124
|
-
return this.get("to");
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Returns a string listing the email-header names and values of this object
|
|
129
|
-
*
|
|
130
|
-
* @return {string} A string representation of this object
|
|
131
|
-
* @public
|
|
132
|
-
* @instance
|
|
133
|
-
* @function
|
|
134
|
-
*/
|
|
135
|
-
toString() {
|
|
136
|
-
return "EmailHeaders " + json().stringify(headerMap(this), null, " ");
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
module.exports = EmailHeaders;
|
|
141
|
-
|
|
142
|
-
const dependencies = new WeakMap();
|
|
143
|
-
function get(thiz, dependency) {
|
|
144
|
-
return dependencies.get(thiz)[dependency];
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function headerMap(thiz) {
|
|
148
|
-
return get(thiz, "headerMap");
|
|
149
|
-
}
|
|
150
|
-
function json() {
|
|
151
|
-
return JSON;
|
|
152
|
-
}
|
|
153
|
-
function lowercaseHeaderMap(thiz) {
|
|
154
|
-
return get(thiz, "lowercaseHeaderMap");
|
|
155
|
-
}
|
package/lib/Emails.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* JavaScript utility methods for emails
|
|
5
|
-
* @public
|
|
6
|
-
* @class
|
|
7
|
-
*/
|
|
8
|
-
class Emails {
|
|
9
|
-
/**
|
|
10
|
-
* Returns whether the given string is a valid email.
|
|
11
|
-
*
|
|
12
|
-
* The regular expression used to validate strings comes from a [W3C article]{@link https://www.w3.org/TR/2012/WD-html-markup-20120320/input.email.html}.
|
|
13
|
-
*
|
|
14
|
-
* @param {string} string The string to test
|
|
15
|
-
* @return {boolean} true if the given string is a valid email or false otherwise
|
|
16
|
-
* @public
|
|
17
|
-
* @instance
|
|
18
|
-
* @function
|
|
19
|
-
*/
|
|
20
|
-
isValidEmail(string) {
|
|
21
|
-
return /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(
|
|
22
|
-
string,
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Wraps the given nameValueArrayOfHeaders inside an object that makes email-header lookup easy.
|
|
28
|
-
*
|
|
29
|
-
* When using the returned object, you no longer have to worry about the case (e.g., "In-Reply-To" or "In-reply-to") of email headers.
|
|
30
|
-
*
|
|
31
|
-
* [googleapis]{@link https://www.npmjs.com/package/googleapis}'s interface to Gmail returns email headers as an array of name-value objects. The following is a sample:
|
|
32
|
-
* <pre>
|
|
33
|
-
* [ { name: 'Delivered-To', value: 'c@creemama.com' },
|
|
34
|
-
* ...
|
|
35
|
-
* { name: 'Date', value: 'Tue, 10 Jul 2018 10:18:52 -0700' },
|
|
36
|
-
* ...
|
|
37
|
-
* { name: 'To', value: 'Chris Topher <c@creemama.com>' } ]
|
|
38
|
-
* </pre>
|
|
39
|
-
*
|
|
40
|
-
* @param {Array} nameValueArrayOfHeaders An array of name-value objects containing email headers
|
|
41
|
-
* @return {EmailHeaders} A new {@link EmailHeaders} instance
|
|
42
|
-
* @throws {TypeError} If nameValueArrayOfHeaders is not an array-like object containing name-value objects
|
|
43
|
-
* @public
|
|
44
|
-
* @instance
|
|
45
|
-
* @function
|
|
46
|
-
*/
|
|
47
|
-
wrapHeaders(nameValueArrayOfHeaders) {
|
|
48
|
-
const headerMap = {};
|
|
49
|
-
const lowercaseHeaderMap = {};
|
|
50
|
-
for (const header of nameValueArrayOfHeaders) {
|
|
51
|
-
headerMap[header.name] = header.value;
|
|
52
|
-
lowercaseHeaderMap[header.name.toLowerCase()] = header.value;
|
|
53
|
-
}
|
|
54
|
-
const EmailHeaders = requireEmailHeaders();
|
|
55
|
-
return new EmailHeaders({
|
|
56
|
-
headerMap,
|
|
57
|
-
lowercaseHeaderMap,
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
module.exports = Emails;
|
|
63
|
-
|
|
64
|
-
const dependencies = {};
|
|
65
|
-
|
|
66
|
-
function requireEmailHeaders() {
|
|
67
|
-
return (
|
|
68
|
-
dependencies.EmailHeaders ||
|
|
69
|
-
(dependencies.EmailHeaders = require("./EmailHeaders"))
|
|
70
|
-
);
|
|
71
|
-
}
|
package/test/EmailsTest.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const emails = require("..");
|
|
4
|
-
const { expect } = require("chai");
|
|
5
|
-
|
|
6
|
-
describe("Emails", () => {
|
|
7
|
-
describe("#isValidEmail(string)", () => {
|
|
8
|
-
it("should validate foo-bar.baz@example.com", () => {
|
|
9
|
-
expect(emails.isValidEmail("foo-bar.baz@example.com")).to.be.true;
|
|
10
|
-
});
|
|
11
|
-
it("should validate a@a", () => {
|
|
12
|
-
expect(emails.isValidEmail("a@a")).to.be.true;
|
|
13
|
-
});
|
|
14
|
-
it("should invalidate foo-bar.bazexample.com", () => {
|
|
15
|
-
expect(emails.isValidEmail("foo-bar.bazexample.com")).to.be.false;
|
|
16
|
-
});
|
|
17
|
-
it("should invalidate a@", () => {
|
|
18
|
-
expect(emails.isValidEmail("a@")).to.be.false;
|
|
19
|
-
});
|
|
20
|
-
it("should invalidate @b", () => {
|
|
21
|
-
expect(emails.isValidEmail("a@")).to.be.false;
|
|
22
|
-
});
|
|
23
|
-
it("should invalidate null", () => {
|
|
24
|
-
expect(emails.isValidEmail(null)).to.be.false;
|
|
25
|
-
});
|
|
26
|
-
it("should validate the array ['foo-bar.baz@example.com']", () => {
|
|
27
|
-
expect(emails.isValidEmail(["foo-bar.baz@example.com"])).to.be.true;
|
|
28
|
-
});
|
|
29
|
-
it("should invalidate the array ['foo-bar.baz@example.com', 'a@a']", () => {
|
|
30
|
-
expect(emails.isValidEmail(["foo-bar.baz@example.com", "a@a"])).to.be
|
|
31
|
-
.false;
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
describe("#wrapHeaders(nameValueArrayOfHeaders)", () => {
|
|
36
|
-
it("should return a valid EmailHeaders instance", () => {
|
|
37
|
-
const nameValueArrayOfHeaders = [
|
|
38
|
-
{ name: "Delivered-To", value: "c@creemama.com" },
|
|
39
|
-
{ name: "Date", value: "Tue, 10 Jul 2018 10:18:52 -0700" },
|
|
40
|
-
{ name: "To", value: "Chris Topher <c@creemama.com>" },
|
|
41
|
-
];
|
|
42
|
-
|
|
43
|
-
const headers = emails.wrapHeaders(nameValueArrayOfHeaders);
|
|
44
|
-
|
|
45
|
-
expect(headers.deliveredTo()).to.eql("c@creemama.com");
|
|
46
|
-
expect(headers.from()).to.be.undefined;
|
|
47
|
-
expect(headers.get("Date")).to.eql("Tue, 10 Jul 2018 10:18:52 -0700");
|
|
48
|
-
expect(headers.get("date")).to.eql("Tue, 10 Jul 2018 10:18:52 -0700");
|
|
49
|
-
expect(() => headers.get()).to.throw(TypeError);
|
|
50
|
-
expect(() => headers.get(null)).to.throw(TypeError);
|
|
51
|
-
expect(() => headers.get({})).to.throw(TypeError);
|
|
52
|
-
expect(() => headers.get(5)).to.throw(TypeError);
|
|
53
|
-
expect(headers.inReplyTo()).to.be.undefined;
|
|
54
|
-
expect(headers.messageId()).to.be.undefined;
|
|
55
|
-
expect(headers.references()).to.be.undefined;
|
|
56
|
-
expect(headers.subject()).to.be.undefined;
|
|
57
|
-
expect(headers.to()).to.eql("Chris Topher <c@creemama.com>");
|
|
58
|
-
expect(headers.toString()).to.eql(
|
|
59
|
-
"EmailHeaders {\n" +
|
|
60
|
-
' "Delivered-To": "c@creemama.com",\n' +
|
|
61
|
-
' "Date": "Tue, 10 Jul 2018 10:18:52 -0700",\n' +
|
|
62
|
-
' "To": "Chris Topher <c@creemama.com>"\n' +
|
|
63
|
-
"}",
|
|
64
|
-
);
|
|
65
|
-
});
|
|
66
|
-
it("should throw a TypeError if nameValueArrayOfHeaders is not an array-like object containing name-value objects", () => {
|
|
67
|
-
expect(() => emails.wrapHeaders()).to.throw(TypeError);
|
|
68
|
-
expect(() => emails.wrapHeaders(null)).to.throw(TypeError);
|
|
69
|
-
expect(() => emails.wrapHeaders(5)).to.throw(TypeError);
|
|
70
|
-
expect(() => emails.wrapHeaders([0, "a", "b"])).to.throw(TypeError);
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
});
|