PHP WebShell
Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@celo/contractkit/node_modules/fp-ts/es6
Просмотр файла: Either.js
/**
* @file Represents a value of one of two possible types (a disjoint union).
*
* An instance of `Either` is either an instance of `Left` or `Right`.
*
* A common use of `Either` is as an alternative to `Option` for dealing with possible missing values. In this usage,
* `None` is replaced with a `Left` which can contain useful information. `Right` takes the place of `Some`. Convention
* dictates that `Left` is used for failure and `Right` is used for success.
*
* For example, you could use `Either<string, number>` to detect whether a received input is a `string` or a `number`.
*
* ```ts
* const parse = (errorMessage: string) => (input: string): Either<string, number> => {
* const n = parseInt(input, 10)
* return isNaN(n) ? left(errorMessage) : right(n)
* }
* ```
*
* `Either` is right-biased, which means that `Right` is assumed to be the default case to operate on. If it is `Left`,
* operations like `map`, `chain`, ... return the `Left` value unchanged:
*
* ```ts
* import { either } from 'fp-ts/lib/Either'
*
* either.map(right(12), double) // right(24)
* either.map(left(23), double) // left(23)
* ```
*/
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);
};
import { tailRec } from './ChainRec';
import { pipeable } from './pipeable';
/**
* @since 2.0.0
*/
export var URI = 'Either';
/**
* Constructs a new `Either` holding a `Left` value. This usually represents a failure, due to the right-bias of this
* structure
*
* @since 2.0.0
*/
export function left(e) {
return { _tag: 'Left', left: e };
}
/**
* Constructs a new `Either` holding a `Right` value. This usually represents a successful value due to the right bias
* of this structure
*
* @since 2.0.0
*/
export function right(a) {
return { _tag: 'Right', right: a };
}
/**
* Takes a default and a nullable value, if the value is not nully, turn it into a `Right`, if the value is nully use
* the provided default as a `Left`
*
* @since 2.0.0
*/
export function fromNullable(e) {
return function (a) { return (a == null ? left(e) : right(a)); };
}
/**
* Default value for the `onError` argument of `tryCatch`
*
* @since 2.0.0
*/
export function toError(e) {
return e instanceof Error ? e : new Error(String(e));
}
/**
* Constructs a new `Either` from a function that might throw
*
* @example
* import { Either, left, right, tryCatch } from 'fp-ts/lib/Either'
*
* const unsafeHead = <A>(as: Array<A>): A => {
* if (as.length > 0) {
* return as[0]
* } else {
* throw new Error('empty array')
* }
* }
*
* const head = <A>(as: Array<A>): Either<Error, A> => {
* return tryCatch(() => unsafeHead(as), e => (e instanceof Error ? e : new Error('unknown error')))
* }
*
* assert.deepStrictEqual(head([]), left(new Error('empty array')))
* assert.deepStrictEqual(head([1, 2, 3]), right(1))
*
* @since 2.0.0
*/
export function tryCatch(f, onError) {
try {
return right(f());
}
catch (e) {
return left(onError(e));
}
}
/**
* @since 2.0.0
*/
export function fold(onLeft, onRight) {
return function (ma) { return (isLeft(ma) ? onLeft(ma.left) : onRight(ma.right)); };
}
/**
* @since 2.0.0
*/
export function getShow(SE, SA) {
return {
show: function (ma) { return (isLeft(ma) ? "left(" + SE.show(ma.left) + ")" : "right(" + SA.show(ma.right) + ")"); }
};
}
/**
* @since 2.0.0
*/
export function getEq(EL, EA) {
return {
equals: function (x, y) {
return x === y || (isLeft(x) ? isLeft(y) && EL.equals(x.left, y.left) : isRight(y) && EA.equals(x.right, y.right));
}
};
}
/**
* Semigroup returning the left-most non-`Left` value. If both operands are `Right`s then the inner values are
* appended using the provided `Semigroup`
*
* @example
* import { getSemigroup, left, right } from 'fp-ts/lib/Either'
* import { semigroupSum } from 'fp-ts/lib/Semigroup'
*
* const S = getSemigroup<string, number>(semigroupSum)
* assert.deepStrictEqual(S.concat(left('a'), left('b')), left('a'))
* assert.deepStrictEqual(S.concat(left('a'), right(2)), right(2))
* assert.deepStrictEqual(S.concat(right(1), left('b')), right(1))
* assert.deepStrictEqual(S.concat(right(1), right(2)), right(3))
*
*
* @since 2.0.0
*/
export function getSemigroup(S) {
return {
concat: function (x, y) { return (isLeft(y) ? x : isLeft(x) ? y : right(S.concat(x.right, y.right))); }
};
}
/**
* `Apply` semigroup
*
* @example
* import { getApplySemigroup, left, right } from 'fp-ts/lib/Either'
* import { semigroupSum } from 'fp-ts/lib/Semigroup'
*
* const S = getApplySemigroup<string, number>(semigroupSum)
* assert.deepStrictEqual(S.concat(left('a'), left('b')), left('a'))
* assert.deepStrictEqual(S.concat(left('a'), right(2)), left('a'))
* assert.deepStrictEqual(S.concat(right(1), left('b')), left('b'))
* assert.deepStrictEqual(S.concat(right(1), right(2)), right(3))
*
*
* @since 2.0.0
*/
export function getApplySemigroup(S) {
return {
concat: function (x, y) { return (isLeft(x) ? x : isLeft(y) ? y : right(S.concat(x.right, y.right))); }
};
}
/**
* @since 2.0.0
*/
export function getApplyMonoid(M) {
return __assign(__assign({}, getApplySemigroup(M)), { empty: right(M.empty) });
}
/**
* Returns `true` if the either is an instance of `Left`, `false` otherwise
*
* @since 2.0.0
*/
export function isLeft(ma) {
switch (ma._tag) {
case 'Left':
return true;
case 'Right':
return false;
}
}
/**
* Returns `true` if the either is an instance of `Right`, `false` otherwise
*
* @since 2.0.0
*/
export function isRight(ma) {
return isLeft(ma) ? false : true;
}
/**
* @since 2.0.0
*/
export function swap(ma) {
return isLeft(ma) ? right(ma.left) : left(ma.right);
}
/**
* @since 2.0.0
*/
export function orElse(onLeft) {
return function (ma) { return (isLeft(ma) ? onLeft(ma.left) : ma); };
}
/**
* @since 2.0.0
*/
export function getOrElse(onLeft) {
return function (ma) { return (isLeft(ma) ? onLeft(ma.left) : ma.right); };
}
/**
* @since 2.0.0
*/
export function elem(E) {
return function (a, ma) { return (isLeft(ma) ? false : E.equals(a, ma.right)); };
}
/**
* Returns `false` if `Left` or returns the result of the application of the given predicate to the `Right` value.
*
* @example
* import { exists, left, right } from 'fp-ts/lib/Either'
*
* const gt2 = exists((n: number) => n > 2)
*
* assert.strictEqual(gt2(left('a')), false)
* assert.strictEqual(gt2(right(1)), false)
* assert.strictEqual(gt2(right(3)), true)
*
* @since 2.0.0
*/
export function exists(predicate) {
return function (ma) { return (isLeft(ma) ? false : predicate(ma.right)); };
}
/**
* Converts a JavaScript Object Notation (JSON) string into an object.
*
* @example
* import { parseJSON, toError, right, left } from 'fp-ts/lib/Either'
*
* assert.deepStrictEqual(parseJSON('{"a":1}', toError), right({ a: 1 }))
* assert.deepStrictEqual(parseJSON('{"a":}', toError), left(new SyntaxError('Unexpected token } in JSON at position 5')))
*
* @since 2.0.0
*/
export function parseJSON(s, onError) {
return tryCatch(function () { return JSON.parse(s); }, onError);
}
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
*
* @example
* import { stringifyJSON, toError, right, left } from 'fp-ts/lib/Either'
*
* assert.deepStrictEqual(stringifyJSON({ a: 1 }, toError), right('{"a":1}'))
* const circular: any = { ref: null }
* circular.ref = circular
* assert.deepStrictEqual(stringifyJSON(circular, toError), left(new TypeError('Converting circular structure to JSON')))
*
* @since 2.0.0
*/
export function stringifyJSON(u, onError) {
return tryCatch(function () { return JSON.stringify(u); }, onError);
}
var phantom = undefined;
/**
* Builds `Witherable` instance for `Either` given `Monoid` for the left side
*
* @since 2.0.0
*/
export function getWitherable(M) {
var empty = left(M.empty);
var compact = function (ma) {
return isLeft(ma) ? ma : ma.right._tag === 'None' ? left(M.empty) : right(ma.right.value);
};
var separate = function (ma) {
return isLeft(ma)
? { left: ma, right: ma }
: isLeft(ma.right)
? { left: right(ma.right.left), right: empty }
: { left: empty, right: right(ma.right.right) };
};
var partitionMap = function (ma, f) {
if (isLeft(ma)) {
return { left: ma, right: ma };
}
var e = f(ma.right);
return isLeft(e) ? { left: right(e.left), right: empty } : { left: empty, right: right(e.right) };
};
var partition = function (ma, p) {
return isLeft(ma)
? { left: ma, right: ma }
: p(ma.right)
? { left: empty, right: right(ma.right) }
: { left: right(ma.right), right: empty };
};
var filterMap = function (ma, f) {
if (isLeft(ma)) {
return ma;
}
var ob = f(ma.right);
return ob._tag === 'None' ? left(M.empty) : right(ob.value);
};
var filter = function (ma, predicate) {
return isLeft(ma) ? ma : predicate(ma.right) ? ma : left(M.empty);
};
var wither = function (F) {
var traverseF = either.traverse(F);
return function (ma, f) { return F.map(traverseF(ma, f), compact); };
};
var wilt = function (F) {
var traverseF = either.traverse(F);
return function (ma, f) { return F.map(traverseF(ma, f), separate); };
};
return {
URI: URI,
_E: phantom,
map: either.map,
compact: compact,
separate: separate,
filter: filter,
filterMap: filterMap,
partition: partition,
partitionMap: partitionMap,
traverse: either.traverse,
sequence: either.sequence,
reduce: either.reduce,
foldMap: either.foldMap,
reduceRight: either.reduceRight,
wither: wither,
wilt: wilt
};
}
/**
* @since 2.0.0
*/
export function getValidation(S) {
return {
URI: URI,
_E: phantom,
map: either.map,
of: either.of,
ap: function (mab, ma) {
return isLeft(mab)
? isLeft(ma)
? left(S.concat(mab.left, ma.left))
: mab
: isLeft(ma)
? ma
: right(mab.right(ma.right));
},
chain: either.chain,
alt: function (fx, f) {
if (isRight(fx)) {
return fx;
}
var fy = f();
return isLeft(fy) ? left(S.concat(fx.left, fy.left)) : fy;
}
};
}
/**
* @since 2.0.0
*/
export function getValidationSemigroup(SE, SA) {
return {
concat: function (fx, fy) {
return isLeft(fx)
? isLeft(fy)
? left(SE.concat(fx.left, fy.left))
: fx
: isLeft(fy)
? fy
: right(SA.concat(fx.right, fy.right));
}
};
}
/**
* @since 2.0.0
*/
export function getValidationMonoid(SE, SA) {
return {
concat: getValidationSemigroup(SE, SA).concat,
empty: right(SA.empty)
};
}
/**
* @since 2.0.0
*/
export var either = {
URI: URI,
map: function (ma, f) { return (isLeft(ma) ? ma : right(f(ma.right))); },
of: right,
ap: function (mab, ma) { return (isLeft(mab) ? mab : isLeft(ma) ? ma : right(mab.right(ma.right))); },
chain: function (ma, f) { return (isLeft(ma) ? ma : f(ma.right)); },
reduce: function (fa, b, f) { return (isLeft(fa) ? b : f(b, fa.right)); },
foldMap: function (M) { return function (fa, f) { return (isLeft(fa) ? M.empty : f(fa.right)); }; },
reduceRight: function (fa, b, f) { return (isLeft(fa) ? b : f(fa.right, b)); },
traverse: function (F) { return function (ma, f) {
return isLeft(ma) ? F.of(left(ma.left)) : F.map(f(ma.right), right);
}; },
sequence: function (F) { return function (ma) {
return isLeft(ma) ? F.of(left(ma.left)) : F.map(ma.right, right);
}; },
bimap: function (fea, f, g) { return (isLeft(fea) ? left(f(fea.left)) : right(g(fea.right))); },
mapLeft: function (fea, f) { return (isLeft(fea) ? left(f(fea.left)) : fea); },
alt: function (fx, fy) { return (isLeft(fx) ? fy() : fx); },
extend: function (wa, f) { return (isLeft(wa) ? wa : right(f(wa))); },
chainRec: function (a, f) {
return tailRec(f(a), function (e) {
return isLeft(e) ? right(left(e.left)) : isLeft(e.right) ? left(f(e.right.left)) : right(right(e.right.right));
});
},
throwError: left
};
var _a = pipeable(either), alt = _a.alt, ap = _a.ap, apFirst = _a.apFirst, apSecond = _a.apSecond, bimap = _a.bimap, chain = _a.chain, chainFirst = _a.chainFirst, duplicate = _a.duplicate, extend = _a.extend, flatten = _a.flatten, foldMap = _a.foldMap, map = _a.map, mapLeft = _a.mapLeft, reduce = _a.reduce, reduceRight = _a.reduceRight, fromOption = _a.fromOption, fromPredicate = _a.fromPredicate, filterOrElse = _a.filterOrElse;
export {
/**
* @since 2.0.0
*/
alt,
/**
* @since 2.0.0
*/
ap,
/**
* @since 2.0.0
*/
apFirst,
/**
* @since 2.0.0
*/
apSecond,
/**
* @since 2.0.0
*/
bimap,
/**
* @since 2.0.0
*/
chain,
/**
* @since 2.0.0
*/
chainFirst,
/**
* @since 2.0.0
*/
duplicate,
/**
* @since 2.0.0
*/
extend,
/**
* @since 2.0.0
*/
flatten,
/**
* @since 2.0.0
*/
foldMap,
/**
* @since 2.0.0
*/
map,
/**
* @since 2.0.0
*/
mapLeft,
/**
* @since 2.0.0
*/
reduce,
/**
* @since 2.0.0
*/
reduceRight,
/**
* @since 2.0.0
*/
fromOption,
/**
* @since 2.0.0
*/
fromPredicate,
/**
* @since 2.0.0
*/
filterOrElse };
Выполнить команду
Для локальной разработки. Не используйте в интернете!