Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 55 additions & 21 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ module.exports = (function () {
var jsomeRef
, browserColors = []
, browserStyle = require('./browser-style')

, toString = Object.prototype.toString

function getType (value) {
var map = {
'[object Number]' : 'num'
, '[object String]' : 'str'
, '[object Boolean]' : 'bool'
, '[object RegExp]' : 'regex'
, '[object Function]' : 'func'
, '[object Symbol]' : 'symbol'
, '[object BigInt]' : 'bigint'
, 'null' : 'null'
, 'undefined' : 'undef'
}
Expand All @@ -34,9 +37,13 @@ module.exports = (function () {
}

function cleanArray (arr) {
return arr.filter(function (item) {
return getType(item) !== 'func';
});
var cleaned = new Array(arr.length);
for (var i = 0; i < arr.length; i++) {
if ((i in arr) && getType(arr[i]) !== 'func') {
cleaned[i] = arr[i];
}
}
return cleaned;
}

function generateLevel (level) {
Expand Down Expand Up @@ -65,11 +72,24 @@ module.exports = (function () {
}

function colorify (value, level) {
var color = jsomeRef.colors[getType(value)];
return generateLevel(level)
+ (getType(value) === 'str' ? colorifySpec('"', 'quot') : '')
+ useColorProvider('' + value, color)
+ (getType(value) === 'str' ? colorifySpec('"', 'quot') : '');
var type = getType(value);
var color = jsomeRef.colors[type];
var displayValue;
if (type === 'symbol') {
displayValue = value.description || '';
} else if (type === 'bigint') {
displayValue = value.toString() + 'n';
} else {
displayValue = '' + value;
}
return generateLevel(level)
+ (type === 'str' ? colorifySpec('"', 'quot') : '')
+ useColorProvider(displayValue, color)
+ (type === 'str' ? colorifySpec('"', 'quot') : '');
}

function colorifyHole (level) {
return generateLevel(level) + useColorProvider('<empty>', jsomeRef.colors['hole']);
}

function colorifySpec (char, type, level) {
Expand Down Expand Up @@ -98,6 +118,8 @@ module.exports = (function () {
} else {
return str;
}
} else if ((color[0] === '#') && (color.length === 7)) {
return chalk.hex(color)(str);
} else {
return chalk[color](str);
}
Expand Down Expand Up @@ -130,27 +152,39 @@ module.exports = (function () {

} else if (isArray(json)) {
json = cleanArray(json);

if (hasChild(json)) {

var result = json.map(function(item) {
return this.gen(item, level+1);
}.bind(this));


var result = [];
for (var i = 0; i < json.length; i++) {
if (i in json) {
result.push(this.gen(json[i], level+1));
} else {
result.push(colorifyHole(level+1));
}
}

colored.push(colorifySpec('[', 'brack', isChild ? 0 : level));;
colored.push(result.join(colorifySpec(', ', 'punc') + '\n' ));
colored.push(colorifySpec(']', 'brack', level));

} else {

var coloredArray = colorifySpec('[', 'brack', isChild ? 0 : level);
for (var key in json) {
coloredArray += colorify(json[key]) + (json.length-1>key ? colorifySpec(', ', 'punc') : '');
for (var i = 0; i < json.length; i++) {
if (i in json) {
coloredArray += colorify(json[i]);
} else {
coloredArray += colorifyHole(0);
}
if (i < json.length - 1) {
coloredArray += colorifySpec(', ', 'punc');
}
}
colored.push(coloredArray + colorifySpec(']', 'brack'));

}

} else {
return generateLevel(isChild ? 0 : level) + colorify(json);
}
Expand Down
Loading