import { ISO_LEFT , ISO_RIGHT , ISO_HORIZONTAL , ISO_VERTICAL , ISO_NONE , } from "../constants" ; import { ORIENTATION_NOT_SPECIFIED , } from "../errors" ; export default class Orientation { public static fromString ( orientationStr ) { const parseString = ( str ) => { switch ( str ) { case "vertical" : return new Orientation ( ISO_VERTICAL ) ; case "horizontal" : return new Orientation ( ISO_HORIZONTAL ) ; case "left" : return new Orientation ( ISO_LEFT ) ; case "right" : return new Orientation ( ISO_RIGHT ) ; default : throw new Error ( ORIENTATION_NOT_SPECIFIED ) ; } } ; const orientations = orientationStr . split ( "|" ) . map ( str => { let sanitizedStr = str . trim ( ) ; sanitizedStr = sanitizedStr . toLowerCase ( ) ; return parseString ( sanitizedStr ) ; } ) ; if ( orientations . length < 0 ) throw new Error ( ORIENTATION_NOT_SPECIFIED ) ; const result = new Orientation ( ISO_NONE ) ; for ( const orientation of orientations ) { result . merge ( orientation ) ; } return result ; } private _val : number ; constructor ( val ) { if ( val < 0 || val > 0b1111 ) throw Error ( ORIENTATION_NOT_SPECIFIED ) ; this . _val = val ; } get isLeft ( ) { return ( this . _val & ISO_LEFT ) > 0 ; } get isRight ( ) { return ( this . _val & ISO_RIGHT ) > 0 ; } get isHorizontal ( ) { return ( this . _val & ISO_HORIZONTAL ) > 0 ; } get isVertical ( ) { return ( this . _val & ISO_VERTICAL ) > 0 ; } get isSide ( ) { return ( this . _val & ( ISO_LEFT + ISO_RIGHT ) ) > 0 ; } get isNormal ( ) { return ( this . _val & ( ISO_HORIZONTAL + ISO_VERTICAL ) ) > 0 ; } get normal ( ) { return new Orientation ( this . _val & ( ISO_HORIZONTAL + ISO_VERTICAL ) ) ; } get side ( ) { return new Orientation ( this . _val & ( ISO_LEFT + ISO_RIGHT ) ) ; } public checkMutualExclusion ( ) { if ( ( this . isHorizontal === this . isVertical ) || ( this . isLeft === this . isRight ) ) { throw Error ( ORIENTATION_NOT_SPECIFIED ) ; } } public has ( other ) { return ( this . _val & other . _val ) > 0 ; } public match ( other ) { return this . _val === other . _val ; } public merge ( other ) { this . _val = this . _val | other . _val ; } }