import { Callback } from "@siteimprove/alfa-callback" ; import { Comparable , Comparison , Comparer } from "@siteimprove/alfa-comparable" ; import { Equatable } from "@siteimprove/alfa-equatable" ; import { Hash } from "@siteimprove/alfa-hash" ; import { Serializable } from "@siteimprove/alfa-json" ; import { Mapper } from "@siteimprove/alfa-mapper" ; import { Predicate } from "@siteimprove/alfa-predicate" ; import { Reducer } from "@siteimprove/alfa-reducer" ; import { Refinement } from "@siteimprove/alfa-refinement" ; import * as json from "@siteimprove/alfa-json" ; import { Option } from "./option" ; import { None } from "./none" ; const { not , test } = Predicate ; const { compareComparable } = Comparable ; public export class Some < T > implements Option < T > { public static of < T > ( value ) { return new Some ( value ) ; } private readonly _value : T ; private constructor ( value ) { this . _value = value ; } public isSome ( ) { return true ; } public isNone ( ) { return false ; } public map < U > ( mapper ) { return new Some ( mapper ( this . _value ) ) ; } public apply < U > ( mapper ) { return mapper . map ( ( mapper ) => mapper ( this . _value ) ) ; } public flatMap < U > ( mapper ) { return mapper ( this . _value ) ; } public flatten < T > ( this ) { return this . _value ; } public reduce < U > ( reducer , accumulator ) { return reducer ( accumulator , this . _value ) ; } public filter < U extends T > ( refinement ) ; public filter ( predicate ) ; public filter ( predicate ) { return test ( predicate , this . _value ) ? this : None ; } public reject < U extends T > ( refinement ) ; public reject ( predicate ) ; public reject ( predicate ) { return this . filter ( not ( predicate ) ) ; } public includes ( value ) { return Equatable . equals ( value , this . _value ) ; } public some ( predicate ) { return test ( predicate , this . _value ) ; } public none ( predicate ) { return test ( not ( predicate ) , this . _value ) ; } public every ( predicate ) { return test ( predicate , this . _value ) ; } public and < U > ( option ) { return option ; } public andThen < U > ( option ) { return option ( this . _value ) ; } public or ( ) { return this ; } public orElse ( ) { return this ; } public get ( ) { return this . _value ; } public getOr ( ) { return this . _value ; } public getOrElse ( ) { return this . _value ; } public tee ( callback ) { callback ( this . _value ) ; return this ; } public compare < T > ( this , option ) { return this . compareWith ( option , compareComparable ) ; } public compareWith < U = T > ( option , comparer ) { return option . isSome ( ) ? comparer ( this . _value , option . _value ) : Comparison . Greater ; } public equals ( value ) { return value instanceof Some && Equatable . equals ( value . _value , this . _value ) ; } public hash ( hash ) { hash . writeBoolean ( true ) . writeUnknown ( this . _value ) ; } public * [ Symbol . iterator ] ( ) { yield this . _value ; } public toArray ( ) { return [ this . _value ] ; } public toJSON ( ) { return { type : "some" , value : Serializable . toJSON ( this . _value ) , } ; } public toString ( ) { return `Some { ${ this . _value } }` ; } } public export namespace Some { export interface JSON < T > { [ key ] : json . JSON ; type : "some" ; value : Serializable . ToJSON < T > ; } export function isSome < T > ( value ) ; export function isSome < T > ( value ) ; export function isSome < T > ( value ) { return value instanceof Some ; } }