@typed-f/either
Version:
[![NPM Version][either-npm-version-badge]][either-npm] [repo-circleci-badge]: https://img.shields.io/circleci/project/github/Ailrun/typed-f/master.svg?logo=circleci [![Known Vulnerabilities][either-snyk-badge]][either-snyk] [![Supported TypeScript Version
247 lines (246 loc) • 7.68 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var EitherTag = '__typed_f__Either__';
//tslint:disable-next-line: max-line-length
var BaseEither = /** @class */ (function () {
function BaseEither() {
//tslint:disable-next-line: variable-name
this.__typed_f__tag__ = EitherTag;
this.caseOf = this.matchWith;
this.lift = this.map;
this.fmap = this.map;
this.of = this.unit;
this.chain = this.bind;
}
BaseEither.prototype.notEquals = function (other) {
return !this.equals(other);
};
BaseEither.prototype.unit = function (v) {
return new Right(v);
};
return BaseEither;
}());
var Left = /** @class */ (function (_super) {
__extends(Left, _super);
function Left(value) {
var _this = _super.call(this) || this;
_this._kind = Either.Kind.Left;
_this._value = value;
return _this;
}
Object.defineProperty(Left.prototype, "value", {
get: function () {
return this._value;
},
enumerable: true,
configurable: true
});
Left.prototype.isLeft = function () {
return true;
};
Left.prototype.isRight = function () {
return false;
};
Left.prototype.leftOr = function (_def) {
return this._value;
};
Left.prototype.leftOrThrow = function (_err) {
return this._value;
};
Left.prototype.leftOrCompute = function (_f) {
return this._value;
};
Left.prototype.rightOr = function (def) {
return def;
};
Left.prototype.rightOrThrow = function (err) {
throw err;
};
Left.prototype.rightOrCompute = function (f) {
return f(this._value);
};
Left.prototype.matchWith = function (cases) {
return cases.left(this._value);
};
Left.prototype.equals = function (other) {
if (!(other instanceof BaseEither) ||
other.isRight()) {
return false;
}
if (typeof this._value === 'object' &&
this._value != undefined &&
typeof this._value.equals === 'function') {
return Boolean(this._value.equals(other._value));
}
return this._value === other._value;
};
Left.prototype.map = function (_f) {
return this;
};
Left.prototype.ap = function (_f) {
return this;
};
Left.prototype.bind = function (_f) {
return this;
};
return Left;
}(BaseEither));
exports.Left = Left;
var Right = /** @class */ (function (_super) {
__extends(Right, _super);
function Right(value) {
var _this = _super.call(this) || this;
_this._kind = Either.Kind.Right;
_this._value = value;
return _this;
}
Object.defineProperty(Right.prototype, "value", {
get: function () {
return this._value;
},
enumerable: true,
configurable: true
});
Right.prototype.isLeft = function () {
return false;
};
Right.prototype.isRight = function () {
return true;
};
Right.prototype.leftOr = function (def) {
return def;
};
Right.prototype.leftOrThrow = function (err) {
throw err;
};
Right.prototype.leftOrCompute = function (f) {
return f(this._value);
};
Right.prototype.rightOr = function (_def) {
return this._value;
};
Right.prototype.rightOrThrow = function (_err) {
return this._value;
};
Right.prototype.rightOrCompute = function (_f) {
return this._value;
};
Right.prototype.matchWith = function (cases) {
return cases.right(this._value);
};
Right.prototype.equals = function (other) {
if (!(other instanceof BaseEither) ||
other.isLeft()) {
return false;
}
if (typeof this._value === 'object' &&
this._value != undefined &&
typeof this._value.equals === 'function') {
return this._value.equals(other._value);
}
return this._value === other._value;
};
Right.prototype.map = function (f) {
return new Right(f(this._value));
};
Right.prototype.ap = function (f) {
if (f.isLeft()) {
return f;
}
return this.map(f._value);
};
Right.prototype.bind = function (f) {
return f(this._value);
};
return Right;
}(BaseEither));
exports.Right = Right;
var Either;
(function (Either) {
var Kind;
(function (Kind) {
Kind[Kind["Left"] = 0] = "Left";
Kind[Kind["Right"] = 1] = "Right";
})(Kind = Either.Kind || (Either.Kind = {}));
function unit(value) {
return new Right(value);
}
Either.unit = unit;
Either.of = unit;
function sequenceObject(obj) {
var entries = Object.keys(obj)
.map(function (key) { return [key, obj[key]]; });
var leftEntries = entries
.filter(function (entry) { return entry[1].isLeft(); });
if (leftEntries.length > 0) {
return new Left(leftEntries[0][1].value);
}
var result = entries
.filter(function (entry) {
return !entry[1].isLeft();
})
.reduce(function (acc, _a) {
var _b;
var _c = __read(_a, 2), key = _c[0], value = _c[1].value;
return __assign(__assign({}, acc), (_b = {}, _b[key] = value, _b));
}, {});
return new Right(result);
}
Either.sequenceObject = sequenceObject;
function sequenceArray(array) {
return array.reduce(function (acc, mayv) {
return acc.ap(mayv.map(function (v) { return function (arr) { return __spread(arr, [v]); }; }));
}, new Right([]));
}
Either.sequenceArray = sequenceArray;
function map(f) {
return function (eitherv) {
return eitherv.map(f);
};
}
Either.map = map;
})(Either = exports.Either || (exports.Either = {}));