import Assert from '@cafetextual/util/dist/src/assert/Assert'; import { int } from '@cafetextual/nlist/dist/src/ntree/types'; export default class Show { static show(p:any, o:any, prefix:string = "", root:boolean = true):string { var out:string = "" if (typeof p == 'string') { var name:string = p as string; var v:any = o[name]; var vstr:string = Show.serialize(v, "", false); out += prefix + name + ": " + vstr + "\n"; } else if (p instanceof Array) { if (root) { // param is simple list of proerties ["p1", "p2"...] var params:Array = p as Array var n:any for (n of params) { out += Show.show(n, o, prefix + " ", false ); } } else { // param is name of composite object properties, ["property", ["p1", "p2", ... ]] name = p[0] as string; // name with wich to get object params = p[1] as Array v = o[name] out += prefix + name + ": \n" out += Show.show(params, v, prefix + " ", true); } } return out; } // basic static serialize(v:any, prefix:string = "", allowJSON:boolean = true):string { if (v == null) { return "<< null >>"; } else if ((typeof v == 'number') ) { return "(Number) \"" + v.toString() + "\""; } else if (typeof v == 'string') { return "(String) " + v; } else if (typeof v == 'boolean') { return "(Boolean) " + ((v == true) ? "true" : "false") } else if (typeof v == 'object') { var type:string = Show.type(v); var out:string = ""; try { if ( (v as any).hasOwnProperty("show") && ( typeof ((v as any)["show"]) == 'function')) { out = v.show(); } else if ((v as Object).hasOwnProperty("toString") ) { out = v.toString(); } else { out = "not serialized"; //JSON.stringify(v); } } catch (e) { out = "<<< unable to get a text representation >>>"; } return type + "\n" + out; } else if (typeof v == 'function') { return "(function) \n" + (v as Function).toString(); // TODO - could introspect here } return "<<< unknown >>>"; } // valueToString static type(o:Object):string { /* var type:string = getQualifiedClassName(o); if (type.indexOf("::") > 0) { type = type.substr(type.indexOf("::") + 2, type.length); } return type;*/ return typeof o; // <--- can' really do this in js (though see ITypeManifest typeOf) } /** * A mostly deprecated as3 function - much less useful in js * */ static serializeSimpleObject(o:Object, prefix:string, depth:int = 10):string { if (depth < 0) { return "..."; } if (o == null) { return ""; } else if (Show.isLeaf(o)) { return Show.serialize(o); } var cls:string = Show.type(o); var out:string = "(" + cls + ")\n"; var ar:Array = o instanceof Array ? o as Array: null if (ar) { for (var i:int = 0; i < ar.length; i++) { var item:any = ar[i] out += prefix + "- " + Show.serializeSimpleObject(item, prefix +" ", depth-1) + "\n" } } var fn:Function = typeof o == 'function' ? o as Function : null if (fn) { return "(Function)"; } if (typeof o == "object") { var key:string for (key in o) { var v:any = (o as any)[key]; out += prefix + key + ": " + Show.serializeSimpleObject(v, " ", depth-1) + "\n"; } } else { Assert.fail() /* var type:Type = Type.forInstance(o); for each(var p:Variable in type.variables) { key = p.name var v:* = o[key] out += prefix + key + ": " + serializeSimpleObject(v, prefix + " ", depth-1) + "\n"; } */ } return out; } static isLeaf(o:any):boolean { var type:string = Show.type(o) return (o == 'string' || o == 'number' || o == 'boolean'); } } // class