import { isObject } from "./is_object" ; const preTag = "isInstance:" ; export function isInstance ( val , ctorOrTag , scope ? ) { if ( ! val || ! isObject ( val ) ) return false ; const tag = typeof ctorOrTag === "string" ? ctorOrTag : ctorOrTag . name ; scope = scope ? scope + ":" : "" ; return val [ Symbol . for ( preTag + scope + tag ) as any ] === true ; } export function tagInstance ( val , tag ? , scope ? ) { if ( tag === undefined ) { if ( ! val . constructor . name || val . constructor . name === "anonymous" ) { throw new Error ( `Anonymous functions unsupported due to inability to ` + `distinguish them. Please specify an explicit tag instead.` ) ; } tag = val . constructor . name ; } scope = scope ? scope + ":" : "" ; ( val as any ) [ Symbol . for ( preTag + scope + tag ) ] = true ; } export function tagConstructor ( ctor , scope ? , tag ? ) { if ( ctor . prototype == null ) { throw new Error ( `tagConstructor cannot be used for function '${ ctor . name }' ` + `because it does not have a prototype.` ) ; } tagInstance ( ctor . prototype , tag , scope ) ; }