UNPKG

@parischap/conversions

Version:

A functional library to replace partially the native Intl API

184 lines 6.66 kB
/** * This module implements a CVPositiveInteger brand, i.e. a number that represents an integer * greater than or equal to 0 (Infinity, NaN disallowed). Can be used to represent an age, a * quantity,... See the `Effect` documentation about Branding * (https://effect.website/docs/code-style/branded-types/) if you are not familiar with this * concept. */ import * as Brand from 'effect/Brand'; import * as Either from 'effect/Either'; import {flow} from 'effect/Function'; import * as Function from 'effect/Function'; import * as Option from 'effect/Option'; import * as Schema from 'effect/Schema'; import * as CVInteger from './Integer.js'; import * as CVPositive from './internal/Positive.js'; /** * Module tag * * @category Module markers */ export const moduleTag = '@parischap/conversions/PositiveInteger/'; /** * Brand constructor. Should not be used directly * * @ignore */ export const constructor = /*#__PURE__*/Brand.all(CVInteger.constructor, CVPositive.constructor); /** * Constructs a `CVPositiveInteger` from a number without any verifications * * @category Constructors */ export const unsafeFromNumber = /*#__PURE__*/Brand.nominal(); /** * Tries to construct a `CVPositiveInteger` from a number. Returns a `Some` if the conversion can be * performed, a `None` otherwise * * @category Constructors */ export const fromNumberOption = /*#__PURE__*/constructor.option.bind(constructor); /** * Tries to construct a `CVPositiveInteger` from a number. Returns a `Right` if the conversion can * be performed, a `Left` otherwise * * @category Constructors */ export const fromNumber = /*#__PURE__*/constructor.either.bind(constructor); /** * Constructs a `CVPositiveInteger` from a number if possible. Throws otherwise * * @category Constructors */ export const fromNumberOrThrow = constructor; /** * Constructs a `CVPositiveInteger` from a `BigDecimal` without any checks * * @category Constructors */ export const unsafeFromBigDecimal = CVInteger.unsafeFromBigDecimal; /** * Tries to construct a `CVPositiveInteger` from a `BigDecimal`. Returns a `Some` if the conversion * can be performed, a `None` otherwise * * @category Constructors */ export const fromBigDecimalOption = /*#__PURE__*/flow(CVInteger.fromBigDecimalOption, /*#__PURE__*/Option.flatMap(CVPositive.fromNumberOption)); /** * Tries to construct a `CVPositiveInteger` from a `BigDecimal`. Returns a `Right` if the conversion * can be performed, a `Left` otherwise * * @category Constructors */ export const fromBigDecimal = /*#__PURE__*/flow(CVInteger.fromBigDecimal, /*#__PURE__*/Either.flatMap(CVPositive.fromNumber)); /** * Constructs a `CVPositiveInteger` from a `BigDecimal` if possible. Throws otherwise * * @category Constructors */ export const fromBigDecimalOrThrow = /*#__PURE__*/flow(CVInteger.fromBigDecimalOrThrow, CVPositive.fromNumberOrThrow); /** * Constructs a `CVPositiveInteger` from a `BigInt` without any checks * * @category Constructors */ export const unsafeFromBigInt = CVInteger.unsafeFromBigInt; /** * Tries to construct a `CVPositiveInteger` from a `BigInt`. Returns a `Some` if the conversion can * be performed, a `None` otherwise * * @category Constructors */ export const fromBigIntOption = /*#__PURE__*/flow(CVInteger.fromBigIntOption, /*#__PURE__*/Option.flatMap(CVPositive.fromNumberOption)); /** * Tries to construct a `CVPositiveInteger` from a `BigInt`. Returns a `Right` if the conversion can * be performed, a `Left` otherwise * * @category Constructors */ export const fromBigInt = /*#__PURE__*/flow(CVInteger.fromBigInt, /*#__PURE__*/Either.flatMap(CVPositive.fromNumber)); /** * Constructs a `CVPositiveInteger` from a `BigInt` if possible. Throws otherwise * * @category Constructors */ export const fromBigIntOrThrow = /*#__PURE__*/flow(CVInteger.fromBigIntOrThrow, CVPositive.fromNumberOrThrow); /** * Constructs a `CVPositiveInteger` from a `CVReal` without any checks * * @category Constructors */ export const unsafeFromReal = Function.identity; /** * Tries to construct a `CVPositiveInteger` from a `CVReal`. Returns a `Some` if the conversion can * be performed, a `None` otherwise * * @category Constructors */ export const fromRealOption = /*#__PURE__*/flow(CVInteger.fromRealOption, /*#__PURE__*/Option.flatMap(CVPositive.fromNumberOption)); /** * Tries to construct a `CVPositiveInteger` from a `CVReal`. Returns a `Right` if the conversion can * be performed, a `Left` otherwise * * @category Constructors */ export const fromReal = /*#__PURE__*/flow(CVInteger.fromReal, /*#__PURE__*/Either.flatMap(CVPositive.fromNumber)); /** * Constructs a `CVPositiveInteger` from a `CVReal` if possible. Throws otherwise * * @category Constructors */ export const fromRealOrThrow = /*#__PURE__*/flow(CVInteger.fromRealOrThrow, CVPositive.fromNumberOrThrow); /** * Tries to construct a `CVPositiveInteger` from a `CVInteger`. Returns a `Some` if the conversion * can be performed, a `None` otherwise * * @category Constructors */ export const fromIntegerOption = /*#__PURE__*/flow(CVPositive.fromNumberOption); /** * Tries to construct a `CVPositiveInteger` from a `CVInteger`. Returns a `Right` if the conversion * can be performed, a `Left` otherwise * * @category Constructors */ export const fromInteger = CVPositive.fromNumber; /** * Constructs a `CVPositiveInteger` from a `CVInteger` if possible. Throws otherwise * * @category Constructors */ export const fromIntegerOrThrow = /*#__PURE__*/flow(CVPositive.fromNumberOrThrow); /** * Tries to construct a `CVPositiveInteger` from a `CVPositiveReal`. Returns a `Some` if the * conversion can be performed, a `None` otherwise * * @category Constructors */ export const fromPositiveRealOption = /*#__PURE__*/flow(CVInteger.fromRealOption); /** * Tries to construct a `CVPositiveInteger` from a `CVPositiveReal`. Returns a `Right` if the * conversion can be performed, a `Left` otherwise * * @category Constructors */ export const fromPositiveReal = CVInteger.fromReal; /** * Constructs a `CVPositiveInteger` from a `CVPositiveReal` if possible. Throws otherwise * * @category Constructors */ export const fromPositiveRealOrThrow = /*#__PURE__*/flow(CVInteger.fromRealOrThrow); /** * A `Schema` that transforms a number into a `CVPositiveInteger` * * @ignore */ export const SchemaFromNumber = /*#__PURE__*/Schema.Number.pipe(/*#__PURE__*/Schema.fromBrand(constructor)); /** * A Schema that represents a `CVPositiveInteger` * * @ignore */ export const SchemaFromSelf = /*#__PURE__*/Schema.typeSchema(SchemaFromNumber); //# sourceMappingURL=PositiveInteger.js.map