@ioffice/fp
Advanced tools
Comparing version 0.0.0-SNAPSHOT.79fd5033c5bbc1a30a3c47f922b316c3655cfd0e to 0.0.0-SNAPSHOT.e183ae047cc6b9960cdd058fe7d618b459144b95
@@ -15,1 +15,8 @@ # Changelog | ||
## [Unreleased] | ||
## [0.0.1] - August 12, 2019 | ||
- Migrated the fp tools used in the [`ci-builder`](https://github.com/iOffice/ci-builder-eslib). | ||
[Unreleased]: https://github.com/iOffice/fp-eslib/compare/0.0.1...HEAD | ||
[0.0.1]: https://github.com/iOffice/fp-eslib/compare/7270ffed28016080f8bdecef9d29e059a6c3598a...0.0.1 |
@@ -172,2 +172,22 @@ import * as tslib_1 from "tslib"; | ||
/** | ||
* The given function is applied if this is a `Left`. | ||
* | ||
* ``` | ||
* Right(12).mapIfLeft(x => "flower") // Result: Right(12) | ||
* Left(12).mapIfLeft(x => "flower") // Result: Left("flower") | ||
* ``` | ||
* | ||
* The following are equivalent: | ||
* | ||
* ``` | ||
* rightInstance.swap().map(f).swap() | ||
* rightInstance.mapIfLeft(f) | ||
* ``` | ||
*/ | ||
Either.prototype.mapIfLeft = function (f) { | ||
return this.isLeft | ||
? Left(f(this.value)) | ||
: this; | ||
}; | ||
/** | ||
* Returns `Right` with the existing value of `Right` if this is a `Right` and | ||
@@ -227,2 +247,30 @@ * the given predicate `p` holds for the right value, | ||
}; | ||
/** | ||
* Allows us to do | ||
* | ||
* ``` | ||
* try { | ||
* for (const val of instanceOfEither) { | ||
* // handle val if instanceOfEither is a Right. | ||
* } | ||
* } catch (left) { | ||
* // handle instanceOfEither as a left. | ||
* } | ||
* ``` | ||
*/ | ||
Either.prototype[Symbol.iterator] = function () { | ||
var isDone = false; | ||
var instance = this; | ||
return { | ||
next: function () { | ||
if (!instance.isRight) | ||
throw instance; | ||
if (!isDone) { | ||
isDone = true; | ||
return { value: instance.value, done: false }; | ||
} | ||
return { value: instance.value, done: true }; | ||
}, | ||
}; | ||
}; | ||
return Either; | ||
@@ -262,2 +310,39 @@ }()); | ||
}; | ||
export { Either, EitherLeft, EitherRight, Left, Right }; | ||
/** | ||
* Expects `cb` to be a function composed of only `for of` loops that contain | ||
* `Either` objects. When evaluating an `Either` in an iteration this will | ||
* trigger it to throw an exception with the `Left` value which should be | ||
* caught by this function. | ||
* | ||
* This is nothing but a short circuit to break out of a deep nested iteration. | ||
*/ | ||
function evalIteration(cb) { | ||
try { | ||
return Right(cb()); | ||
} | ||
catch (ex) { | ||
return ex; | ||
} | ||
} | ||
/** | ||
* Similar to `evalIteration` but it returns a promise of the result. | ||
*/ | ||
function asyncEvalIteration(cb) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var _a, ex_1; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_b.trys.push([0, 2, , 3]); | ||
_a = Right; | ||
return [4 /*yield*/, cb()]; | ||
case 1: return [2 /*return*/, _a.apply(void 0, [_b.sent()])]; | ||
case 2: | ||
ex_1 = _b.sent(); | ||
return [2 /*return*/, ex_1]; | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
} | ||
export { Either, EitherLeft, EitherRight, Left, Right, evalIteration, asyncEvalIteration, }; |
@@ -0,3 +1,5 @@ | ||
import * as tslib_1 from "tslib"; | ||
import { Some, None } from './Option'; | ||
var match = function (value) { | ||
var e_1, _a; | ||
var options = []; | ||
@@ -7,10 +9,20 @@ for (var _i = 1; _i < arguments.length; _i++) { | ||
} | ||
for (var _a = 0, options_1 = options; _a < options_1.length; _a++) { | ||
var entry = options_1[_a]; | ||
if (entry[0] === value) | ||
return Some(entry[1]()); | ||
try { | ||
for (var options_1 = tslib_1.__values(options), options_1_1 = options_1.next(); !options_1_1.done; options_1_1 = options_1.next()) { | ||
var entry = options_1_1.value; | ||
if (entry[0] === value) | ||
return Some(entry[1]()); | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (options_1_1 && !options_1_1.done && (_a = options_1.return)) _a.call(options_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return None; | ||
}; | ||
var ifElseChain = function () { | ||
var e_2, _a; | ||
var options = []; | ||
@@ -20,10 +32,20 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
} | ||
for (var _a = 0, options_2 = options; _a < options_2.length; _a++) { | ||
var entry = options_2[_a]; | ||
if (entry[0]) | ||
return Some(entry[1]()); | ||
try { | ||
for (var options_2 = tslib_1.__values(options), options_2_1 = options_2.next(); !options_2_1.done; options_2_1 = options_2.next()) { | ||
var entry = options_2_1.value; | ||
if (entry[0]) | ||
return Some(entry[1]()); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (options_2_1 && !options_2_1.done && (_a = options_2.return)) _a.call(options_2); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
return None; | ||
}; | ||
function matchType(value) { | ||
var e_3, _a; | ||
var options = []; | ||
@@ -33,10 +55,19 @@ for (var _i = 1; _i < arguments.length; _i++) { | ||
} | ||
for (var _a = 0, options_3 = options; _a < options_3.length; _a++) { | ||
var entry = options_3[_a]; | ||
if (value instanceof entry[0]) { | ||
return Some(entry[1](value)); | ||
try { | ||
for (var options_3 = tslib_1.__values(options), options_3_1 = options_3.next(); !options_3_1.done; options_3_1 = options_3.next()) { | ||
var entry = options_3_1.value; | ||
if (value instanceof entry[0]) { | ||
return Some(entry[1](value)); | ||
} | ||
} | ||
} | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (options_3_1 && !options_3_1.done && (_a = options_3.return)) _a.call(options_3); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
return None; | ||
} | ||
export { match, ifElseChain, matchType }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const Option_1 = require("./Option"); | ||
@@ -173,2 +174,22 @@ const Types_1 = require("./Types"); | ||
/** | ||
* The given function is applied if this is a `Left`. | ||
* | ||
* ``` | ||
* Right(12).mapIfLeft(x => "flower") // Result: Right(12) | ||
* Left(12).mapIfLeft(x => "flower") // Result: Left("flower") | ||
* ``` | ||
* | ||
* The following are equivalent: | ||
* | ||
* ``` | ||
* rightInstance.swap().map(f).swap() | ||
* rightInstance.mapIfLeft(f) | ||
* ``` | ||
*/ | ||
mapIfLeft(f) { | ||
return this.isLeft | ||
? Left(f(this.value)) | ||
: this; | ||
} | ||
/** | ||
* Returns `Right` with the existing value of `Right` if this is a `Right` and | ||
@@ -228,2 +249,30 @@ * the given predicate `p` holds for the right value, | ||
} | ||
/** | ||
* Allows us to do | ||
* | ||
* ``` | ||
* try { | ||
* for (const val of instanceOfEither) { | ||
* // handle val if instanceOfEither is a Right. | ||
* } | ||
* } catch (left) { | ||
* // handle instanceOfEither as a left. | ||
* } | ||
* ``` | ||
*/ | ||
[Symbol.iterator]() { | ||
let isDone = false; | ||
const instance = this; | ||
return { | ||
next() { | ||
if (!instance.isRight) | ||
throw instance; | ||
if (!isDone) { | ||
isDone = true; | ||
return { value: instance.value, done: false }; | ||
} | ||
return { value: instance.value, done: true }; | ||
}, | ||
}; | ||
} | ||
} | ||
@@ -261,1 +310,32 @@ exports.Either = Either; | ||
exports.Right = Right; | ||
/** | ||
* Expects `cb` to be a function composed of only `for of` loops that contain | ||
* `Either` objects. When evaluating an `Either` in an iteration this will | ||
* trigger it to throw an exception with the `Left` value which should be | ||
* caught by this function. | ||
* | ||
* This is nothing but a short circuit to break out of a deep nested iteration. | ||
*/ | ||
function evalIteration(cb) { | ||
try { | ||
return Right(cb()); | ||
} | ||
catch (ex) { | ||
return ex; | ||
} | ||
} | ||
exports.evalIteration = evalIteration; | ||
/** | ||
* Similar to `evalIteration` but it returns a promise of the result. | ||
*/ | ||
function asyncEvalIteration(cb) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
try { | ||
return Right(yield cb()); | ||
} | ||
catch (ex) { | ||
return ex; | ||
} | ||
}); | ||
} | ||
exports.asyncEvalIteration = asyncEvalIteration; |
{ | ||
"name": "@ioffice/fp", | ||
"version": "0.0.0-SNAPSHOT.79fd5033c5bbc1a30a3c47f922b316c3655cfd0e", | ||
"version": "0.0.0-SNAPSHOT.e183ae047cc6b9960cdd058fe7d618b459144b95", | ||
"description": "", | ||
@@ -16,3 +16,4 @@ "author": "iOFFICE Team", | ||
"devDependencies": { | ||
"@ioffice/ci-builder": "0.0.1", | ||
"@ioffice/ci-builder": "0.1.0", | ||
"@ioffice/fp": "0.0.1", | ||
"@types/chai": "4.1.7", | ||
@@ -19,0 +20,0 @@ "@types/colors": "1.2.1", |
# @ioffice/fp | ||
[![NPM Version](https://img.shields.io/npm/v/@ioffice/fp.svg)](https://www.npmjs.com/package/@ioffice/fp) | ||
[![License](https://img.shields.io/npm/l/@ioffice/fp.svg)](LICENSE) | ||
[![Build Status](https://travis-ci.com/iOffice/fp-eslib.svg?branch=master)](https://travis-ci.com/iOffice/fp-eslib) |
@@ -16,3 +16,3 @@ import { Option } from './Option'; | ||
*/ | ||
declare abstract class Either<A, B> { | ||
declare abstract class Either<A, B> implements Iterable<B> { | ||
/** | ||
@@ -146,2 +146,18 @@ * The value of the `Either` object. | ||
/** | ||
* The given function is applied if this is a `Left`. | ||
* | ||
* ``` | ||
* Right(12).mapIfLeft(x => "flower") // Result: Right(12) | ||
* Left(12).mapIfLeft(x => "flower") // Result: Left("flower") | ||
* ``` | ||
* | ||
* The following are equivalent: | ||
* | ||
* ``` | ||
* rightInstance.swap().map(f).swap() | ||
* rightInstance.mapIfLeft(f) | ||
* ``` | ||
*/ | ||
mapIfLeft<Y>(f: (b: A) => Y): Either<Y, B>; | ||
/** | ||
* Returns `Right` with the existing value of `Right` if this is a `Right` and | ||
@@ -187,2 +203,16 @@ * the given predicate `p` holds for the right value, | ||
static cond<X, Y>(test: boolean, right: LazyArg<Y>, left: LazyArg<X>): Either<X, Y>; | ||
/** | ||
* Allows us to do | ||
* | ||
* ``` | ||
* try { | ||
* for (const val of instanceOfEither) { | ||
* // handle val if instanceOfEither is a Right. | ||
* } | ||
* } catch (left) { | ||
* // handle instanceOfEither as a left. | ||
* } | ||
* ``` | ||
*/ | ||
[Symbol.iterator](): Iterator<B>; | ||
} | ||
@@ -207,2 +237,15 @@ declare class EitherLeft<A, B> extends Either<A, B> { | ||
declare const Right: <A, B>(val: B | null | undefined) => EitherRight<A, B>; | ||
export { Either, EitherLeft, EitherRight, Left, Right }; | ||
/** | ||
* Expects `cb` to be a function composed of only `for of` loops that contain | ||
* `Either` objects. When evaluating an `Either` in an iteration this will | ||
* trigger it to throw an exception with the `Left` value which should be | ||
* caught by this function. | ||
* | ||
* This is nothing but a short circuit to break out of a deep nested iteration. | ||
*/ | ||
declare function evalIteration<A, B>(cb: () => B | undefined): Either<A, B>; | ||
/** | ||
* Similar to `evalIteration` but it returns a promise of the result. | ||
*/ | ||
declare function asyncEvalIteration<A, B>(cb: () => Promise<B | undefined>): Promise<Either<A, B>>; | ||
export { Either, EitherLeft, EitherRight, Left, Right, evalIteration, asyncEvalIteration, }; |
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
80639
2310
6
41