@util.js/emails 0.41.6 → 0.41.8

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.
@@ -0,0 +1,178 @@
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
+ var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
13
+ var _weakMap = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/weak-map"));
14
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
15
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
16
+ var EmailHeaders = /*#__PURE__*/function () {
17
+ function EmailHeaders(options) {
18
+ (0, _classCallCheck2["default"])(this, EmailHeaders);
19
+ dependencies.set(this, options);
20
+ }
21
+
22
+ /**
23
+ * Returns the value of the Delivered-To header
24
+ *
25
+ * @return {undefined|string} The value of the Date header as a string or undefined
26
+ * @public
27
+ * @instance
28
+ * @function
29
+ */
30
+ return (0, _createClass2["default"])(EmailHeaders, [{
31
+ key: "deliveredTo",
32
+ value: function deliveredTo() {
33
+ return this.get("delivered-to");
34
+ }
35
+
36
+ /**
37
+ * Returns the value of the Date header
38
+ *
39
+ * @return {undefined|string} The value of the Date header as a string or undefined
40
+ * @public
41
+ * @instance
42
+ * @function
43
+ */
44
+ }, {
45
+ key: "date",
46
+ value: function date() {
47
+ return this.get("date");
48
+ }
49
+
50
+ /**
51
+ * Returns the value of the From header
52
+ *
53
+ * @return {undefined|string} The value of the From header as a string or undefined
54
+ * @public
55
+ * @instance
56
+ * @function
57
+ */
58
+ }, {
59
+ key: "from",
60
+ value: function from() {
61
+ return this.get("from");
62
+ }
63
+
64
+ /**
65
+ * Returns the value of the given headerName
66
+ *
67
+ * @param {string} headerName The header to look up a value for
68
+ * @return {undefined|string} The value of the given header name as a string or undefined
69
+ * @throws {TypeError} If headerName does not have a toLowerCase function
70
+ * @public
71
+ * @instance
72
+ * @function
73
+ */
74
+ }, {
75
+ key: "get",
76
+ value: function get(headerName) {
77
+ return lowercaseHeaderMap(this)[headerName.toLowerCase()];
78
+ }
79
+
80
+ /**
81
+ * Returns the value of the In-Reply-To header
82
+ *
83
+ * @return {undefined|string} The value of the In-Reply-To header as a string or undefined
84
+ * @public
85
+ * @instance
86
+ * @function
87
+ */
88
+ }, {
89
+ key: "inReplyTo",
90
+ value: function inReplyTo() {
91
+ return this.get("in-reply-to");
92
+ }
93
+
94
+ /**
95
+ * Returns the value of the Message-Id header
96
+ *
97
+ * @return {undefined|string} The value of the Message-Id header as a string or undefined
98
+ * @public
99
+ * @instance
100
+ * @function
101
+ */
102
+ }, {
103
+ key: "messageId",
104
+ value: function messageId() {
105
+ return this.get("message-id");
106
+ }
107
+
108
+ /**
109
+ * Returns the value of the References header
110
+ *
111
+ * @return {undefined|string} The value of the References header as a string or undefined
112
+ * @public
113
+ * @instance
114
+ * @function
115
+ */
116
+ }, {
117
+ key: "references",
118
+ value: function references() {
119
+ return this.get("references");
120
+ }
121
+
122
+ /**
123
+ * Returns the value of the Subject header
124
+ *
125
+ * @return {undefined|string} The value of the Subject header as a string or undefined
126
+ * @public
127
+ * @instance
128
+ * @function
129
+ */
130
+ }, {
131
+ key: "subject",
132
+ value: function subject() {
133
+ return this.get("subject");
134
+ }
135
+
136
+ /**
137
+ * Returns the value of the To header
138
+ *
139
+ * @return {undefined|string} The value of the To header as a string or undefined
140
+ * @public
141
+ * @instance
142
+ * @function
143
+ */
144
+ }, {
145
+ key: "to",
146
+ value: function to() {
147
+ return this.get("to");
148
+ }
149
+
150
+ /**
151
+ * Returns a string listing the email-header names and values of this object
152
+ *
153
+ * @return {string} A string representation of this object
154
+ * @public
155
+ * @instance
156
+ * @function
157
+ */
158
+ }, {
159
+ key: "toString",
160
+ value: function toString() {
161
+ return "EmailHeaders " + json().stringify(headerMap(this), null, " ");
162
+ }
163
+ }]);
164
+ }();
165
+ module.exports = EmailHeaders;
166
+ var dependencies = new _weakMap["default"]();
167
+ function get(thiz, dependency) {
168
+ return dependencies.get(thiz)[dependency];
169
+ }
170
+ function headerMap(thiz) {
171
+ return get(thiz, "headerMap");
172
+ }
173
+ function json() {
174
+ return JSON;
175
+ }
176
+ function lowercaseHeaderMap(thiz) {
177
+ return get(thiz, "lowercaseHeaderMap");
178
+ }
package/dist/Emails.js ADDED
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * JavaScript utility methods for emails
5
+ * @public
6
+ * @class
7
+ */
8
+ var _sliceInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/slice");
9
+ var _Array$from = require("@babel/runtime-corejs3/core-js-stable/array/from");
10
+ var _Symbol = require("@babel/runtime-corejs3/core-js-stable/symbol");
11
+ var _getIteratorMethod = require("@babel/runtime-corejs3/core-js/get-iterator-method");
12
+ var _Array$isArray = require("@babel/runtime-corejs3/core-js-stable/array/is-array");
13
+ var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
14
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
15
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
16
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof _Symbol && _getIteratorMethod(r) || r["@@iterator"]; if (!t) { if (_Array$isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
17
+ function _unsupportedIterableToArray(r, a) { if (r) { var _context; if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = _sliceInstanceProperty(_context = {}.toString.call(r)).call(_context, 8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? _Array$from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
18
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
19
+ var Emails = /*#__PURE__*/function () {
20
+ function Emails() {
21
+ (0, _classCallCheck2["default"])(this, Emails);
22
+ }
23
+ return (0, _createClass2["default"])(Emails, [{
24
+ key: "isValidEmail",
25
+ value:
26
+ /**
27
+ * Returns whether the given string is a valid email.
28
+ *
29
+ * 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}.
30
+ *
31
+ * @param {string} string The string to test
32
+ * @return {boolean} true if the given string is a valid email or false otherwise
33
+ * @public
34
+ * @instance
35
+ * @function
36
+ */
37
+ function isValidEmail(string) {
38
+ return /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(string);
39
+ }
40
+
41
+ /**
42
+ * Wraps the given nameValueArrayOfHeaders inside an object that makes email-header lookup easy.
43
+ *
44
+ * 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.
45
+ *
46
+ * [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:
47
+ * <pre>
48
+ * [ { name: 'Delivered-To', value: 'c@creemama.com' },
49
+ * ...
50
+ * { name: 'Date', value: 'Tue, 10 Jul 2018 10:18:52 -0700' },
51
+ * ...
52
+ * { name: 'To', value: 'Chris Topher <c@creemama.com>' } ]
53
+ * </pre>
54
+ *
55
+ * @param {Array} nameValueArrayOfHeaders An array of name-value objects containing email headers
56
+ * @return {EmailHeaders} A new {@link EmailHeaders} instance
57
+ * @throws {TypeError} If nameValueArrayOfHeaders is not an array-like object containing name-value objects
58
+ * @public
59
+ * @instance
60
+ * @function
61
+ */
62
+ }, {
63
+ key: "wrapHeaders",
64
+ value: function wrapHeaders(nameValueArrayOfHeaders) {
65
+ var headerMap = {};
66
+ var lowercaseHeaderMap = {};
67
+ var _iterator = _createForOfIteratorHelper(nameValueArrayOfHeaders),
68
+ _step;
69
+ try {
70
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
71
+ var header = _step.value;
72
+ headerMap[header.name] = header.value;
73
+ lowercaseHeaderMap[header.name.toLowerCase()] = header.value;
74
+ }
75
+ } catch (err) {
76
+ _iterator.e(err);
77
+ } finally {
78
+ _iterator.f();
79
+ }
80
+ var EmailHeaders = requireEmailHeaders();
81
+ return new EmailHeaders({
82
+ headerMap: headerMap,
83
+ lowercaseHeaderMap: lowercaseHeaderMap
84
+ });
85
+ }
86
+ }]);
87
+ }();
88
+ module.exports = Emails;
89
+ var dependencies = {};
90
+ function requireEmailHeaders() {
91
+ return dependencies.EmailHeaders || (dependencies.EmailHeaders = require("./EmailHeaders"));
92
+ }
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.6"
35
+ "version": "0.41.8"
32
36
  }
@@ -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
- }
@@ -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
- });