/
tuna.js
110 lines (84 loc) · 3.14 KB
/
tuna.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*!
tuna.js
Tuna - Common sense JavaScript object introspection
Copyright(c) 2011 Victor Castell <victorcoder@gmail.com>
MIT Licensed
Public Domain
No warranty expressed or implied. Use at your own risk.
USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
NOT CONTROL.
Usage
Tuna.describe(object);
See example files for mode in depth usage.
You can use the other methods but they are not of much utility outside the
scope of the class.
Some code borrowed from:
https://github.com/ry/node/blob/master/lib/util.js
*/
// Wrap in a closure for hiding methods and avoid global variables.
var Tuna = (function() {
// For interenal use.
// Gets a JS object instance and returns it's type. It knows if it's
// a primitive JS type or a Object type.
function typify(object) {
var type = null;
//Check for primitive
var primitives = ["string", "number", "boolean", "null", "undefined"];
for (var i in primitives) {
if (typeof object == primitives[i]) {
type = primitives[i].charAt(0).toUpperCase() + primitives[i].substr(1);
break;
}
}
var coreObjects = ["Boolean", "Number", "String", "Array", "Function", "RegExp", "Date", "Object"];
if (type == null) {
for (var i in coreObjects) {
if(object instanceof eval(coreObjects[i])) {
type = coreObjects[i];
break;
}
}
}
return type;
}
// For internal use.
// Gets a function object as an input and tries to return
// the function notation.
function functionize(funk) {
var def = null;
def = funk.toString().match(/function\s\(.*\)/);
return (def != null) ? def[0].replace(/\s/, "") : funk.name;
}
return {
// Gets any JS object as input and traverse it's methods and
// properties to form a new object with an outline of the object
// structure.
describe: function(object) {
var description = {};
var members = [];
description.type = typify(object);
description.name =
(typeof object === "function") ? functionize(object) : typify(object);
for (var i in object) {
var def = null;
var name = null;
var member = {};
if (typeof object[i] === "function") {
def = functionize(object[i]);
}
member.type = typify(object[i]);
member.name = (def == null) ? i : def;
if (member.type === "Object") {
member.members = Tuna.describe(object[i]);
}
members.push(member);
}
description.members = members;
return description;
}
}
})();
try {
exports.Tuna = Tuna;
} catch(error) {
}