-
Notifications
You must be signed in to change notification settings - Fork 0
/
jdump.js
executable file
·177 lines (147 loc) · 4.73 KB
/
jdump.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
{
'0': {
name: 'John',
age: 15,
website: ''
},
'1': {
name: 'Mic Smith',
age: 23,
website: 'benben.cc'
}
}
---------------------------------------
| INDEX | NAME | AGE | WEBSITE |
---------------------------------------
| 0 | John | 15 | |
---------------------------------------
| 1 | Mic Smith | 23 | benben.cc |
---------------------------------------
*/
(function() {
var jdump, utils;
jdump = function() {
this.init.apply(this, arguments);
};
jdump.prototype = {
version: '0.1.0-dev',
init: function() {
this.meta = {};
this.output = '';
this.utils = utils;
},
lineLen: function() {
var len = 0, num = 0;
utils.each(this.meta, function(o, i, r) {
num++;
len += 2 + o;
});
len += num + 1;
return len;
},
genMeta: function(data) {
var that = this;
utils.each(data, function(o, i, r) {
utils.each(o, function(o, i, r) {
var len = utils.getLength(o);
var titleLen = utils.getLength(i);
len = len > titleLen ? len : titleLen;
if(!that.meta[i] || len > that.meta[i]) {
that.meta[i] = len;
}
});
});
},
print: function(data) {
var that = this;
var lineLen, lineStr;
var tmpStr;
var titleStr;
data = utils.listLike(data) ? data : [data];
that.init();
that.genMeta(data);
lineLen = that.lineLen();
lineStr = utils.fill('', lineLen, '-');
utils.each(data, function(o, i, r) {
if(!titleStr) {
that.echo(lineStr + '\r\n');
utils.each(o, function(o, i, r) {
tmpStr = '| ' + utils.fill(i, that.meta[i]) + ' ';
that.echo(tmpStr);
});
that.echo('|\r\n');
titleStr = true;
that.echo(lineStr + '\r\n');
}
utils.each(o, function(o, i, r) {
tmpStr = '| ' + utils.fill(o, that.meta[i]) + ' ';
that.echo(tmpStr);
});
that.echo('|\r\n');
});
that.echo(lineStr);
return that.output;
},
echo: function(str) {
this.output += str;
}
};
utils = {
isArray: Array.isArray || function(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
},
isObject: function(obj) {
return obj === Object(obj);
},
isPlainObject: function(obj) {
return this.isObject(obj) && obj.constructor === Object;
},
each: function(list, fn) {
if(this.isPlainObject(list)) {
for(var i in list) {
if(list.hasOwnProperty(i)) {
fn(list[i], i, list);
}
}
return;
}
if(!this.isArray(list)) {
return;
}
if(Array.prototype.forEach) {
list.forEach(fn);
return;
}
for(var i = 0; i < list.length; i++) {
fn(list[i], i, list);
}
},
getLength: function(str) {
str = String(str);
return str.match(/[^ -~]/g) == null ? str.length : str.length + str.match(/[^ -~]/g).length;
},
fill: function(str, len, char) {
var cur_len = this.getLength(str);
var fill_len = len - cur_len;
char = char || ' ';
if(this.getLength(str) < len) {
str += char;
str = arguments.callee.call(this, str, len, char);
}
return str;
},
listLike: function(data) {
var that = this;
var flag = true;
this.each(data, function(o, i, r) {
if(!that.isPlainObject(o)) {
flag = false;
}
});
return flag;
}
};
jdump = new jdump();
typeof(module) !== 'undefined' && module.exports ? module.exports = jdump : this.jdump = jdump;
})();