import { unknown } from "../.." ; import { Option } from "./option" ; declare const UnitId = "typeprops/lib/data/unit" ; declare module "typeprops" { interface TypeProps < T , Params > { [ UnitId ] : { infer : T extends Unit < infer L > ? [ L ] : never ; construct : Unit < Params [ 0 ] > ; } ; } } export type UnitLike < T > = { value : T ; } ; export class Unit < T > { static from < V > ( iterable ) { if ( iterable == undefined ) { return new Unit ( iterable ) ; } let iterator = ( iterable as any ) [ Symbol . iterator ] ; if ( typeof iterator === "function" || iterator . toString ( ) === "[object Function]" ) { let next = iterator . next ( ) ; return new Unit ( next . value ) ; } if ( "length" in ( iterable as any ) && 0 in ( iterable as any ) ) { return new Unit ( ( iterable as any ) [ 0 ] ) ; } if ( "value" in ( iterable as any ) ) { return new Unit ( ( iterable as any ) . value ) ; } return new Unit ( iterable as V ) ; } static isUnit < T > ( identity ) ; static isUnit ( identity ) { return identity instanceof Unit ; } value : T ; constructor ( value ) { this . value = value ; } map < U > ( transform ) { return new Unit ( transform ( this . value ) ) ; } chain < U > ( transform ) { return transform ( this . value ) ; } toOption ( ) { return Option . from ( this ) ; } * [ Symbol . iterator ] ( ) { yield this . value ; } } export namespace Unit { export const of = < T > ( a ) : Unit < T > => new Unit ( a ) ; export const map = < T , U > ( transform , unit ) : Unit < U > => unit . map ( transform ) ; export const chain = < T , U > ( transform , unit ) : Unit < U > => unit . chain ( transform ) ; }