/** * NonPositive integer branded newtype. * * Represents integers which are negative or zero. * * ```math * { z | z ∈ ℤ, z >= -2 ** 53 + 1, z <= 0 } * ``` * * @since 0.0.4 */ 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 N from 'fp-ts/number' import * as TD from 'io-ts/TaskDecoder' import * as t from 'io-ts/Type' import * as Arb from '../internal/ArbitraryBase' import { pipe } from 'fp-ts/function' import * as fc from 'fast-check' import { isInt } from './Int' /** * @since 0.0.4 * @internal */ interface NonPositiveIntBrand { readonly NonPositiveInt: unique symbol } /** * NonPositive integer branded newtype. * * Represents integers which are negative or zero. * * ```math * { z | z ∈ ℤ, z >= -2 ** 53 + 1, z <= 0 } * ``` * * @since 0.0.4 * @category Model */ export type NonPositiveInt = number & NonPositiveIntBrand /** * @since 0.0.4 * @category Model */ export type SchemableParams = HKT2 /** * @since 0.0.4 * @category Model */ export type SchemableParams1 = Kind /** * @since 0.0.4 * @category Model */ export type SchemableParams2 = Kind2 /** * @since 0.0.4 * @category Model */ export type SchemableParams2C = Kind2 /** * @since 0.0.4 * @category Refinements */ export const isNonPositiveInt = (n: number): n is NonPositiveInt => isInt(n) && n <= 0 /** * @since 0.0.4 * @category Instances */ export const Decoder: SchemableParams2C = pipe( D.number, D.refine(isNonPositiveInt, 'NonPositiveInt') ) /** * @since 0.0.4 * @category Instances */ export const Encoder: SchemableParams2 = Enc.id() /** * @since 0.0.4 * @category Instances */ export const Eq: SchemableParams1 = N.Eq /** * @since 0.0.4 * @category Instances */ export const Guard: SchemableParams1 = pipe(G.number, G.refine(isNonPositiveInt)) /** * @since 0.0.4 * @category Instances */ export const TaskDecoder: SchemableParams2C = pipe( TD.number, TD.refine(isNonPositiveInt, 'NonPositiveInt') ) /** * @since 0.0.4 * @category Instances */ export const Type: SchemableParams1 = pipe( t.number, t.refine(isNonPositiveInt, 'NonPositiveInt') ) /** * @since 0.0.4 * @category Instances */ export const Arbitrary: SchemableParams1 = fc .integer({ min: Number.MIN_SAFE_INTEGER, max: 0, }) .filter(isNonPositiveInt)