Skip to content

Commit

Permalink
Merge pull request #177 from shimataro/develop
Browse files Browse the repository at this point in the history
version 0.14.0
  • Loading branch information
shimataro authored Aug 11, 2018
2 parents 92cdcc1 + 35c53b6 commit b28b98d
Show file tree
Hide file tree
Showing 57 changed files with 3,019 additions and 2,905 deletions.
24 changes: 10 additions & 14 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
{
"comments": false,
"presets": [
[
"env",
{
"targets": {
"node": "current"
},
"exclude": [
"transform-regenerator"
],
"useBuiltIns": true
}
]
["@babel/preset-env", {"targets": {"node": "current"}, "useBuiltIns": "usage"}]
],
"plugins": [
"transform-decorators-legacy"
["@babel/plugin-proposal-decorators", {"legacy": true}]
],
"comments": false
"env": {
"esm": {
"presets": [
["@babel/preset-env", {"targets": {"node": "current"}, "useBuiltIns": "usage", "modules": false}]
]
}
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ Thumbs.db
/*.iml
/nbproject/
/.project
/.vscode/
/.vscode/**
!/.vscode/tasks.json
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ cache:
directories:
- "node_modules"
script:
- "npm test"
- "npm run lint"
- "npm run verify"
37 changes: 37 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "verify",
"group": {
"kind": "test",
"isDefault": true
},
"problemMatcher": []
},
{
"type": "npm",
"script": "test",
"group": "test",
"problemMatcher": []
},
{
"type": "npm",
"script": "watch",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"type": "npm",
"script": "build",
"group": "build",
"problemMatcher": []
}
]
}
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.14.0] - 2018-08-11
### Added
* Introduction in `README.md`

### Changed
* cause an error `adjuster.CAUSE.NOT_OBJECT` when `input` type of `adjuster.adjust()` is not an object

### Fixed
* build error in Windows

### Others
* support ES Modules (`*.mjs`)

## [0.13.0] - 2018-07-20
### Added
* `adjuster.numberArray().eachInteger()`
Expand Down Expand Up @@ -138,7 +151,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [0.1.0] - 2018-04-18
* First release.

[Unreleased]: https://github.com/shimataro/node-adjuster/compare/v0.13.0...HEAD
[Unreleased]: https://github.com/shimataro/node-adjuster/compare/v0.14.0...HEAD
[0.14.0]: https://github.com/shimataro/node-adjuster/compare/v0.13.0...v0.14.0
[0.13.0]: https://github.com/shimataro/node-adjuster/compare/v0.12.0...v0.13.0
[0.12.0]: https://github.com/shimataro/node-adjuster/compare/v0.11.0...v0.12.0
[0.11.0]: https://github.com/shimataro/node-adjuster/compare/v0.10.1...v0.11.0
Expand Down
100 changes: 99 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ validate and adjust input values

## Table of Contents

* [Introduction](#introduction)
* [Install](#install)
* [Loading](#loading)
* [Reference](#reference)
Expand All @@ -29,13 +30,84 @@ validate and adjust input values

---

## Introduction

All of web applications need handling input parameters, consists of following steps:
* fill default values
* cast types
* validate values
* ...and revise them if needed

`node-adjuster` does all of them, by compact and highly readable code!

### example

```javascript
import adjuster from "adjuster";
import assert from "assert";

const constraints = { // constraints for input
id: adjuster.number().minValue(1), // number, >=1
name: adjuster.string().maxLength(16, true), // string, max 16 characters (trims if over)
age: adjuster.number().integer(true).minValue(0), // number, integer (trims if decimal), >=0
email: adjuster.email(), // e-mail
state: adjuster.string().only("active", "inactive"), // string, accepts only "active" and "inactive"
classes: adjuster.numberArray().separatedBy(",").ignoreEachErrors(), // array of number, separated by ",", ignores errors
skills: adjuster.stringArray().separatedBy(",").ignoreEachErrors(), // array of string, separated by ",", ignores errors
credit_card: adjuster.numericString().separatedBy("-").checksum(adjuster.NUMERIC_STRING_CHECKSUM_ALGORITHM.CREDIT_CARD), // numeric string, separated by "-", checks by Luhn algorithm
remote_addr: adjuster.ipv4(), // IPv4
remote_addr_ipv6: adjuster.ipv6(), // IPv6
limit: adjuster.number().integer().default(10).minValue(1, true).maxValue(100, true), // number, integer, omittable (sets 10 if omitted), >=1 (sets 1 if less), <=100 (sets 100 if greater)
offset: adjuster.number().integer().default(0).minValue(0, true), // number, integer, omiitable (sets 0 if omited), >=0 (sets 0 if less)
};
const input = { // input values
id: "1",
name: "Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Ciprin Cipriano de la Santísima Trinidad Ruiz y Picasso",
age: 20.5,
email: "[email protected]",
state: "active",
classes: "1,3,abc,4",
skills: "c,c++,javascript,python,,swift,kotlin",
credit_card: "4111-1111-1111-1111",
remote_addr: "127.0.0.1",
remote_addr_ipv6: "::1",
limit: "0",
};
const expected = { // should be adjusted to this
id: 1,
name: "Pablo Diego José",
age: 20,
email: "[email protected]",
state: "active",
classes: [1, 3, 4],
skills: ["c", "c++", "javascript", "python", "swift", "kotlin"],
credit_card: "4111111111111111",
remote_addr: "127.0.0.1",
remote_addr_ipv6: "::1",
limit: 1,
offset: 0,
};

// Let's adjust!
const adjusted = adjuster.adjust(input, constraints);

// verification
assert.deepStrictEqual(adjusted, expected);
```

That's all! No control flows! Isn't it cool?

For details, see [basic usage](#basic-usage).

## Install

install from [npm registry](https://www.npmjs.com/package/adjuster).
```bash
npm install -S adjuster
```

NOTE: package name is `adjuster`, NOT `node-adjuster`!

## Loading

### using [Babel](https://babeljs.io/)
Expand All @@ -54,7 +126,11 @@ or
var {default: adjuster} = require("adjuster");
```

ES Modules(`.mjs`) is not provided for now.
### ES Modules
```javascript
// foo.mjs - in the same way as Babel!
import adjuster from "adjuster";
```

## Reference
### types and constants
Expand Down Expand Up @@ -291,6 +367,28 @@ catch (err) {
}
```

###### error handling 4
when input value is not an object

NOTE: `constraint` won't be checked because it's predictable; should be generated by programmer, not an external input

```javascript
import adjuster from "adjuster";
import assert from "assert";

const constraints = {};
const input = 123;

try {
// `input` must be an object
const adjusted = adjuster.adjust(input, constraints);
}
catch (err) {
assert.strictEqual(err.key, null);
assert.strictEqual(err.cause, adjuster.CAUSE.NOT_OBJECT);
}
```

### number
#### ambient declarations

Expand Down
3 changes: 1 addition & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ install:
test_script:
- "node --version"
- "npm --version"
- "npm test"
- "npm run lint"
- "npm run verify"
build: "off"
2 changes: 1 addition & 1 deletion create-release-branch
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function regenerate_npm_shrinkwrap() {
}

function verify_package() {
npm test && npm run lint
npm run verify
}

function commit_changes() {
Expand Down
3 changes: 0 additions & 3 deletions node-adjuster.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
}
],
"settings": {
"files.associations": {
"*.es": "javascript"
},
"eslint.enable": true,
"json.format.enable": true,
"javascript.format.enable": true,
Expand Down
Loading

0 comments on commit b28b98d

Please sign in to comment.