-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
444 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ Thumbs.db | |
/*.iml | ||
/nbproject/ | ||
/.project | ||
/*.code-workspace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,11 @@ npm install -S adjuster | |
import adjuster from "adjuster"; | ||
|
||
// should be OK | ||
adjuster.number().adjust("-123"); // === -123; | ||
adjuster.number().adjust(-123); // === -123; | ||
adjuster.number().in(1, 3, 5).adjust(1); // === 1 | ||
|
||
// should be adjusted | ||
adjuster.number().adjust("-123"); // === -123; | ||
adjuster.number().default(10).adjust(undefined); // === 10 | ||
adjuster.number().allowEmpty(123).adjust(""); // === 123 | ||
adjuster.number().minValue(1, true).adjust(0); // === 1 | ||
|
@@ -43,11 +44,12 @@ adjuster.number().maxValue(100).adjust(101); // throws AdjusterError; err.cause | |
import adjuster from "adjuster"; | ||
|
||
// should be OK | ||
adjuster.string().adjust(123); // === "123" | ||
adjuster.string().adjust("123"); // === "123" | ||
adjuster.string().allowEmpty("xyz").adjust(""); // === "xyz" | ||
adjuster.string().in("eat", "sleep", "play").adjust("sleep"); // === "sleep" | ||
|
||
// should be adjusted | ||
adjuster.string().adjust(123); // === "123" | ||
adjuster.string().default("xyz").adjust(undefined); // === "xyz" | ||
adjuster.string().maxLength(5, true).adjust("abcdefg"); // === "abcde" | ||
|
||
|
@@ -59,25 +61,59 @@ adjuster.string().minLength(5).adjust("a"); // throws AdjusterError; err.cause = | |
adjuster.string().maxLength(5).adjust("abcdefg"); // throws AdjusterError; err.cause === adjuster.CAUSE.MAX_LENGTH | ||
``` | ||
|
||
### IPv4 | ||
|
||
```javascript | ||
import adjuster from "adjuster"; | ||
|
||
// should be OK | ||
adjuster.ipv4().adjust("0.0.0.0"); // === "0.0.0.0" | ||
adjuster.ipv4().adjust("192.168.0.1"); // === "192.168.0.1" | ||
adjuster.ipv4().adjust("255.255.255.255"); // === "255.255.255.255" | ||
|
||
// should cause errors; err.cause === adjuster.CAUSE.IPV4 | ||
adjuster.ipv4().adjust("0.0.0."); | ||
adjuster.ipv4().adjust("0.0.0.0."); | ||
adjuster.ipv4().adjust("255.255.255.256"); | ||
``` | ||
|
||
### IPv6 | ||
|
||
```javascript | ||
import adjuster from "adjuster"; | ||
|
||
// should be OK | ||
adjuster.ipv6().adjust("0000:0000:0000:0000:0000:0000:0000:0000"); // === "0000:0000:0000:0000:0000:0000:0000:0000" | ||
adjuster.ipv6().adjust("::1"); // === "::1" | ||
adjuster.ipv6().adjust("::"); // === "::" | ||
adjuster.ipv6().adjust("1::1"); // === "1::1" | ||
adjuster.ipv6().adjust("::ffff:192.0.2.1"); // === "::ffff:192.0.2.1"; IPv4-mapped address | ||
|
||
// should cause errors; err.cause === adjuster.CAUSE.IPV6 | ||
adjuster.ipv6().adjust("0000"); | ||
adjuster.ipv6().adjust("ffff:"); | ||
adjuster.ipv6().adjust("0000:0000:0000:0000:0000:0000:0000:0000:"); | ||
``` | ||
|
||
|
||
```javascript | ||
import adjuster from "adjuster"; | ||
|
||
// should be OK | ||
adjuster.email().adjust("user+mailbox/[email protected]"); // dot-string | ||
adjuster.email().adjust("!#$%&'*+-/=?^_`.{|}[email protected]"); // dot-string | ||
adjuster.email().adjust("\"Fred\\\"Bloggs\"@example.com"); // quoted-string | ||
adjuster.email().adjust("\"Joe.\\\\Blow\"@example.com"); // quoted-string | ||
adjuster.email().adjust("[email protected]"); | ||
adjuster.email().adjust("[email protected]"); | ||
adjuster.email().adjust("user+mailbox/[email protected]"); // === "user+mailbox/[email protected]"; dot-string | ||
adjuster.email().adjust("!#$%&'*+-/=?^_`.{|}[email protected]"); // === "!#$%&'*+-/=?^_`.{|}[email protected]"; dot-string | ||
adjuster.email().adjust("\"Fred\\\"Bloggs\"@example.com"); // === "\"Fred\\\"Bloggs\"@example.com"; quoted-string | ||
adjuster.email().adjust("\"Joe.\\\\Blow\"@example.com"); // === "\"Joe.\\\\Blow\"@example.com"; quoted-string | ||
adjuster.email().adjust("[email protected]"); // === "[email protected]" | ||
adjuster.email().adjust("[email protected]"); // === "[email protected]" | ||
|
||
// should cause errors; err.cause === adjuster.CAUSE.EMAIL | ||
adjuster.email().adjust("@example.com"); | ||
adjuster.email().adjust("[email protected]"); | ||
adjuster.email().adjust("[email protected]"); | ||
adjuster.email().adjust("[email protected]"); | ||
adjuster.email().adjust("user@example@com"); | ||
adjuster.emal().adjust("user@example@com"); | ||
adjuster.email().adjust("user-example-com"); | ||
adjuster.email().adjust("user@example_domain.com"); | ||
adjuster.email().adjust("[email protected]"); | ||
|
@@ -93,13 +129,17 @@ const inputData = { | |
name: "Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Ciprin Cipriano de la Santísima Trinidad Ruiz y Picasso", | ||
email: "[email protected]", | ||
state: "active", | ||
remote_addr: "127.0.0.1", | ||
remote_addr_ipv6: "::1", | ||
limit: "0", | ||
}; | ||
const adjusters = { | ||
id: adjuster.number().minValue(1), | ||
name: adjuster.string().maxLength(16, true), | ||
email: adjuster.email(), | ||
state: adjuster.string().in("active", "inactive"), | ||
remote_addr: adjuster.ipv4(), | ||
remote_addr_ipv6: adjuster.ipv6(), | ||
limit: adjuster.number().default(10).minValue(1, true).maxValue(100, true), | ||
offset: adjuster.number().default(0).minValue(0, true), | ||
}; | ||
|
@@ -108,6 +148,8 @@ const expected = { | |
name: "Pablo Diego José", | ||
email: "[email protected]", | ||
state: "active", | ||
remote_addr: "127.0.0.1", | ||
remote_addr_ipv6: "::1", | ||
limit: 1, | ||
offset: 0, | ||
}; | ||
|
@@ -118,19 +160,23 @@ const adjusted = adjuster.adjustData(inputData, adjusters); | |
|
||
## Release notes | ||
|
||
* 2018/05/06 *version 0.4.0* | ||
* New Functions | ||
* `adjuster.ipv4()` | ||
* `adjuster.ipv6()` | ||
* Change Specifications | ||
* strict IPv4 and IPv6 validation for `adjuster.email()` | ||
* 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import {CAUSE} from "./constants"; | ||
|
||
import AdjusterInterface from "./AdjusterInterface"; | ||
import StringAdjuster from "./StringAdjuster"; | ||
|
||
const PATTERN_COMPONENT = `(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})`; | ||
const PATTERN = `${PATTERN_COMPONENT}(\\.${PATTERN_COMPONENT}){3}`; | ||
|
||
const REGEXP = new RegExp(`^${PATTERN}$`); | ||
|
||
export {PATTERN}; | ||
|
||
/** | ||
* adjuster for IPv4 | ||
*/ | ||
export default class IPv4Adjuster extends AdjusterInterface | ||
{ | ||
/** | ||
* constructor | ||
*/ | ||
constructor() | ||
{ | ||
super(); | ||
|
||
this._objAdjuster = new StringAdjuster() | ||
.pattern(REGEXP); | ||
} | ||
|
||
/** | ||
* set default value; enable to omit | ||
* @param {string} value default value | ||
* @return {IPv4Adjuster} | ||
*/ | ||
default(value) | ||
{ | ||
this._objAdjuster.default(value); | ||
return this; | ||
} | ||
|
||
/** | ||
* allow empty string (NOT undefined) | ||
* @param {?string} [value=null] value on empty | ||
* @return {IPv4Adjuster} | ||
*/ | ||
allowEmpty(value = null) | ||
{ | ||
this._objAdjuster.allowEmpty(value); | ||
return this; | ||
} | ||
|
||
/** | ||
* do adjust | ||
* @param {*} value value to be checked | ||
* @param {?_OnError} onError callback function on error | ||
* @return {string} adjusted value | ||
*/ | ||
adjust(value, onError = null) | ||
{ | ||
try | ||
{ | ||
return this._objAdjuster.adjust(value); | ||
} | ||
catch(err) | ||
{ | ||
if(err.cause === CAUSE.PATTERN) | ||
{ | ||
err.cause = CAUSE.IPV4; | ||
} | ||
return AdjusterInterface._handleError(onError, err.cause, value); | ||
} | ||
} | ||
} |
Oops, something went wrong.