/**
 * Copyright 2018 Twitter, Inc.
 * Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 */ import { foreachObject } from "./foreachObject" ; import { hasConstructor } from "./hasConstructor" ; import { invariant } from "./invariant" ; import { isEmptyObject } from "./isEmptyObject" ; import { mapObject } from "./mapObject" ; import { RenderedSpan } from "./RenderedSpan" ; export class SqrlObject { isSqrlObject = true ; getData ( ) { throw new Error ( "not implemented." ) ; } getBasicValue ( ) { throw new Error ( "not implemented." ) ; } tryGetFeatureType ( ) { return null ; } tryGetTimeMs ( ) { return null ; } tryGetOfClass ( cls ) { if ( this instanceof cls ) { return this ; } return null ; } getTimeMs ( ) { const timeMs = this . tryGetTimeMs ( ) ; invariant ( timeMs !== null , "SqrlObject tryGetTimeMs not implemented" ) ; return timeMs ; } render ( ) { return { class : [ "value" , "json" ] , text : JSON . stringify ( this . getData ( ) , undefined , 2 ) + "\n" , } ; } static isTruthy ( obj ) { if ( ! obj ) { return false ; } else if ( typeof obj !== "object" ) { return true ; } else if ( obj instanceof Buffer ) { return obj . length > 0 ; } else if ( Array . isArray ( obj ) ) { return obj . some ( ( v ) => v !== null ) ; } else if ( obj instanceof Set ) { return obj . size > 0 ; } else { return ! isEmptyObject ( obj ) ; } } static isBasic ( val ) { if ( val instanceof SqrlObject || val instanceof Buffer || val instanceof Set ) { return false ; } else if ( Array . isArray ( val ) ) { return val . every ( SqrlObject . isBasic ) ; } else if ( val !== null && typeof val === "object" ) { if ( hasConstructor ( val ) ) { return true ; } else { let basic = true ; foreachObject ( val , ( subValue ) => { basic = basic && SqrlObject . isBasic ( subValue ) ; } ) ; return basic ; } } else { return true ; } } static ensureBasicTypeValue ( value , expandData = false ) { if ( value === null ) { return { type : null , value : null , } ; } else { let type = "Any" ; if ( value instanceof SqrlObject ) { type = value . tryGetFeatureType ( ) || "Any" ; } return { type , value : SqrlObject . ensureBasic ( value , expandData ) , } ; } } static ensureBasic ( val , expandData ? ) { if ( SqrlObject . isBasic ( val ) ) { return val ; } if ( val instanceof Set ) { val = Array . from ( val ) ; } if ( val instanceof SqrlObject ) { if ( expandData ) { return SqrlObject . ensureBasic ( val . getData ( ) , expandData ) ; } else { return val . getBasicValue ( ) ; } } else if ( val instanceof Buffer ) { return val . toString ( "hex" ) ; } else if ( Array . isArray ( val ) ) { return val . map ( ( v ) => SqrlObject . ensureBasic ( v , expandData ) ) ; } else if ( typeof val === "object" && ! hasConstructor ( val ) ) { return mapObject ( val , ( v ) => SqrlObject . ensureBasic ( v , expandData ) ) ; } else { return val ; } } static ensureOfClass ( val , cls ) { invariant ( val instanceof SqrlObject , "Could not ensureOfClass non-SqrlObject:: %s" , val ) ; const result = val . tryGetOfClass ( cls ) ; invariant ( result , "Value was not of correct type for ensureOfClass" , val ) ; return result ; } }