@fakenickels/let-anything
Advanced tools
Comparing version
{ | ||
"name": "@fakenickels/let-anything", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "src/index.js", | ||
"license": "MIT", | ||
"typings": "src/index.ts", | ||
"devDependencies": { | ||
@@ -7,0 +8,0 @@ "fp-ts": "^2.8.2", |
@@ -15,5 +15,60 @@ <p align="center"> | ||
# Quick usage | ||
<details> | ||
<summary>Show async/await demo</summary> | ||
[Run in CodeSandbox](https://codesandbox.io/s/wizardly-hopper-n1n1f?file=/src/index.ts) | ||
```js | ||
import {letAnything} from '@fakenickels/let-anything'; | ||
// define a context, in this case we are creating our own async-await! | ||
const letPromise = letAnything({ | ||
let_: (value, continuation) => value.then(continuation), | ||
}); | ||
async function main() { | ||
const userName = yield Promise.resolve("Subaru-kun") | ||
const deathCount = yield Promise.resolve(12909238409382) | ||
return Promise.resolve(`User ${userName} has a death count of ${deathCount}`) | ||
} | ||
letPromise(main).then(console.log).catch(console.log) | ||
// User Subaru-kun has a death count of 12909238409382 | ||
``` | ||
</details> | ||
<details> | ||
<summary>Show `fp-ts/either` demo</summary> | ||
[Run in CodeSandbox](https://codesandbox.io/s/wizardly-hopper-n1n1f?file=/src/index.ts) | ||
```js | ||
import {either} from 'fp-ts' | ||
import {letAnything} from '@fakenickels/let-anything' | ||
// You could say I'm not a very good with TS types | ||
const letEither = letAnything<either.Either<any, any>>({ | ||
let_: (value, continuation) => either.chain(continuation)(value) | ||
}); | ||
function* stuff() { | ||
const value = yield either.right("d"); | ||
const anotherValue = yield either.right("e"); | ||
const anotherAnother = yield either.right("bug"); | ||
return either.right(value + anotherValue + anotherAnother); | ||
} | ||
console.log( | ||
either.getOrElse(error => `Something went wrong: ${error}`)(letEither(stuff)) | ||
) | ||
// debug | ||
``` | ||
</details> | ||
# Why? | ||
@@ -20,0 +75,0 @@ |
@@ -32,24 +32,22 @@ "use strict"; | ||
var index_1 = require("../src/index"); | ||
var letResultAsync = index_1.letAnything({ | ||
var letEither = index_1.letAnything({ | ||
let_: function (value, continuation) { return fp_ts_1.either.chain(continuation)(value); } | ||
}); | ||
function stuff() { | ||
var value, anotherValue, anotherAnother, _a, _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
var value, anotherValue, anotherAnother; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, fp_ts_1.either.right("d")]; | ||
case 1: | ||
value = _c.sent(); | ||
value = _a.sent(); | ||
return [4 /*yield*/, fp_ts_1.either.right("e")]; | ||
case 2: | ||
anotherValue = _c.sent(); | ||
anotherValue = _a.sent(); | ||
return [4 /*yield*/, fp_ts_1.either.right("bug")]; | ||
case 3: | ||
anotherAnother = _c.sent(); | ||
_b = (_a = Promise).resolve; | ||
return [4 /*yield*/, fp_ts_1.either.right(value + anotherValue + anotherAnother)]; | ||
case 4: return [2 /*return*/, _b.apply(_a, [_c.sent()])]; | ||
anotherAnother = _a.sent(); | ||
return [2 /*return*/, fp_ts_1.either.right(value + anotherValue + anotherAnother)]; | ||
} | ||
}); | ||
} | ||
letResultAsync(stuff); | ||
console.log(fp_ts_1.either.getOrElse(function (error) { return "Something went wrong: " + error; })(letEither(stuff))); |
@@ -5,3 +5,3 @@ import {either} from 'fp-ts' | ||
const letEither = letAnything<either.Either<any, any>>({ | ||
let_: (value, continuation) => either.chain(continuation)(value) | ||
let_: (value, continuation) => either.chain(continuation)(value) | ||
}); | ||
@@ -17,2 +17,5 @@ | ||
letEither(stuff) | ||
console.log( | ||
either.getOrElse(error => `Something went wrong: ${error}`)(letEither(stuff)) | ||
) |
@@ -6,3 +6,3 @@ { | ||
"module": "commonjs", | ||
"lib": ["ES2020"], | ||
"lib": ["ES2018", "DOM"], | ||
"rootDirs": [ "./src", "./tests" ], | ||
@@ -9,0 +9,0 @@ "strict": true, |
159
52.88%15346
-93.5%9
-10%