@parischap/conversions
Version:
A functional library to replace partially the native Intl API
143 lines • 4.72 kB
JavaScript
/**
* This module implements a CVInteger brand, i.e. a number that represents an integer (Infinity, NaN
* disallowed). Can be used to represent a floor in a lift, a signed 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 CVInt from './internal/Int.js';
import * as CVReal from './Real.js';
/**
* Module tag
*
* @category Module markers
*/
export const moduleTag = '@parischap/conversions/Integer/';
/**
* Brand constructor. Should not be used directly
*
* @ignore
*/
export const constructor = /*#__PURE__*/Brand.all(CVReal.constructor, CVInt.constructor);
/**
* Constructs a `CVInteger` from a number without any verifications
*
* @category Constructors
*/
export const unsafeFromNumber = /*#__PURE__*/Brand.nominal();
/**
* Tries to construct a `CVInteger` 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 `CVnteger` 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 `CVInteger` from a number if possible. Throws otherwise
*
* @category Constructors
*/
export const fromNumberOrThrow = constructor;
/**
* Constructs a `CVInteger` from a `BigDecimal` without any checks
*
* @category Constructors
*/
export const unsafeFromBigDecimal = CVReal.unsafeFromBigDecimal;
/**
* Tries to construct a `CVInteger` from a `BigDecimal`. Returns a `Some` if the conversion can be
* performed, a `None` otherwise
*
* @category Constructors
*/
export const fromBigDecimalOption = /*#__PURE__*/flow(CVReal.fromBigDecimalOption, /*#__PURE__*/Option.flatMap(CVInt.fromNumberOption));
/**
* Tries to construct a `CVInteger` from a `BigDecimal`. Returns a `Right` if the conversion can be
* performed, a `Left` otherwise
*
* @category Constructors
*/
export const fromBigDecimal = /*#__PURE__*/flow(CVReal.fromBigDecimal, /*#__PURE__*/Either.flatMap(CVInt.fromNumber));
/**
* Constructs a `CVInteger` from a `BigDecimal` if possible. Throws otherwise
*
* @category Constructors
*/
export const fromBigDecimalOrThrow = /*#__PURE__*/flow(CVReal.fromBigDecimalOrThrow, CVInt.fromNumberOrThrow);
/**
* Constructs a `CVInteger` from a `BigInt` without any checks
*
* @category Constructors
*/
export const unsafeFromBigInt = CVReal.unsafeFromBigInt;
/**
* Tries to construct a `CVInteger` from a `BigInt`. Returns a `Some` if the conversion can be
* performed, a `None` otherwise
*
* @category Constructors
*/
export const fromBigIntOption = /*#__PURE__*/flow(CVReal.fromBigIntOption);
/**
* Tries to construct a `CVInteger` from a `BigInt`. Returns a `Right` if the conversion can be
* performed, a `Left` otherwise
*
* @category Constructors
*/
export const fromBigInt = /*#__PURE__*/flow(CVReal.fromBigInt, /*#__PURE__*/Either.flatMap(CVInt.fromNumber));
/**
* Constructs a `CVInteger` from a `BigInt` if possible. Throws otherwise
*
* @category Constructors
*/
export const fromBigIntOrThrow = /*#__PURE__*/flow(CVReal.fromBigIntOrThrow);
/**
* Constructs a `CVInteger` from a `CVReal` without any checks
*
* @category Constructors
*/
export const unsafeFromReal = Function.identity;
/**
* Tries to construct a `CVInteger` from a `CVReal`. Returns a `Some` if the conversion can be
* performed, a `None` otherwise
*
* @category Constructors
*/
export const fromRealOption = CVInt.fromNumberOption;
/**
* Tries to construct a `CVInteger` from a `CVReal`. Returns a `Right` if the conversion can be
* performed, a `Left` otherwise
*
* @category Constructors
*/
export const fromReal = CVInt.fromNumber;
/**
* Constructs a `CVInteger` from a `CVReal` if possible. Throws otherwise
*
* @category Constructors
*/
export const fromRealOrThrow = CVInt.fromNumberOrThrow;
/**
* A `Schema` that transforms a number into a `CVInteger`
*
* @ignore
*/
export const SchemaFromNumber = /*#__PURE__*/Schema.Number.pipe(/*#__PURE__*/Schema.fromBrand(constructor));
/**
* A `Schema` that represents a `CVInteger`
*
* @ignore
*/
export const SchemaFromSelf = /*#__PURE__*/Schema.typeSchema(SchemaFromNumber);
//# sourceMappingURL=Integer.js.map