import Context from '../ctx' ; export const isANumber = x => Number . isFinite ( x ) ; export const isNotANumber = x => ! isANumber ( x ) ; /**
 * Number
 */ export default class NumberContext < Maybe extends null | undefined | number = number > extends Context < number | Maybe > { public readonly name = 'Number' ; public minimum : number | null = null ; public maximum : number | null = null ; constructor ( optional = false , nullable = false ) { super ( optional , nullable ) ; this . push ( v => isNotANumber ( v ) ? new Error ( 'must-be-a-number' ) : true ) ; this . push ( v => this . minimum != null ? v < this . minimum ? new Error ( 'invalid-range' ) : true : true , 'min' ) ; this . push ( v => this . maximum != null ? v > this . maximum ? new Error ( 'invalid-range' ) : true : true , 'max' ) ; } param min param max public range ( min , max ) { this . min ( min ) ; this . max ( max ) ; return this ; } param threshold public min ( threshold ) { this . minimum = threshold ; return this ; } param threshold public max ( threshold ) { this . maximum = threshold ; return this ; } /**
     * このインスタンスの値が整数でなければエラーにします
     */ public int ( ) { this . push ( v => ! Number . isInteger ( v ) ? new Error ( 'must-be-an-intager' ) : true , 'int' ) ; return this ; } public makeOptional ( ) { return new NumberContext ( true , false ) ; } public makeNullable ( ) { return new NumberContext ( false , true ) ; } public makeOptionalNullable ( ) { return new NumberContext ( true , true ) ; } }