fp-ts-std
Version:
The missing pseudo-standard library for fp-ts.
42 lines (41 loc) • 2.07 kB
JavaScript
import * as O from "fp-ts/Option";
import { and, not } from "fp-ts/Predicate";
import { flow, identity, pipe } from "fp-ts/function";
import * as A from "fp-ts/Array";
import { Bounded as BoundedInfinity } from "fp-ts/number";
import { unless } from "./Function";
import * as L from "./Lazy";
export const isValid = not(Number.isNaN);
export const fromStringWithRadix = (radix) => (string) => pipe(Number.parseInt(string, radix), O.fromPredicate(isValid));
export const fromString = flow(Number, O.fromPredicate(isValid));
export const floatFromString = flow(Number.parseFloat, O.fromPredicate(isValid));
export const integerFromString = fromStringWithRadix(10);
export const increment = x => x + 1;
export const decrement = x => x - 1;
export const add = (x) => y => x + y;
export const multiply = (x) => y => x * y;
export const subtract = (subtrahend) => minuend => minuend - subtrahend;
export const divide = (divisor) => dividend => dividend / divisor;
export const rem = (divisor) => dividend => dividend % divisor;
export const mod = (divisor) => dividend => ((dividend % divisor) + divisor) % divisor;
export const negate = n => -n;
export const isFinite = n => Math.abs(n) !== Number.POSITIVE_INFINITY;
export const toFinite = unless(isFinite)(n => Math.sign(n) * Number.MAX_SAFE_INTEGER);
export const isPositive = n => Math.sign(n) === 1;
export const isNegative = n => Math.sign(n) === -1;
export const isNonNegative = n => Math.sign(n) !== -1;
export const isNonPositive = n => Math.sign(n) !== 1;
export const BoundedSafe = {
...BoundedInfinity,
top: Number.MAX_SAFE_INTEGER,
bottom: Number.MIN_SAFE_INTEGER,
};
export const EnumInt = {
...BoundedSafe,
succ: flow(O.fromPredicate(and((n) => n < Number.MAX_SAFE_INTEGER)(Number.isInteger)), O.map(increment)),
pred: flow(O.fromPredicate(and((n) => n > Number.MIN_SAFE_INTEGER)(Number.isInteger)), O.map(decrement)),
toEnum: O.some,
fromEnum: identity,
cardinality: L.of(Number.POSITIVE_INFINITY),
};
export const digits = (n) => pipe([...`${n}`], A.filterMap(fromString));