Skip to content

Commit

Permalink
version 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
shimataro committed Apr 22, 2018
2 parents d5eca8a + 8fb1076 commit 55b9c95
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,19 @@ const adjusted = adjuster.adjustData(inputData, adjusters);

## Release notes

* 2018/04/22 *version 0.3.0*
* Bugfixes
* quoted-pair of email
* import error in `EmailAdjuster.es`
* Change Specifications
* limit the length of local/domain part of email

* 2018/04/21 *version 0.2.0*
* Bugfixes
* test error on npm@5
* Change Specifications
* enable to specify value to `allowEmpty()`
* support IPv6 domain for `EmailAdjuster`

* 2018/04/18 *version 0.1.0*
* First release.
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "adjuster",
"description": "validate and adjust input values",
"version": "0.2.0",
"version": "0.3.0",
"author": "shimataro",
"license": "MIT",
"repository": {
Expand Down
23 changes: 20 additions & 3 deletions src/libs/EmailAdjuster.es
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {CAUSE} from "./constants";
import AdjusterInterface from "./AdjusterInterface";
import AdjusterError from "./AdjusterError";
import StringAdjuster from "./StringAdjuster";

const MAX_LENGTH = 254;
const MAX_LENGTH_LOCAL = 64;
const MAX_LENGTH_DOMAIN = 255;
const MAX_LENGTH = MAX_LENGTH_LOCAL + 1 + MAX_LENGTH_DOMAIN; // local-part + "@" + domain-part

// https://tools.ietf.org/html/rfc5321
// https://tools.ietf.org/html/rfc5322
Expand All @@ -14,7 +17,7 @@ const REGEXP_CHARSET_IPV4 = "\\d";
const REGEXP_CHARSET_IPV6 = "[\\da-fA-F]";

const REGEXP_COMPONENT_DOT = `${REGEXP_CHARSET_DOT}+`;
const REGEXP_COMPONENT_QUOTED = `(${REGEXP_CHARSET_QUOTED}|\\\\|\\")+`;
const REGEXP_COMPONENT_QUOTED = `(${REGEXP_CHARSET_QUOTED}|\\\\[\\\\"])+`;
const REGEXP_COMPONENT_TLD = `${REGEXP_CHARSET_TLD}+`;
const REGEXP_COMPONENT_SLD = `${REGEXP_CHARSET_SLD}+`;
const REGEXP_COMPONENT_IPV4 = `${REGEXP_CHARSET_IPV4}{1,3}`;
Expand Down Expand Up @@ -95,7 +98,21 @@ export default class EmailAdjuster extends AdjusterInterface
{
try
{
return this._objAdjuster.adjust(value);
const adjusted = this._objAdjuster.adjust(value);

const atPosition = adjusted.lastIndexOf("@");
if(atPosition > MAX_LENGTH_LOCAL)
{
// local-part length error
throw new AdjusterError(CAUSE.MAX_LENGTH, value);
}
if(adjusted.length - atPosition - 1 > MAX_LENGTH_DOMAIN)
{
// domain-part length error
throw new AdjusterError(CAUSE.MAX_LENGTH, value);
}

return adjusted;
}
catch(err)
{
Expand Down
38 changes: 37 additions & 1 deletion test/libs/EmailAdjuster.test.es
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import EmailAdjuster from "libs/EmailAdjuster";
describe("default", testDefault);
describe("empty", testEmpty);
describe("allowEmpty", testAllowEmpty);
describe("maxLength", testMaxLength);
describe("pattern", testPattern);
describe("email", testEmail);
}
Expand Down Expand Up @@ -52,6 +53,39 @@ function testAllowEmpty()
});
}

function testMaxLength()
{
const objEmailAdjuster = new EmailAdjuster();
it("should be OK", () =>
{
const values = [
"1234567890123456789012345678901234567890123456789012345678901234@example.com",
"user@12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901.com",
"1234567890123456789012345678901234567890123456789012345678901234@12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901.com",
];
for(const value of values)
{
expect(objEmailAdjuster.adjust(value)).toEqual(value);
}
});
it("should cause error(s)", () =>
{
const values = [
"12345678901234567890123456789012345678901234567890123456789012345@example.com",
"user@123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012.com",
"12345678901234567890123456789012345678901234567890123456789012345@12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901.com",
"1234567890123456789012345678901234567890123456789012345678901234@123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012.com",
];
for(const value of values)
{
expect(() =>
{
objEmailAdjuster.adjust(value);
}).toThrow(CAUSE.MAX_LENGTH);
}
});
}

function testPattern()
{
const objEmailAdjuster = new EmailAdjuster().pattern(/^\w+@([\w-]+\.)+\w+$/);
Expand Down Expand Up @@ -81,9 +115,11 @@ function testEmail()
"user+mailbox/[email protected]",
"!#$%&'*+-/=?^_`.{|}[email protected]",

// quoted-pair
// quoted-string
"\"Fred\\\"Bloggs\"@example.com",
"\"Joe.\\\\Blow\"@example.com",
"\"...\"@example.com",
"\"(@_@) >_< ...(o;_;)o\"@example.com",

// domain
"[email protected]",
Expand Down

0 comments on commit 55b9c95

Please sign in to comment.