Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

functional-match-case

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

functional-match-case - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

78

index.js

@@ -1,61 +0,1 @@

/**
* functional-match-case
* --------------
* `npm install functional-match-case`
* or
* `yarn add functional-match-case`
*
* Example:
*
* Turn:
*
* switch(someValue) {
* case A:
* case B:
* return resultA;
* case C:
* return resultB;
* case D:
* return functionC();
* default:
* return defaultValue;
* }
*
* Into:
*
* import matchCase from 'functional-match-case';
*
* const match = matchCase({
* [A]: resultA,
* [B]: resultA,
* [C]: resultB,
* [D]: functionC, // just the ref, will be called when needed
* })(defaultValue);
*
* Then whenever needed:
*
* match(someValue);
*
* Extra benefits:
*
* Comparability of cases. For example:
*
* someMatchCase = {
* [A]: resultA,
* [B]: resultB,
* };
*
* anotherMatchCase = {
* [C]: resultC,
* [D]: functionD,
* }
*
* composedMatcher = matchCase({
* ...someMatchCase,
* ...anotherMatchCase,
* [F]: resultF,
* })(defaultValue);
*
*/
const executeIfFunction = require('execute-if-function');

@@ -67,4 +7,9 @@

*/
const switchCaseHashMap = cases => defaultCase => key =>
cases.hasOwnProperty(key) ? cases[key] : defaultCase;
var switchCaseHashMap = function switchCaseHashMap(cases) {
return function (defaultCase) {
return function (key) {
return cases.hasOwnProperty(key) ? cases[key] : defaultCase;
};
};
};

@@ -75,5 +20,10 @@ /**

*/
const matchCase = cases => defaultCase => key =>
executeIfFunction(switchCaseHashMap(cases)(defaultCase)(key));
var matchCase = function matchCase(cases) {
return function (defaultCase) {
return function (key) {
return executeIfFunction(switchCaseHashMap(cases)(defaultCase)(key));
};
};
};
module.exports = matchCase;

2

package.json
{
"name": "functional-match-case",
"version": "1.0.2",
"version": "1.0.3",
"description": "A functional approach to using switch.",

@@ -5,0 +5,0 @@ "main": "index.js",

# functional-match-case
Use lazy, functional programming friendly hash maps instead of `switch` statements.
## Getting Started
**Install**
```bash

@@ -9,4 +15,16 @@ npm install functional-match-case

**Use it!**
```javascript
import matchCase from 'functional-match-case';
const match = matchCase({
cars: 1,
trucks: 2,
other: getOtherCode, // a function to call in case of “other” case
})(defaultValue); // defaultValue could be anything, even a ref to a function that should be called!
```
## Example:
Now for some more detailed examples and use cases:
Turn:

@@ -76,3 +94,3 @@ ```javascript

You could use a simple hash map instead of a `switch`. (Assuming no need for a `default` case.)
You could use a simple hash map instead of a `switch`. (Assuming no need for a `default` case.)

@@ -89,4 +107,4 @@ But, if you added functions like this:

Then both functions would be executed.
Then those functions would be executed and evaluated right away.
With `functional-match-case` you just pass a reference and it will be executed when needed.

@@ -20,1 +20,25 @@ const test = require('tape');

});
test('README example works', (t) => {
const A = 'A';
const B = 'B';
const C = 'C';
const D = 'D';
const resultA = 1;
const resultB = 2;
const functionC = () => 3;
const defaultValue = 4;
const match = matchCase({
[A]: resultA,
[B]: resultA,
[C]: resultB,
[D]: functionC,
})(defaultValue);
t.plan(5);
t.equal(match(A), resultA);
t.equal(match(B), resultA);
t.equal(match(C), resultB);
t.equal(match(D), functionC());
t.equal(match(), defaultValue);
t.end();
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc