Skip to content

Commit 4cc91e4

Browse files
committed
Adding generator module
1 parent c105556 commit 4cc91e4

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

generator.js

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
var options;
2+
3+
module.exports = (function () {
4+
5+
function getType (value) {
6+
var map = {
7+
'[object Number]' : 'num'
8+
, '[object String]' : 'str'
9+
, '[object Boolean]' : 'bool'
10+
, '[object Function]' : 'func'
11+
, 'null' : 'null'
12+
, 'undefined' : 'undef'
13+
}
14+
15+
return map[toString.call(value)] || map[''+value];
16+
}
17+
18+
function repeat (str, times) {
19+
times = times || -1;
20+
return (new Array(times + 1)).join(str);
21+
}
22+
23+
function clearObject (obj) {
24+
var lastKey = '';
25+
for(var key in obj) {
26+
(getType(obj[key])==='func') && delete obj[key] || (lastKey = key);
27+
}
28+
return lastKey;
29+
}
30+
31+
function clearArray (arr) {
32+
return arr.filter(function (item) {
33+
return getType(item) !== 'func';
34+
});
35+
}
36+
37+
function generateLevel (level) {
38+
var levelStr = repeat(' ', options.level.spaces)
39+
, opts = options.level;
40+
41+
if(options.level.show && levelStr.length) {
42+
levelStr = levelStr.replace(' ', options.params.colored ? opts.char[opts.color] : opts.char);
43+
}
44+
45+
46+
return repeat(levelStr, level);
47+
}
48+
49+
function hasChild (obj) {
50+
for(var key in obj) {
51+
if(isArray(obj[key]) || isObject(obj[key])) return true;
52+
}
53+
}
54+
55+
function isArray (arr) {
56+
return toString.call(arr).match(/^\[object Array\]$/);
57+
}
58+
59+
function isObject (obj) {
60+
return toString.call(obj).match(/^\[object Object\]$/);
61+
}
62+
63+
function colorify (value, level) {
64+
var color = options.colors[getType(value)];
65+
return generateLevel(level)
66+
+ (getType(value) === 'str' ? (options.params.colored ? colorifySpec('"', 'quot') : '"') : '')
67+
+ (options.params.colored ? (( '' + value )[color]) : ('' + value))
68+
+ (getType(value) === 'str' ? (options.params.colored ? colorifySpec('"', 'quot') : '"') : '');
69+
}
70+
71+
function colorifySpec (char, type, level) {
72+
return generateLevel(level) + (options.params.colored ? char[options.colors[type]] : char);
73+
}
74+
75+
return {
76+
gen : function (json, level, isChild) {
77+
var colored = [];
78+
level = level || 0;
79+
80+
if (isObject(json)) {
81+
82+
var lastKey = clearObject(json);
83+
colored.push(colorifySpec('{', 'brack', isChild ? 0 : level));
84+
level++;
85+
86+
for(var key in json) {
87+
var result = colorifySpec(key, 'attr', level)
88+
+ colorifySpec(': ', 'punc')
89+
+ this.gen(json[key], level, true)
90+
+ (key !== lastKey ? colorifySpec(',', 'punc') : '');
91+
colored.push(result);
92+
}
93+
94+
colored.push(colorifySpec('}', 'brack', --level));
95+
96+
} else if(isArray(json)) {
97+
json = clearArray(json);
98+
99+
if(hasChild(json)) {
100+
101+
var result = json.map(function(item) {
102+
return this.gen(item, level+1);
103+
}.bind(this));
104+
105+
colored.push(colorifySpec('[', 'brack', isChild ? 0 : level));;
106+
colored.push(result.join(colorifySpec(', ', 'punc') + '\n' ));
107+
colored.push(colorifySpec(']', 'brack', level));
108+
109+
} else {
110+
111+
var coloredArray = colorifySpec('[', 'brack');
112+
for(var key in json) {
113+
coloredArray += [
114+
colorify(json[key])
115+
, json.length-1>key ? colorifySpec(', ', 'punc') : ''
116+
].join('');
117+
}
118+
colored.push(coloredArray + colorifySpec(']', 'brack'));
119+
120+
}
121+
122+
} else {
123+
return colorify(json);
124+
}
125+
126+
return colored.join('\n');
127+
},
128+
setOptions : function (opts) {
129+
options = opts;
130+
return this;
131+
}
132+
}
133+
134+
})();

0 commit comments

Comments
 (0)