/** * Positive integer branded newtype string. * * Represents positive integers: * * ```math * { z | z ∈ ℤ, z > 0, z <= 2 ** 53 - 1 } * ``` * * @since 0.0.1 */ import { Kind, Kind2, URIS, URIS2, HKT2 } from 'fp-ts/HKT' import * as D from 'io-ts/Decoder' import * as Enc from 'io-ts/Encoder' import * as Eq_ from 'fp-ts/Eq' import * as G from 'io-ts/Guard' import * as TD from 'io-ts/TaskDecoder' import * as t from 'io-ts/Type' import * as Str from 'fp-ts/string' import { pipe, unsafeCoerce } from 'fp-ts/function' import * as Arb from '../internal/ArbitraryBase' import * as PositiveInt from '../number/PositiveInt' import { isIntString, toInt } from './IntString' /** * @since 0.0.1 * @internal */ interface PositiveIntStringBrand { readonly PositiveIntString: unique symbol } /** * Positive integer branded newtype. * * Represents positive integers: * * ```math * { z | z ∈ ℤ, z > 0, z <= 2 ** 53 - 1 } * ``` * * @since 0.0.1 * @category Model */ export type PositiveIntString = string & PositiveIntStringBrand /** * @since 0.0.1 * @category Model */ export type SchemableParams = HKT2 /** * @since 0.0.1 * @category Model */ export type SchemableParams1 = Kind /** * @since 0.0.3 * @category Model */ export type SchemableParams2 = Kind2 /** * @since 0.0.1 * @category Model */ export type SchemableParams2C = Kind2 /** * @since 0.0.1 * @category Refinements */ export function isPositiveIntString(n: string): n is PositiveIntString { return isIntString(n) && toInt(n) > 0 } /** * @since 0.0.1 * @category Instances */ export const Decoder: SchemableParams2C = pipe( D.string, D.refine(isPositiveIntString, 'PositiveIntString') ) /** * @since 0.0.1 * @category Instances */ export const Eq: SchemableParams1 = Str.Eq /** * @since 0.0.1 * @category Instances */ export const Guard: SchemableParams1 = pipe( G.string, G.refine(isPositiveIntString) ) /** * @since 0.0.1 * @category Instances */ export const TaskDecoder: SchemableParams2C = pipe( TD.string, TD.refine(isPositiveIntString, 'PositiveIntString') ) /** * @since 0.0.1 * @category Instances */ export const Type: SchemableParams1 = pipe( t.string, t.refine(isPositiveIntString, 'PositiveIntString') ) /** * @since 0.0.1 * @category Destructors */ export const toPositiveInt: (n: PositiveIntString) => PositiveInt.PositiveInt = n => unsafeCoerce(Number(n)) /** * @since 0.0.3 * @category Instances */ export const Encoder: SchemableParams2 = Enc.id() /** * @since 0.0.3 * @category Instances */ export const Arbitrary: SchemableParams1 = PositiveInt.Arbitrary.map(i => i.toString() ) as Arb.Arbitrary