/** * Non-negative floating point branded newtype string. * * Represents non-negative floating point numbers: * * ```math * { f | f ∈ ℝ, f >= 0, f <= Number.MAX_VALUE } * ``` * * @since 0.0.2 */ 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 Str from 'fp-ts/string' import * as TD from 'io-ts/TaskDecoder' import * as t from 'io-ts/Type' import { pipe, unsafeCoerce } from 'fp-ts/function' import * as Arb from '../internal/ArbitraryBase' import * as NonNegativeFloat from '../number/NonNegativeFloat' /** * @since 0.0.2 * @internal */ interface NonNegativeFloatStringBrand { readonly NonNegativeFloatString: unique symbol } /** * Non-negative floating point branded newtype string. * * Represents non-negative floating point numbers: * * ```math * { f | f ∈ ℝ, f >= 0, f <= Number.MAX_VALUE } * ``` * * @since 0.0.2 * @category Model */ export type NonNegativeFloatString = string & NonNegativeFloatStringBrand /** * @since 0.0.2 * @category Model */ export type SchemableParams = HKT2 /** * @since 0.0.2 * @category Model */ export type SchemableParams1 = Kind /** * @since 0.0.2 * @category Model */ export type SchemableParams2 = Kind2 /** * @since 0.0.2 * @category Model */ export type SchemableParams2C = Kind2 /** * @since 0.0.2 * @category Refinements */ export const isNonNegativeFloatString = (s: string): s is NonNegativeFloatString => typeof s === 'string' && pipe(s, Number, NonNegativeFloat.isNonNegativeFloat) /** * @since 0.0.2 * @category Instances */ export const Decoder: SchemableParams2C = pipe( D.string, D.refine(isNonNegativeFloatString, 'NonNegativeFloatString') ) /** * @since 0.0.2 * @category Instances */ export const Eq: SchemableParams1 = Str.Eq /** * @since 0.0.2 * @category Instances */ export const Guard: SchemableParams1 = pipe( G.string, G.refine(isNonNegativeFloatString) ) /** * @since 0.0.2 * @category Instances */ export const TaskDecoder: SchemableParams2C = pipe( TD.string, TD.refine(isNonNegativeFloatString, 'NonNegativeFloatString') ) /** * @since 0.0.2 * @category Instances */ export const Type: SchemableParams1 = pipe( t.string, t.refine(isNonNegativeFloatString, 'NonNegativeFloatString') ) /** * @since 0.0.3 * @category Instances */ export const Encoder: SchemableParams2 = Enc.id() /** * @since 0.0.3 * @category Instances */ export const Arbitrary: SchemableParams1 = NonNegativeFloat.Arbitrary.map(f => f.toString() ) as Arb.Arbitrary /** * @since 0.0.2 * @category Utilities */ export const toNonNegativeFloat: ( s: NonNegativeFloatString ) => NonNegativeFloat.NonNegativeFloat = s => unsafeCoerce(Number(s))