1
0
mirror of https://github.com/chylex/objtree.git synced 2025-07-25 01:59:06 +02:00

Add support for being included as an npm module

This commit is contained in:
chylex 2017-07-24 11:46:13 +02:00
parent f1a6f08983
commit ab989f13f1
2 changed files with 197 additions and 176 deletions

View File

@ -1,4 +1,4 @@
`objtree` is a script that generates a text tree representation of an object. The script can be executed in a browser console. `objtree` is a script that generates a text tree representation of an object. The script can be executed in a browser console or imported as an `npm` module.
# How to Use # How to Use

View File

@ -1,20 +1,21 @@
const OBJTREE_OBJECT = 0; (function(){
const OBJTREE_UNKNOWN = 1; const OBJTREE_OBJECT = 0;
const OBJTREE_COMPLEX_FUNCTION = 2; const OBJTREE_UNKNOWN = 1;
const OBJTREE_SIMPLE_FUNCTION = 3; const OBJTREE_COMPLEX_FUNCTION = 2;
const OBJTREE_PRIMARRAY = 4; const OBJTREE_SIMPLE_FUNCTION = 3;
const OBJTREE_VARIABLE = 5; const OBJTREE_PRIMARRAY = 4;
const OBJTREE_TOODEEP = 6; const OBJTREE_VARIABLE = 5;
const OBJTREE_TOODEEP = 6;
const OBJTREE_NAMES = [ const OBJTREE_NAMES = [
"[obj]", "[???]", "[fun]", "[fun]", "[arr]", "[var]", "[!!!]" "[obj]", "[???]", "[fun]", "[fun]", "[arr]", "[var]", "[!!!]"
]; ];
var objtree = function(target, { var objtree = function(target, {
maxlevel = 10, maxlevel = 10,
grandparent = "", grandparent = "",
exclude = [] exclude = []
} = {}){ } = {}){
var excludeRules = exclude.map(rule => new RegExp(rule)); var excludeRules = exclude.map(rule => new RegExp(rule));
var getObjectDesc = function(obj){ var getObjectDesc = function(obj){
@ -178,6 +179,10 @@ var objtree = function(target, {
}, },
downloadText: function(filename){ downloadText: function(filename){
if (typeof window === "undefined"){
throw "objtree.downloadText is only supported in a browser";
}
let url = window.URL.createObjectURL(new Blob([obj.asText()], { "type": "octet/stream" })); let url = window.URL.createObjectURL(new Blob([obj.asText()], { "type": "octet/stream" }));
let ele = document.createElement("a"); let ele = document.createElement("a");
document.body.appendChild(ele); document.body.appendChild(ele);
@ -191,4 +196,20 @@ var objtree = function(target, {
}; };
return obj; return obj;
}; };
objtree.TYPE_OBJECT = OBJTREE_OBJECT;
objtree.TYPE_UNKNOWN = OBJTREE_UNKNOWN;
objtree.TYPE_COMPLEX_FUNCTION = OBJTREE_COMPLEX_FUNCTION;
objtree.TYPE_SIMPLE_FUNCTION = OBJTREE_SIMPLE_FUNCTION;
objtree.TYPE_PRIMARRAY = OBJTREE_PRIMARRAY;
objtree.TYPE_VARIABLE = OBJTREE_VARIABLE;
objtree.TYPE_TOODEEP = OBJTREE_TOODEEP;
if (typeof module !== "undefined" && typeof module.exports !== "undefined"){
module.exports = objtree;
}
else{
window.objtree = objtree;
}
})();