Comparing version 0.0.1 to 0.0.3
{ | ||
"name": "pass-when", | ||
"version": "0.0.1", | ||
"version": "0.0.3", | ||
"description": "Functional and more robust switch statement replacement", | ||
@@ -11,18 +11,23 @@ "main": "lib/pass-when.js", | ||
"devDependencies": { | ||
"babel": "6.3.13", | ||
"babel-core": "6.1.18", | ||
"babel-eslint": "5.0.0", | ||
"babel-loader": "6.1.0", | ||
"babel-plugin-add-module-exports": "0.1.2", | ||
"babel-preset-es2015": "6.3.13", | ||
"babel": "6.5.2", | ||
"babel-core": "6.8.0", | ||
"babel-eslint": "6.0.4", | ||
"babel-loader": "6.2.4", | ||
"babel-plugin-add-module-exports": "0.2.0", | ||
"babel-preset-es2015": "6.6.0", | ||
"babel-preset-stage-0": "^6.5.0", | ||
"chai": "3.4.1", | ||
"eslint": "1.7.2", | ||
"eslint-loader": "1.1.0", | ||
"mocha": "2.3.4", | ||
"webpack": "1.12.9", | ||
"yargs": "3.32.0" | ||
"chai": "3.5.0", | ||
"eslint": "^2.9.0", | ||
"eslint-config-airbnb-base": "^3.0.0", | ||
"eslint-plugin-import": "^1.7.0", | ||
"mocha": "2.4.5", | ||
"webpack": "1.13.0", | ||
"yargs": "4.7.0" | ||
}, | ||
"scripts": { | ||
"test": "mocha --compilers js:babel-core/register --colors -w ./test/*.spec.js" | ||
"build": "webpack --mode=build", | ||
"dev": "webpack --progress --colors --watch --mode=dev", | ||
"test": "mocha --compilers js:babel-core/register --colors ./test/*.spec.js", | ||
"test:watch": "mocha --compilers js:babel-core/register --colors -w ./test/*.spec.js", | ||
"lint": "eslint src/**/*.js test/**/*.js" | ||
}, | ||
@@ -29,0 +34,0 @@ "repository": { |
# pass-when | ||
Functional and more robust `switch` statement replacement. | ||
```js | ||
pass(8) | ||
.when(v => v > 10).to(v => `${v} is greater than 10`) | ||
.when(v => v < 10).to(v => `${v} is lower than 10`) | ||
.or().to(() => "is 10") | ||
.resolve(); | ||
// 8 is lower than 10 | ||
``` | ||
The idea behind it was to create simple way to solve problems of `switch` | ||
statement and turn it from statement to an expression. It works nicely when | ||
you're creating Redux reducer or writing custom validation with library like | ||
`validator` (what I'm doing when using `redux-form`). | ||
## Install | ||
```bash | ||
npm install --save pass-when | ||
``` | ||
## Usage | ||
* resolve value with functor passed to `to` after first matched `when` | ||
* resolve value with functor passed to `to` after `or` as default | ||
```js | ||
import pass from "pass-when"; | ||
function compute(value) { | ||
return pass(value) | ||
.when(v => v > 10).to(v => `${v} is greater than 10`) | ||
.when(v => v < 10).to(v => `${v} is lower than 10`) | ||
.or().to(() => "is 10") | ||
.resolve(); | ||
} | ||
compute(12); // 12 is greater than 10 | ||
compute(8); // 8 is lower than 10 | ||
compute(10); // is 10 | ||
``` | ||
* allows for additional conditions with `andWhen` and `orWhen` | ||
```js | ||
function compute(value) { | ||
return pass(value) | ||
.when(v => v > 100) | ||
.andWhen(v => v % 2 === 0) | ||
.to(v => `${v} is greater than 100 and even`) | ||
.when(v => v > 100) | ||
.andWhen(v => v % 2 === 1) | ||
.to(v => `${v} is greater than 100 and odd`) | ||
.when(v => v < 50) | ||
.orWhen(v => v % 2 === 1) | ||
.to(v => `${v} is lower than 50 or odd`) | ||
.or() | ||
.to(v => `${v} is 50 - 100`) | ||
.resolve(); | ||
} | ||
compute(201); // 201 is greater than 100 and odd | ||
compute(200); // 200 is greater than 100 and even | ||
compute(52); // 52 is 50 - 100 | ||
compute(51); // 51 is lower than 50 or odd | ||
compute(10); // 10 is lower than 50 or odd | ||
``` | ||
* can resolve to promise | ||
```js | ||
const response = { status: 404, data: { foo: "bar" } }; | ||
pass(response) | ||
.when(({ status }) => status >= 400 && status < 600) | ||
.to(res => ({ ...res, error: true })) | ||
.or() | ||
.to(res => ({ ...res, error: false })) | ||
.resolveToPromise() | ||
.then(res => { | ||
// { status: 404, data: { foo: "bar" }, error: true } | ||
}); | ||
``` |
@@ -5,2 +5,23 @@ function pass(what) { | ||
const afterPassContext = { | ||
when, | ||
}; | ||
const afterWhenContext = { | ||
orWhen, | ||
andWhen, | ||
to, | ||
}; | ||
const afterToContext = { | ||
when, | ||
or, | ||
resolve, | ||
resolveToPromise, | ||
}; | ||
const afterOrContext = { | ||
to, | ||
}; | ||
function when(fn) { | ||
@@ -48,2 +69,5 @@ if (typeof fn !== "function") { | ||
function resolve() { | ||
if (typeof result !== "function") { | ||
throw new Error("at least one \"when\" case must match before resolve"); | ||
} | ||
return result(what); | ||
@@ -53,26 +77,8 @@ } | ||
function resolveToPromise() { | ||
if (typeof result !== "function") { | ||
throw new Error("at least one \"when\" case must match before resolveToPromise"); | ||
} | ||
return Promise.resolve(result(what)); | ||
} | ||
const afterPassContext = { | ||
when, | ||
}; | ||
const afterToContext = { | ||
when, | ||
or, | ||
resolve, | ||
resolveToPromise, | ||
}; | ||
const afterOrContext = { | ||
to, | ||
}; | ||
const afterWhenContext = { | ||
orWhen, | ||
andWhen, | ||
to, | ||
}; | ||
return afterPassContext; | ||
@@ -79,0 +85,0 @@ } |
@@ -9,65 +9,37 @@ import chai from "chai"; | ||
describe("pass-when", () => { | ||
it("can be used as switch statement replacement", () => { | ||
const state = { counter: 5 }; | ||
const action = { type: "DECREMENT_COUNTER", payload: 10 }; | ||
it("resolve value with functor passed to \"to\" after first matched \"when\"", () => { | ||
function compute(value) { | ||
return pass(value) | ||
.when(v => v > 10) | ||
.to(v => `${v} is greater than 10`) | ||
.when(v => v < 10) | ||
.to(v => `${v} is lower than 10`) | ||
.when(v => v === 10) | ||
.to(() => "is 10") | ||
.resolve(); | ||
} | ||
pass(action) | ||
.when(({ type }) => type === "INCREMENT_COUNTER") | ||
.to(({ payload }) => state.counter += payload) | ||
.when(({ type }) => type === "DECREMENT_COUNTER") | ||
.to(({ payload }) => state.counter -= payload) | ||
.resolve(); | ||
expect(state).to.deep.equal({ counter: -5 }); | ||
expect(compute(11)).to.equal("11 is greater than 10"); | ||
expect(compute(9)).to.equal("9 is lower than 10"); | ||
expect(compute(10)).to.equal("is 10"); | ||
}); | ||
it("can be used as switch statement replacement but it's an expression", () => { | ||
const state = { counter: 5 }; | ||
const action = { type: "DECREMENT_COUNTER", payload: 10 }; | ||
it("resolve value with functor passed to \"to\" after \"or\" as default", () => { | ||
function compute(value) { | ||
return pass(value) | ||
.when(v => v > 10) | ||
.to(v => `${v} is greater than 10`) | ||
.when(v => v < 10) | ||
.to(v => `${v} is lower than 10`) | ||
.or() | ||
.to(() => "is 10") | ||
.resolve(); | ||
} | ||
const newState = pass(action) | ||
.when(({ type }) => type === "INCREMENT_COUNTER") | ||
.to(({ payload }) => ({ ...state, counter: state.counter + payload })) | ||
.when(({ type }) => type === "DECREMENT_COUNTER") | ||
.to(({ payload }) => ({ ...state, counter: state.counter - payload })) | ||
.resolve(); | ||
expect(newState).to.deep.equal({ counter: -5 }); | ||
expect(compute(11)).to.equal("11 is greater than 10"); | ||
expect(compute(9)).to.equal("9 is lower than 10"); | ||
expect(compute(10)).to.equal("is 10"); | ||
}); | ||
it("can resolve to promise", done => { | ||
const state = { counter: 5 }; | ||
const action = { type: "DECREMENT_COUNTER", payload: 10 }; | ||
pass(action) | ||
.when(({ type }) => type === "INCREMENT_COUNTER") | ||
.to(({ payload }) => ({ ...state, counter: state.counter + payload })) | ||
.when(({ type }) => type === "DECREMENT_COUNTER") | ||
.to(({ payload }) => ({ ...state, counter: state.counter - payload })) | ||
.resolveToPromise() | ||
.then(newState => { | ||
expect(newState).to.deep.equal({ counter: -5 }); | ||
done(); | ||
}); | ||
}); | ||
it("can resolve default with or", done => { | ||
const state = { counter: 5 }; | ||
const action = { type: "PAYMET_REQUEST" }; | ||
pass(action) | ||
.when(({ type }) => type === "INCREMENT_COUNTER") | ||
.to(({ payload }) => ({ ...state, counter: state.counter + payload })) | ||
.when(({ type }) => type === "DECREMENT_COUNTER") | ||
.to(({ payload }) => ({ ...state, counter: state.counter - payload })) | ||
.or() | ||
.to(() => state) | ||
.resolveToPromise() | ||
.then(newState => { | ||
expect(newState).to.deep.equal(state); | ||
done(); | ||
}); | ||
}); | ||
it("allows for chaining when with andWhen or orWhen for more complicated conditions", () => { | ||
it("allows for additional conditions with \"andWhen\" and \"orWhen\"", () => { | ||
function compute(value) { | ||
@@ -89,8 +61,21 @@ return pass(value) | ||
expect(compute(201)).to.equal("201 is greater than 100 and odd"); | ||
expect(compute(200)).to.equal("200 is greater than 100 and even"); | ||
expect(compute(52)).to.equal("52 is 50 - 100"); | ||
expect(compute(51)).to.equal("51 is lower than 50 or odd"); | ||
expect(compute(10)).to.equal("10 is lower than 50 or odd"); | ||
expect(compute(51)).to.equal("51 is lower than 50 or odd"); | ||
expect(compute(52)).to.equal("52 is 50 - 100"); | ||
expect(compute(200)).to.equal("200 is greater than 100 and even"); | ||
expect(compute(201)).to.equal("201 is greater than 100 and odd"); | ||
}); | ||
it("can resolve to promise", () => { | ||
const response = { status: 404, data: { foo: "bar" } }; | ||
return pass(response) | ||
.when(({ status }) => status >= 400 && status < 600) | ||
.to(res => ({ ...res, error: true })) | ||
.or() | ||
.to(res => ({ ...res, error: false })) | ||
.resolveToPromise() | ||
.then(res => { | ||
expect(res.error).to.equal(true); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
325
87
28727
14