1203 lines
29 KiB
JavaScript
1203 lines
29 KiB
JavaScript
import { getApplicativeMonoid } from './Applicative';
|
|
import { apFirst as apFirst_, apS as apS_, apSecond as apSecond_, getApplySemigroup as getApplySemigroup_ } from './Apply';
|
|
import { bind as bind_, chainFirst as chainFirst_ } from './Chain';
|
|
import { chainEitherK as chainEitherK_, fromEitherK as fromEitherK_, chainFirstEitherK as chainFirstEitherK_ } from './FromEither';
|
|
import { constNull, constUndefined, flow, identity, pipe } from './function';
|
|
import { let as let__, bindTo as bindTo_, flap as flap_ } from './Functor';
|
|
import * as _ from './internal';
|
|
import { not } from './Predicate';
|
|
import { first, last } from './Semigroup';
|
|
import { separated } from './Separated';
|
|
import { wiltDefault, witherDefault } from './Witherable';
|
|
import { guard as guard_ } from './Zero';
|
|
// -------------------------------------------------------------------------------------
|
|
// constructors
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* `None` doesn't have a constructor, instead you can use it directly as a value. Represents a missing value.
|
|
*
|
|
* @category constructors
|
|
* @since 2.0.0
|
|
*/
|
|
export var none = _.none;
|
|
/**
|
|
* Constructs a `Some`. Represents an optional value that exists.
|
|
*
|
|
* @category constructors
|
|
* @since 2.0.0
|
|
*/
|
|
export var some = _.some;
|
|
export function fromPredicate(predicate) {
|
|
return function (a) { return (predicate(a) ? some(a) : none); };
|
|
}
|
|
/**
|
|
* Returns the `Left` value of an `Either` if possible.
|
|
*
|
|
* @example
|
|
* import { getLeft, none, some } from 'fp-ts/Option'
|
|
* import { right, left } from 'fp-ts/Either'
|
|
*
|
|
* assert.deepStrictEqual(getLeft(right(1)), none)
|
|
* assert.deepStrictEqual(getLeft(left('a')), some('a'))
|
|
*
|
|
* @category constructors
|
|
* @since 2.0.0
|
|
*/
|
|
export var getLeft = function (ma) { return (ma._tag === 'Right' ? none : some(ma.left)); };
|
|
/**
|
|
* Returns the `Right` value of an `Either` if possible.
|
|
*
|
|
* @example
|
|
* import { getRight, none, some } from 'fp-ts/Option'
|
|
* import { right, left } from 'fp-ts/Either'
|
|
*
|
|
* assert.deepStrictEqual(getRight(right(1)), some(1))
|
|
* assert.deepStrictEqual(getRight(left('a')), none)
|
|
*
|
|
* @category constructors
|
|
* @since 2.0.0
|
|
*/
|
|
export var getRight = function (ma) { return (ma._tag === 'Left' ? none : some(ma.right)); };
|
|
var _map = function (fa, f) { return pipe(fa, map(f)); };
|
|
var _ap = function (fab, fa) { return pipe(fab, ap(fa)); };
|
|
var _chain = function (ma, f) { return pipe(ma, chain(f)); };
|
|
var _reduce = function (fa, b, f) { return pipe(fa, reduce(b, f)); };
|
|
var _foldMap = function (M) {
|
|
var foldMapM = foldMap(M);
|
|
return function (fa, f) { return pipe(fa, foldMapM(f)); };
|
|
};
|
|
var _reduceRight = function (fa, b, f) { return pipe(fa, reduceRight(b, f)); };
|
|
var _traverse = function (F) {
|
|
var traverseF = traverse(F);
|
|
return function (ta, f) { return pipe(ta, traverseF(f)); };
|
|
};
|
|
/* istanbul ignore next */
|
|
var _alt = function (fa, that) { return pipe(fa, alt(that)); };
|
|
var _filter = function (fa, predicate) { return pipe(fa, filter(predicate)); };
|
|
/* istanbul ignore next */
|
|
var _filterMap = function (fa, f) { return pipe(fa, filterMap(f)); };
|
|
/* istanbul ignore next */
|
|
var _extend = function (wa, f) { return pipe(wa, extend(f)); };
|
|
/* istanbul ignore next */
|
|
var _partition = function (fa, predicate) {
|
|
return pipe(fa, partition(predicate));
|
|
};
|
|
/* istanbul ignore next */
|
|
var _partitionMap = function (fa, f) { return pipe(fa, partitionMap(f)); };
|
|
/**
|
|
* @category type lambdas
|
|
* @since 2.0.0
|
|
*/
|
|
export var URI = 'Option';
|
|
/**
|
|
* @category instances
|
|
* @since 2.0.0
|
|
*/
|
|
export var getShow = function (S) { return ({
|
|
show: function (ma) { return (isNone(ma) ? 'none' : "some(".concat(S.show(ma.value), ")")); }
|
|
}); };
|
|
/**
|
|
* @example
|
|
* import { none, some, getEq } from 'fp-ts/Option'
|
|
* import * as N from 'fp-ts/number'
|
|
*
|
|
* const E = getEq(N.Eq)
|
|
* assert.strictEqual(E.equals(none, none), true)
|
|
* assert.strictEqual(E.equals(none, some(1)), false)
|
|
* assert.strictEqual(E.equals(some(1), none), false)
|
|
* assert.strictEqual(E.equals(some(1), some(2)), false)
|
|
* assert.strictEqual(E.equals(some(1), some(1)), true)
|
|
*
|
|
* @category instances
|
|
* @since 2.0.0
|
|
*/
|
|
export var getEq = function (E) { return ({
|
|
equals: function (x, y) { return x === y || (isNone(x) ? isNone(y) : isNone(y) ? false : E.equals(x.value, y.value)); }
|
|
}); };
|
|
/**
|
|
* The `Ord` instance allows `Option` values to be compared with
|
|
* `compare`, whenever there is an `Ord` instance for
|
|
* the type the `Option` contains.
|
|
*
|
|
* `None` is considered to be less than any `Some` value.
|
|
*
|
|
*
|
|
* @example
|
|
* import { none, some, getOrd } from 'fp-ts/Option'
|
|
* import * as N from 'fp-ts/number'
|
|
*
|
|
* const O = getOrd(N.Ord)
|
|
* assert.strictEqual(O.compare(none, none), 0)
|
|
* assert.strictEqual(O.compare(none, some(1)), -1)
|
|
* assert.strictEqual(O.compare(some(1), none), 1)
|
|
* assert.strictEqual(O.compare(some(1), some(2)), -1)
|
|
* assert.strictEqual(O.compare(some(1), some(1)), 0)
|
|
*
|
|
* @category instances
|
|
* @since 2.0.0
|
|
*/
|
|
export var getOrd = function (O) { return ({
|
|
equals: getEq(O).equals,
|
|
compare: function (x, y) { return (x === y ? 0 : isSome(x) ? (isSome(y) ? O.compare(x.value, y.value) : 1) : -1); }
|
|
}); };
|
|
/**
|
|
* Monoid returning the left-most non-`None` value. If both operands are `Some`s then the inner values are
|
|
* concatenated using the provided `Semigroup`
|
|
*
|
|
* | x | y | concat(x, y) |
|
|
* | ------- | ------- | ------------------ |
|
|
* | none | none | none |
|
|
* | some(a) | none | some(a) |
|
|
* | none | some(b) | some(b) |
|
|
* | some(a) | some(b) | some(concat(a, b)) |
|
|
*
|
|
* @example
|
|
* import { getMonoid, some, none } from 'fp-ts/Option'
|
|
* import { SemigroupSum } from 'fp-ts/number'
|
|
*
|
|
* const M = getMonoid(SemigroupSum)
|
|
* assert.deepStrictEqual(M.concat(none, none), none)
|
|
* assert.deepStrictEqual(M.concat(some(1), none), some(1))
|
|
* assert.deepStrictEqual(M.concat(none, some(1)), some(1))
|
|
* assert.deepStrictEqual(M.concat(some(1), some(2)), some(3))
|
|
*
|
|
* @category instances
|
|
* @since 2.0.0
|
|
*/
|
|
export var getMonoid = function (S) { return ({
|
|
concat: function (x, y) { return (isNone(x) ? y : isNone(y) ? x : some(S.concat(x.value, y.value))); },
|
|
empty: none
|
|
}); };
|
|
/**
|
|
* @category mapping
|
|
* @since 2.0.0
|
|
*/
|
|
export var map = function (f) { return function (fa) {
|
|
return isNone(fa) ? none : some(f(fa.value));
|
|
}; };
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var Functor = {
|
|
URI: URI,
|
|
map: _map
|
|
};
|
|
/**
|
|
* @category constructors
|
|
* @since 2.7.0
|
|
*/
|
|
export var of = some;
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var Pointed = {
|
|
URI: URI,
|
|
of: of
|
|
};
|
|
/**
|
|
* @since 2.0.0
|
|
*/
|
|
export var ap = function (fa) { return function (fab) {
|
|
return isNone(fab) ? none : isNone(fa) ? none : some(fab.value(fa.value));
|
|
}; };
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var Apply = {
|
|
URI: URI,
|
|
map: _map,
|
|
ap: _ap
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var Applicative = {
|
|
URI: URI,
|
|
map: _map,
|
|
ap: _ap,
|
|
of: of
|
|
};
|
|
/**
|
|
* Composes computations in sequence, using the return value of one computation to determine the next computation.
|
|
*
|
|
* @category sequencing
|
|
* @since 2.0.0
|
|
*/
|
|
export var chain = function (f) { return function (ma) {
|
|
return isNone(ma) ? none : f(ma.value);
|
|
}; };
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
export var Chain = {
|
|
URI: URI,
|
|
map: _map,
|
|
ap: _ap,
|
|
chain: _chain
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var Monad = {
|
|
URI: URI,
|
|
map: _map,
|
|
ap: _ap,
|
|
of: of,
|
|
chain: _chain
|
|
};
|
|
/**
|
|
* @category folding
|
|
* @since 2.0.0
|
|
*/
|
|
export var reduce = function (b, f) { return function (fa) {
|
|
return isNone(fa) ? b : f(b, fa.value);
|
|
}; };
|
|
/**
|
|
* @category folding
|
|
* @since 2.0.0
|
|
*/
|
|
export var foldMap = function (M) { return function (f) { return function (fa) {
|
|
return isNone(fa) ? M.empty : f(fa.value);
|
|
}; }; };
|
|
/**
|
|
* @category folding
|
|
* @since 2.0.0
|
|
*/
|
|
export var reduceRight = function (b, f) { return function (fa) {
|
|
return isNone(fa) ? b : f(fa.value, b);
|
|
}; };
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var Foldable = {
|
|
URI: URI,
|
|
reduce: _reduce,
|
|
foldMap: _foldMap,
|
|
reduceRight: _reduceRight
|
|
};
|
|
/**
|
|
* Less strict version of [`alt`](#alt).
|
|
*
|
|
* The `W` suffix (short for **W**idening) means that the return types will be merged.
|
|
*
|
|
* @category error handling
|
|
* @since 2.9.0
|
|
*/
|
|
export var altW = function (that) { return function (fa) {
|
|
return isNone(fa) ? that() : fa;
|
|
}; };
|
|
/**
|
|
* Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to
|
|
* types of kind `* -> *`.
|
|
*
|
|
* In case of `Option` returns the left-most non-`None` value.
|
|
*
|
|
* | x | y | pipe(x, alt(() => y) |
|
|
* | ------- | ------- | -------------------- |
|
|
* | none | none | none |
|
|
* | some(a) | none | some(a) |
|
|
* | none | some(b) | some(b) |
|
|
* | some(a) | some(b) | some(a) |
|
|
*
|
|
* @example
|
|
* import * as O from 'fp-ts/Option'
|
|
* import { pipe } from 'fp-ts/function'
|
|
*
|
|
* assert.deepStrictEqual(
|
|
* pipe(
|
|
* O.none,
|
|
* O.alt(() => O.none)
|
|
* ),
|
|
* O.none
|
|
* )
|
|
* assert.deepStrictEqual(
|
|
* pipe(
|
|
* O.some('a'),
|
|
* O.alt<string>(() => O.none)
|
|
* ),
|
|
* O.some('a')
|
|
* )
|
|
* assert.deepStrictEqual(
|
|
* pipe(
|
|
* O.none,
|
|
* O.alt(() => O.some('b'))
|
|
* ),
|
|
* O.some('b')
|
|
* )
|
|
* assert.deepStrictEqual(
|
|
* pipe(
|
|
* O.some('a'),
|
|
* O.alt(() => O.some('b'))
|
|
* ),
|
|
* O.some('a')
|
|
* )
|
|
*
|
|
* @category error handling
|
|
* @since 2.0.0
|
|
*/
|
|
export var alt = altW;
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var Alt = {
|
|
URI: URI,
|
|
map: _map,
|
|
alt: _alt
|
|
};
|
|
/**
|
|
* @since 2.7.0
|
|
*/
|
|
export var zero = function () { return none; };
|
|
/**
|
|
* @category instances
|
|
* @since 2.11.0
|
|
*/
|
|
export var Zero = {
|
|
URI: URI,
|
|
zero: zero
|
|
};
|
|
/**
|
|
* @category do notation
|
|
* @since 2.11.0
|
|
*/
|
|
export var guard = /*#__PURE__*/ guard_(Zero, Pointed);
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var Alternative = {
|
|
URI: URI,
|
|
map: _map,
|
|
ap: _ap,
|
|
of: of,
|
|
alt: _alt,
|
|
zero: zero
|
|
};
|
|
/**
|
|
* @since 2.0.0
|
|
*/
|
|
export var extend = function (f) { return function (wa) {
|
|
return isNone(wa) ? none : some(f(wa));
|
|
}; };
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var Extend = {
|
|
URI: URI,
|
|
map: _map,
|
|
extend: _extend
|
|
};
|
|
/**
|
|
* @category filtering
|
|
* @since 2.0.0
|
|
*/
|
|
export var compact = /*#__PURE__*/ chain(identity);
|
|
var defaultSeparated = /*#__PURE__*/ separated(none, none);
|
|
/**
|
|
* @category filtering
|
|
* @since 2.0.0
|
|
*/
|
|
export var separate = function (ma) {
|
|
return isNone(ma) ? defaultSeparated : separated(getLeft(ma.value), getRight(ma.value));
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var Compactable = {
|
|
URI: URI,
|
|
compact: compact,
|
|
separate: separate
|
|
};
|
|
/**
|
|
* @category filtering
|
|
* @since 2.0.0
|
|
*/
|
|
export var filter = function (predicate) {
|
|
return function (fa) {
|
|
return isNone(fa) ? none : predicate(fa.value) ? fa : none;
|
|
};
|
|
};
|
|
/**
|
|
* @category filtering
|
|
* @since 2.0.0
|
|
*/
|
|
export var filterMap = function (f) { return function (fa) {
|
|
return isNone(fa) ? none : f(fa.value);
|
|
}; };
|
|
/**
|
|
* @category filtering
|
|
* @since 2.0.0
|
|
*/
|
|
export var partition = function (predicate) {
|
|
return function (fa) {
|
|
return separated(_filter(fa, not(predicate)), _filter(fa, predicate));
|
|
};
|
|
};
|
|
/**
|
|
* @category filtering
|
|
* @since 2.0.0
|
|
*/
|
|
export var partitionMap = function (f) { return flow(map(f), separate); };
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var Filterable = {
|
|
URI: URI,
|
|
map: _map,
|
|
compact: compact,
|
|
separate: separate,
|
|
filter: _filter,
|
|
filterMap: _filterMap,
|
|
partition: _partition,
|
|
partitionMap: _partitionMap
|
|
};
|
|
/**
|
|
* @category traversing
|
|
* @since 2.6.3
|
|
*/
|
|
export var traverse = function (F) {
|
|
return function (f) {
|
|
return function (ta) {
|
|
return isNone(ta) ? F.of(none) : F.map(f(ta.value), some);
|
|
};
|
|
};
|
|
};
|
|
/**
|
|
* @category traversing
|
|
* @since 2.6.3
|
|
*/
|
|
export var sequence = function (F) {
|
|
return function (ta) {
|
|
return isNone(ta) ? F.of(none) : F.map(ta.value, some);
|
|
};
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var Traversable = {
|
|
URI: URI,
|
|
map: _map,
|
|
reduce: _reduce,
|
|
foldMap: _foldMap,
|
|
reduceRight: _reduceRight,
|
|
traverse: _traverse,
|
|
sequence: sequence
|
|
};
|
|
var _wither = /*#__PURE__*/ witherDefault(Traversable, Compactable);
|
|
var _wilt = /*#__PURE__*/ wiltDefault(Traversable, Compactable);
|
|
/**
|
|
* @category filtering
|
|
* @since 2.6.5
|
|
*/
|
|
export var wither = function (F) {
|
|
var _witherF = _wither(F);
|
|
return function (f) { return function (fa) { return _witherF(fa, f); }; };
|
|
};
|
|
/**
|
|
* @category filtering
|
|
* @since 2.6.5
|
|
*/
|
|
export var wilt = function (F) {
|
|
var _wiltF = _wilt(F);
|
|
return function (f) { return function (fa) { return _wiltF(fa, f); }; };
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var Witherable = {
|
|
URI: URI,
|
|
map: _map,
|
|
reduce: _reduce,
|
|
foldMap: _foldMap,
|
|
reduceRight: _reduceRight,
|
|
traverse: _traverse,
|
|
sequence: sequence,
|
|
compact: compact,
|
|
separate: separate,
|
|
filter: _filter,
|
|
filterMap: _filterMap,
|
|
partition: _partition,
|
|
partitionMap: _partitionMap,
|
|
wither: _wither,
|
|
wilt: _wilt
|
|
};
|
|
/**
|
|
* @since 2.7.0
|
|
*/
|
|
export var throwError = function () { return none; };
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
export var MonadThrow = {
|
|
URI: URI,
|
|
map: _map,
|
|
ap: _ap,
|
|
of: of,
|
|
chain: _chain,
|
|
throwError: throwError
|
|
};
|
|
/**
|
|
* Transforms an `Either` to an `Option` discarding the error.
|
|
*
|
|
* Alias of [getRight](#getright)
|
|
*
|
|
* @category conversions
|
|
* @since 2.0.0
|
|
*/
|
|
export var fromEither = getRight;
|
|
/**
|
|
* @category instances
|
|
* @since 2.11.0
|
|
*/
|
|
export var FromEither = {
|
|
URI: URI,
|
|
fromEither: fromEither
|
|
};
|
|
// -------------------------------------------------------------------------------------
|
|
// refinements
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* Returns `true` if the option is an instance of `Some`, `false` otherwise.
|
|
*
|
|
* @example
|
|
* import { some, none, isSome } from 'fp-ts/Option'
|
|
*
|
|
* assert.strictEqual(isSome(some(1)), true)
|
|
* assert.strictEqual(isSome(none), false)
|
|
*
|
|
* @category refinements
|
|
* @since 2.0.0
|
|
*/
|
|
export var isSome = _.isSome;
|
|
/**
|
|
* Returns `true` if the option is `None`, `false` otherwise.
|
|
*
|
|
* @example
|
|
* import { some, none, isNone } from 'fp-ts/Option'
|
|
*
|
|
* assert.strictEqual(isNone(some(1)), false)
|
|
* assert.strictEqual(isNone(none), true)
|
|
*
|
|
* @category refinements
|
|
* @since 2.0.0
|
|
*/
|
|
export var isNone = function (fa) { return fa._tag === 'None'; };
|
|
/**
|
|
* Less strict version of [`match`](#match).
|
|
*
|
|
* The `W` suffix (short for **W**idening) means that the handler return types will be merged.
|
|
*
|
|
* @category pattern matching
|
|
* @since 2.10.0
|
|
*/
|
|
export var matchW = function (onNone, onSome) {
|
|
return function (ma) {
|
|
return isNone(ma) ? onNone() : onSome(ma.value);
|
|
};
|
|
};
|
|
/**
|
|
* Alias of [`matchW`](#matchw).
|
|
*
|
|
* @category pattern matching
|
|
* @since 2.10.0
|
|
*/
|
|
export var foldW = matchW;
|
|
/**
|
|
* Takes a (lazy) default value, a function, and an `Option` value, if the `Option` value is `None` the default value is
|
|
* returned, otherwise the function is applied to the value inside the `Some` and the result is returned.
|
|
*
|
|
* @example
|
|
* import { some, none, match } from 'fp-ts/Option'
|
|
* import { pipe } from 'fp-ts/function'
|
|
*
|
|
* assert.strictEqual(
|
|
* pipe(
|
|
* some(1),
|
|
* match(() => 'a none', a => `a some containing ${a}`)
|
|
* ),
|
|
* 'a some containing 1'
|
|
* )
|
|
*
|
|
* assert.strictEqual(
|
|
* pipe(
|
|
* none,
|
|
* match(() => 'a none', a => `a some containing ${a}`)
|
|
* ),
|
|
* 'a none'
|
|
* )
|
|
*
|
|
* @category pattern matching
|
|
* @since 2.10.0
|
|
*/
|
|
export var match = matchW;
|
|
/**
|
|
* Alias of [`match`](#match).
|
|
*
|
|
* @category pattern matching
|
|
* @since 2.0.0
|
|
*/
|
|
export var fold = match;
|
|
/**
|
|
* Less strict version of [`getOrElse`](#getorelse).
|
|
*
|
|
* The `W` suffix (short for **W**idening) means that the handler return type will be merged.
|
|
*
|
|
* @category error handling
|
|
* @since 2.6.0
|
|
*/
|
|
export var getOrElseW = function (onNone) {
|
|
return function (ma) {
|
|
return isNone(ma) ? onNone() : ma.value;
|
|
};
|
|
};
|
|
/**
|
|
* Extracts the value out of the structure, if it exists. Otherwise returns the given default value
|
|
*
|
|
* @example
|
|
* import { some, none, getOrElse } from 'fp-ts/Option'
|
|
* import { pipe } from 'fp-ts/function'
|
|
*
|
|
* assert.strictEqual(
|
|
* pipe(
|
|
* some(1),
|
|
* getOrElse(() => 0)
|
|
* ),
|
|
* 1
|
|
* )
|
|
* assert.strictEqual(
|
|
* pipe(
|
|
* none,
|
|
* getOrElse(() => 0)
|
|
* ),
|
|
* 0
|
|
* )
|
|
*
|
|
* @category error handling
|
|
* @since 2.0.0
|
|
*/
|
|
export var getOrElse = getOrElseW;
|
|
/**
|
|
* @category mapping
|
|
* @since 2.10.0
|
|
*/
|
|
export var flap = /*#__PURE__*/ flap_(Functor);
|
|
/**
|
|
* Combine two effectful actions, keeping only the result of the first.
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
export var apFirst = /*#__PURE__*/ apFirst_(Apply);
|
|
/**
|
|
* Combine two effectful actions, keeping only the result of the second.
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
export var apSecond = /*#__PURE__*/ apSecond_(Apply);
|
|
/**
|
|
* @category sequencing
|
|
* @since 2.0.0
|
|
*/
|
|
export var flatten = compact;
|
|
/**
|
|
* Composes computations in sequence, using the return value of one computation to determine the next computation and
|
|
* keeping only the result of the first.
|
|
*
|
|
* @category sequencing
|
|
* @since 2.0.0
|
|
*/
|
|
export var chainFirst =
|
|
/*#__PURE__*/ chainFirst_(Chain);
|
|
/**
|
|
* @since 2.0.0
|
|
*/
|
|
export var duplicate = /*#__PURE__*/ extend(identity);
|
|
/**
|
|
* @category lifting
|
|
* @since 2.11.0
|
|
*/
|
|
export var fromEitherK = /*#__PURE__*/ fromEitherK_(FromEither);
|
|
/**
|
|
* @category sequencing
|
|
* @since 2.11.0
|
|
*/
|
|
export var chainEitherK =
|
|
/*#__PURE__*/ chainEitherK_(FromEither, Chain);
|
|
/**
|
|
* @category sequencing
|
|
* @since 2.12.0
|
|
*/
|
|
export var chainFirstEitherK =
|
|
/*#__PURE__*/ chainFirstEitherK_(FromEither, Chain);
|
|
/**
|
|
* Constructs a new `Option` from a nullable type. If the value is `null` or `undefined`, returns `None`, otherwise
|
|
* returns the value wrapped in a `Some`.
|
|
*
|
|
* @example
|
|
* import { none, some, fromNullable } from 'fp-ts/Option'
|
|
*
|
|
* assert.deepStrictEqual(fromNullable(undefined), none)
|
|
* assert.deepStrictEqual(fromNullable(null), none)
|
|
* assert.deepStrictEqual(fromNullable(1), some(1))
|
|
*
|
|
* @category conversions
|
|
* @since 2.0.0
|
|
*/
|
|
export var fromNullable = function (a) { return (a == null ? none : some(a)); };
|
|
/**
|
|
* Transforms an exception into an `Option`. If `f` throws, returns `None`, otherwise returns the output wrapped in a
|
|
* `Some`.
|
|
*
|
|
* See also [`tryCatchK`](#trycatchk).
|
|
*
|
|
* @example
|
|
* import { none, some, tryCatch } from 'fp-ts/Option'
|
|
*
|
|
* assert.deepStrictEqual(
|
|
* tryCatch(() => {
|
|
* throw new Error()
|
|
* }),
|
|
* none
|
|
* )
|
|
* assert.deepStrictEqual(tryCatch(() => 1), some(1))
|
|
*
|
|
* @category interop
|
|
* @since 2.0.0
|
|
*/
|
|
export var tryCatch = function (f) {
|
|
try {
|
|
return some(f());
|
|
}
|
|
catch (e) {
|
|
return none;
|
|
}
|
|
};
|
|
/**
|
|
* Converts a function that may throw to one returning a `Option`.
|
|
*
|
|
* @category interop
|
|
* @since 2.10.0
|
|
*/
|
|
export var tryCatchK = function (f) {
|
|
return function () {
|
|
var a = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
a[_i] = arguments[_i];
|
|
}
|
|
return tryCatch(function () { return f.apply(void 0, a); });
|
|
};
|
|
};
|
|
/**
|
|
* Returns a *smart constructor* from a function that returns a nullable value.
|
|
*
|
|
* @example
|
|
* import { fromNullableK, none, some } from 'fp-ts/Option'
|
|
*
|
|
* const f = (s: string): number | undefined => {
|
|
* const n = parseFloat(s)
|
|
* return isNaN(n) ? undefined : n
|
|
* }
|
|
*
|
|
* const g = fromNullableK(f)
|
|
*
|
|
* assert.deepStrictEqual(g('1'), some(1))
|
|
* assert.deepStrictEqual(g('a'), none)
|
|
*
|
|
* @category lifting
|
|
* @since 2.9.0
|
|
*/
|
|
export var fromNullableK = function (f) { return flow(f, fromNullable); };
|
|
/**
|
|
* This is `chain` + `fromNullable`, useful when working with optional values.
|
|
*
|
|
* @example
|
|
* import { some, none, fromNullable, chainNullableK } from 'fp-ts/Option'
|
|
* import { pipe } from 'fp-ts/function'
|
|
*
|
|
* interface Employee {
|
|
* readonly company?: {
|
|
* readonly address?: {
|
|
* readonly street?: {
|
|
* readonly name?: string
|
|
* }
|
|
* }
|
|
* }
|
|
* }
|
|
*
|
|
* const employee1: Employee = { company: { address: { street: { name: 'high street' } } } }
|
|
*
|
|
* assert.deepStrictEqual(
|
|
* pipe(
|
|
* fromNullable(employee1.company),
|
|
* chainNullableK(company => company.address),
|
|
* chainNullableK(address => address.street),
|
|
* chainNullableK(street => street.name)
|
|
* ),
|
|
* some('high street')
|
|
* )
|
|
*
|
|
* const employee2: Employee = { company: { address: { street: {} } } }
|
|
*
|
|
* assert.deepStrictEqual(
|
|
* pipe(
|
|
* fromNullable(employee2.company),
|
|
* chainNullableK(company => company.address),
|
|
* chainNullableK(address => address.street),
|
|
* chainNullableK(street => street.name)
|
|
* ),
|
|
* none
|
|
* )
|
|
*
|
|
* @category sequencing
|
|
* @since 2.9.0
|
|
*/
|
|
export var chainNullableK = function (f) {
|
|
return function (ma) {
|
|
return isNone(ma) ? none : fromNullable(f(ma.value));
|
|
};
|
|
};
|
|
/**
|
|
* Extracts the value out of the structure, if it exists. Otherwise returns `null`.
|
|
*
|
|
* @example
|
|
* import { some, none, toNullable } from 'fp-ts/Option'
|
|
* import { pipe } from 'fp-ts/function'
|
|
*
|
|
* assert.strictEqual(
|
|
* pipe(
|
|
* some(1),
|
|
* toNullable
|
|
* ),
|
|
* 1
|
|
* )
|
|
* assert.strictEqual(
|
|
* pipe(
|
|
* none,
|
|
* toNullable
|
|
* ),
|
|
* null
|
|
* )
|
|
*
|
|
* @category conversions
|
|
* @since 2.0.0
|
|
*/
|
|
export var toNullable = /*#__PURE__*/ match(constNull, identity);
|
|
/**
|
|
* Extracts the value out of the structure, if it exists. Otherwise returns `undefined`.
|
|
*
|
|
* @example
|
|
* import { some, none, toUndefined } from 'fp-ts/Option'
|
|
* import { pipe } from 'fp-ts/function'
|
|
*
|
|
* assert.strictEqual(
|
|
* pipe(
|
|
* some(1),
|
|
* toUndefined
|
|
* ),
|
|
* 1
|
|
* )
|
|
* assert.strictEqual(
|
|
* pipe(
|
|
* none,
|
|
* toUndefined
|
|
* ),
|
|
* undefined
|
|
* )
|
|
*
|
|
* @category conversions
|
|
* @since 2.0.0
|
|
*/
|
|
export var toUndefined = /*#__PURE__*/ match(constUndefined, identity);
|
|
export function elem(E) {
|
|
return function (a, ma) {
|
|
if (ma === undefined) {
|
|
var elemE_1 = elem(E);
|
|
return function (ma) { return elemE_1(a, ma); };
|
|
}
|
|
return isNone(ma) ? false : E.equals(a, ma.value);
|
|
};
|
|
}
|
|
/**
|
|
* Returns `true` if the predicate is satisfied by the wrapped value
|
|
*
|
|
* @example
|
|
* import { some, none, exists } from 'fp-ts/Option'
|
|
* import { pipe } from 'fp-ts/function'
|
|
*
|
|
* assert.strictEqual(
|
|
* pipe(
|
|
* some(1),
|
|
* exists(n => n > 0)
|
|
* ),
|
|
* true
|
|
* )
|
|
* assert.strictEqual(
|
|
* pipe(
|
|
* some(1),
|
|
* exists(n => n > 1)
|
|
* ),
|
|
* false
|
|
* )
|
|
* assert.strictEqual(
|
|
* pipe(
|
|
* none,
|
|
* exists(n => n > 0)
|
|
* ),
|
|
* false
|
|
* )
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
export var exists = function (predicate) {
|
|
return function (ma) {
|
|
return isNone(ma) ? false : predicate(ma.value);
|
|
};
|
|
};
|
|
// -------------------------------------------------------------------------------------
|
|
// do notation
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* @category do notation
|
|
* @since 2.9.0
|
|
*/
|
|
export var Do = /*#__PURE__*/ of(_.emptyRecord);
|
|
/**
|
|
* @category do notation
|
|
* @since 2.8.0
|
|
*/
|
|
export var bindTo = /*#__PURE__*/ bindTo_(Functor);
|
|
var let_ = /*#__PURE__*/ let__(Functor);
|
|
export {
|
|
/**
|
|
* @category do notation
|
|
* @since 2.13.0
|
|
*/
|
|
let_ as let };
|
|
/**
|
|
* @category do notation
|
|
* @since 2.8.0
|
|
*/
|
|
export var bind = /*#__PURE__*/ bind_(Chain);
|
|
/**
|
|
* @category do notation
|
|
* @since 2.8.0
|
|
*/
|
|
export var apS = /*#__PURE__*/ apS_(Apply);
|
|
/**
|
|
* @since 2.11.0
|
|
*/
|
|
export var ApT = /*#__PURE__*/ of(_.emptyReadonlyArray);
|
|
// -------------------------------------------------------------------------------------
|
|
// array utils
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.11.0
|
|
*/
|
|
export var traverseReadonlyNonEmptyArrayWithIndex = function (f) {
|
|
return function (as) {
|
|
var o = f(0, _.head(as));
|
|
if (isNone(o)) {
|
|
return none;
|
|
}
|
|
var out = [o.value];
|
|
for (var i = 1; i < as.length; i++) {
|
|
var o_1 = f(i, as[i]);
|
|
if (isNone(o_1)) {
|
|
return none;
|
|
}
|
|
out.push(o_1.value);
|
|
}
|
|
return some(out);
|
|
};
|
|
};
|
|
/**
|
|
* Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.11.0
|
|
*/
|
|
export var traverseReadonlyArrayWithIndex = function (f) {
|
|
var g = traverseReadonlyNonEmptyArrayWithIndex(f);
|
|
return function (as) { return (_.isNonEmpty(as) ? g(as) : ApT); };
|
|
};
|
|
/**
|
|
* Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.9.0
|
|
*/
|
|
export var traverseArrayWithIndex = traverseReadonlyArrayWithIndex;
|
|
/**
|
|
* Equivalent to `ReadonlyArray#traverse(Applicative)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.9.0
|
|
*/
|
|
export var traverseArray = function (f) {
|
|
return traverseReadonlyArrayWithIndex(function (_, a) { return f(a); });
|
|
};
|
|
/**
|
|
* Equivalent to `ReadonlyArray#sequence(Applicative)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.9.0
|
|
*/
|
|
export var sequenceArray =
|
|
/*#__PURE__*/ traverseArray(identity);
|
|
// -------------------------------------------------------------------------------------
|
|
// deprecated
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* Use `Refinement` module instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export function getRefinement(getOption) {
|
|
return function (a) { return isSome(getOption(a)); };
|
|
}
|
|
/**
|
|
* Use [`chainNullableK`](#chainnullablek) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var mapNullable = chainNullableK;
|
|
/**
|
|
* This instance is deprecated, use small, specific instances instead.
|
|
* For example if a function needs a `Functor` instance, pass `O.Functor` instead of `O.option`
|
|
* (where `O` is from `import O from 'fp-ts/Option'`)
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var option = {
|
|
URI: URI,
|
|
map: _map,
|
|
of: of,
|
|
ap: _ap,
|
|
chain: _chain,
|
|
reduce: _reduce,
|
|
foldMap: _foldMap,
|
|
reduceRight: _reduceRight,
|
|
traverse: _traverse,
|
|
sequence: sequence,
|
|
zero: zero,
|
|
alt: _alt,
|
|
extend: _extend,
|
|
compact: compact,
|
|
separate: separate,
|
|
filter: _filter,
|
|
filterMap: _filterMap,
|
|
partition: _partition,
|
|
partitionMap: _partitionMap,
|
|
wither: _wither,
|
|
wilt: _wilt,
|
|
throwError: throwError
|
|
};
|
|
/**
|
|
* Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getApplySemigroup = /*#__PURE__*/ getApplySemigroup_(Apply);
|
|
/**
|
|
* Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getApplyMonoid = /*#__PURE__*/ getApplicativeMonoid(Applicative);
|
|
/**
|
|
* Use
|
|
*
|
|
* ```ts
|
|
* import { first } from 'fp-ts/Semigroup'
|
|
* import { getMonoid } from 'fp-ts/Option'
|
|
*
|
|
* getMonoid(first())
|
|
* ```
|
|
*
|
|
* instead.
|
|
*
|
|
* Monoid returning the left-most non-`None` value
|
|
*
|
|
* | x | y | concat(x, y) |
|
|
* | ------- | ------- | ------------ |
|
|
* | none | none | none |
|
|
* | some(a) | none | some(a) |
|
|
* | none | some(b) | some(b) |
|
|
* | some(a) | some(b) | some(a) |
|
|
*
|
|
* @example
|
|
* import { getFirstMonoid, some, none } from 'fp-ts/Option'
|
|
*
|
|
* const M = getFirstMonoid<number>()
|
|
* assert.deepStrictEqual(M.concat(none, none), none)
|
|
* assert.deepStrictEqual(M.concat(some(1), none), some(1))
|
|
* assert.deepStrictEqual(M.concat(none, some(2)), some(2))
|
|
* assert.deepStrictEqual(M.concat(some(1), some(2)), some(1))
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getFirstMonoid = function () { return getMonoid(first()); };
|
|
/**
|
|
* Use
|
|
*
|
|
* ```ts
|
|
* import { last } from 'fp-ts/Semigroup'
|
|
* import { getMonoid } from 'fp-ts/Option'
|
|
*
|
|
* getMonoid(last())
|
|
* ```
|
|
*
|
|
* instead.
|
|
*
|
|
* Monoid returning the right-most non-`None` value
|
|
*
|
|
* | x | y | concat(x, y) |
|
|
* | ------- | ------- | ------------ |
|
|
* | none | none | none |
|
|
* | some(a) | none | some(a) |
|
|
* | none | some(b) | some(b) |
|
|
* | some(a) | some(b) | some(b) |
|
|
*
|
|
* @example
|
|
* import { getLastMonoid, some, none } from 'fp-ts/Option'
|
|
*
|
|
* const M = getLastMonoid<number>()
|
|
* assert.deepStrictEqual(M.concat(none, none), none)
|
|
* assert.deepStrictEqual(M.concat(some(1), none), some(1))
|
|
* assert.deepStrictEqual(M.concat(none, some(2)), some(2))
|
|
* assert.deepStrictEqual(M.concat(some(1), some(2)), some(2))
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
export var getLastMonoid = function () { return getMonoid(last()); };
|