Comparing version 0.0.3 to 0.0.4
@@ -66,2 +66,27 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
var afterPassContext = { | ||
when: when, | ||
whenNot: whenNot | ||
}; | ||
var afterWhenContext = { | ||
orWhen: orWhen, | ||
orWhenNot: orWhenNot, | ||
andWhen: andWhen, | ||
andWhenNot: andWhenNot, | ||
to: to | ||
}; | ||
var afterToContext = { | ||
when: when, | ||
whenNot: whenNot, | ||
or: or, | ||
resolve: resolve, | ||
resolveToPromise: resolveToPromise | ||
}; | ||
var afterOrContext = { | ||
to: to | ||
}; | ||
function when(fn) { | ||
@@ -75,2 +100,10 @@ if (typeof fn !== "function") { | ||
function whenNot(fn) { | ||
if (typeof fn !== "function") { | ||
throw new Error("first argument passed to \"whenNot\" must be a function"); | ||
} | ||
lastMatches = !result && !fn(what); | ||
return afterWhenContext; | ||
} | ||
function andWhen(fn) { | ||
@@ -84,2 +117,10 @@ if (typeof fn !== "function") { | ||
function andWhenNot(fn) { | ||
if (typeof fn !== "function") { | ||
throw new Error("first argument passed to \"andWhenNot\" must be a function"); | ||
} | ||
lastMatches = !result && lastMatches && !fn(what); | ||
return afterWhenContext; | ||
} | ||
function orWhen(fn) { | ||
@@ -95,2 +136,12 @@ if (typeof fn !== "function") { | ||
function orWhenNot(fn) { | ||
if (typeof fn !== "function") { | ||
throw new Error("first argument passed to \"orWhen\" must be a function"); | ||
} | ||
if (!result && !fn(what)) { | ||
lastMatches = true; | ||
} | ||
return afterWhenContext; | ||
} | ||
function to(fn) { | ||
@@ -112,2 +163,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); | ||
@@ -117,26 +171,8 @@ } | ||
function resolveToPromise() { | ||
if (typeof result !== "function") { | ||
throw new Error("at least one \"when\" case must match before resolveToPromise"); | ||
} | ||
return Promise.resolve(result(what)); | ||
} | ||
var afterPassContext = { | ||
when: when | ||
}; | ||
var afterToContext = { | ||
when: when, | ||
or: or, | ||
resolve: resolve, | ||
resolveToPromise: resolveToPromise | ||
}; | ||
var afterOrContext = { | ||
to: to | ||
}; | ||
var afterWhenContext = { | ||
orWhen: orWhen, | ||
andWhen: andWhen, | ||
to: to | ||
}; | ||
return afterPassContext; | ||
@@ -143,0 +179,0 @@ } |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("pass-when",[],t):"object"==typeof exports?exports["pass-when"]=t():e["pass-when"]=t()}(this,function(){return function(e){function t(o){if(n[o])return n[o].exports;var r=n[o]={exports:{},id:o,loaded:!1};return e[o].call(r.exports,r,r.exports,t),r.loaded=!0,r.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t){"use strict";function n(e){function t(t){if("function"!=typeof t)throw new Error('first argument passed to "when" must be a function');return i=!c&&t(e),l}function n(t){if("function"!=typeof t)throw new Error('first argument passed to "andWhen" must be a function');return i=!c&&i&&t(e),l}function o(t){if("function"!=typeof t)throw new Error('first argument passed to "orWhen" must be a function');return!c&&t(e)&&(i=!0),l}function r(e){if("function"!=typeof e)throw new Error('first argument passed to "to" must be a function');return i&&(c=e),p}function u(){return i=!c&&!i,d}function f(){return c(e)}function s(){return Promise.resolve(c(e))}var i=null,c=null,a={when:t},p={when:t,or:u,resolve:f,resolveToPromise:s},d={to:r},l={orWhen:o,andWhen:n,to:r};return a}Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=n,e.exports=t["default"]}])}); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("pass-when",[],e):"object"==typeof exports?exports["pass-when"]=e():t["pass-when"]=e()}(this,function(){return function(t){function e(o){if(n[o])return n[o].exports;var r=n[o]={exports:{},id:o,loaded:!1};return t[o].call(r.exports,r,r.exports,e),r.loaded=!0,r.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e){"use strict";function n(t){function e(e){if("function"!=typeof e)throw new Error('first argument passed to "when" must be a function');return p=!h&&e(t),d}function n(e){if("function"!=typeof e)throw new Error('first argument passed to "whenNot" must be a function');return p=!h&&!e(t),d}function o(e){if("function"!=typeof e)throw new Error('first argument passed to "andWhen" must be a function');return p=!h&&p&&e(t),d}function r(e){if("function"!=typeof e)throw new Error('first argument passed to "andWhenNot" must be a function');return p=!h&&p&&!e(t),d}function u(e){if("function"!=typeof e)throw new Error('first argument passed to "orWhen" must be a function');return!h&&e(t)&&(p=!0),d}function f(e){if("function"!=typeof e)throw new Error('first argument passed to "orWhen" must be a function');return h||e(t)||(p=!0),d}function s(t){if("function"!=typeof t)throw new Error('first argument passed to "to" must be a function');return p&&(h=t),m}function i(){return p=!h&&!p,l}function a(){if("function"!=typeof h)throw new Error('at least one "when" case must match before resolve');return h(t)}function c(){if("function"!=typeof h)throw new Error('at least one "when" case must match before resolveToPromise');return Promise.resolve(h(t))}var p=null,h=null,w={when:e,whenNot:n},d={orWhen:u,orWhenNot:f,andWhen:o,andWhenNot:r,to:s},m={when:e,whenNot:n,or:i,resolve:a,resolveToPromise:c},l={to:s};return w}Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=n,t.exports=e["default"]}])}); | ||
//# sourceMappingURL=pass-when.min.js.map |
{ | ||
"name": "pass-when", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Functional and more robust switch statement replacement", | ||
@@ -5,0 +5,0 @@ "main": "lib/pass-when.js", |
@@ -87,1 +87,32 @@ # pass-when | ||
``` | ||
* has negative operators whenNot, andWhenNot and orWhenNot corresponding to when, | ||
andWhen and orWhen | ||
```js | ||
function compute(value) { | ||
return pass(value) | ||
.whenNot(v => v <= 100) | ||
.andWhenNot(v => v % 2 === 1) | ||
.to(v => `${v} is greater than 100 and even`) | ||
.whenNot(v => v <= 100) | ||
.andWhenNot(v => v % 2 === 0) | ||
.to(v => `${v} is greater than 100 and odd`) | ||
.whenNot(v => v >= 50) | ||
.orWhenNot(v => v % 2 === 0) | ||
.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 | ||
``` | ||
## Todo | ||
* Alternative syntax `when(a, b)=>when(a).to(b)` ([issues/1](https://github.com/MichalZalecki/pass-when/issues/1)) |
@@ -7,2 +7,3 @@ function pass(what) { | ||
when, | ||
whenNot, | ||
}; | ||
@@ -12,3 +13,5 @@ | ||
orWhen, | ||
orWhenNot, | ||
andWhen, | ||
andWhenNot, | ||
to, | ||
@@ -19,2 +22,3 @@ }; | ||
when, | ||
whenNot, | ||
or, | ||
@@ -37,2 +41,10 @@ resolve, | ||
function whenNot(fn) { | ||
if (typeof fn !== "function") { | ||
throw new Error("first argument passed to \"whenNot\" must be a function"); | ||
} | ||
lastMatches = !result && !fn(what); | ||
return afterWhenContext; | ||
} | ||
function andWhen(fn) { | ||
@@ -46,2 +58,10 @@ if (typeof fn !== "function") { | ||
function andWhenNot(fn) { | ||
if (typeof fn !== "function") { | ||
throw new Error("first argument passed to \"andWhenNot\" must be a function"); | ||
} | ||
lastMatches = !result && lastMatches && !fn(what); | ||
return afterWhenContext; | ||
} | ||
function orWhen(fn) { | ||
@@ -57,2 +77,12 @@ if (typeof fn !== "function") { | ||
function orWhenNot(fn) { | ||
if (typeof fn !== "function") { | ||
throw new Error("first argument passed to \"orWhen\" must be a function"); | ||
} | ||
if (!result && !fn(what)) { | ||
lastMatches = true; | ||
} | ||
return afterWhenContext; | ||
} | ||
function to(fn) { | ||
@@ -59,0 +89,0 @@ if (typeof fn !== "function") { |
@@ -79,2 +79,43 @@ import chai from "chai"; | ||
}); | ||
it("has negative operator whenNot corresponding to when", () => { | ||
function compute(value) { | ||
return pass(value) | ||
.whenNot(v => v <= 10) | ||
.to(v => `${v} is greater than 10`) | ||
.whenNot(v => v >= 10) | ||
.to(v => `${v} is lower than 10`) | ||
.whenNot(v => v !== 10) | ||
.to(() => "is 10") | ||
.resolve(); | ||
} | ||
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("has negative operators andWhenNot and orWhenNot corresponding to andWhen and orWhen", () => { | ||
function compute(value) { | ||
return pass(value) | ||
.whenNot(v => v <= 100) | ||
.andWhenNot(v => v % 2 === 1) | ||
.to(v => `${v} is greater than 100 and even`) | ||
.whenNot(v => v <= 100) | ||
.andWhenNot(v => v % 2 === 0) | ||
.to(v => `${v} is greater than 100 and odd`) | ||
.whenNot(v => v >= 50) | ||
.orWhenNot(v => v % 2 === 0) | ||
.to(v => `${v} is lower than 50 or odd`) | ||
.or() | ||
.to(v => `${v} is 50 - 100`) | ||
.resolve(); | ||
} | ||
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"); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
38285
425
118