Last active
October 18, 2016 02:27
-
-
Save aNd1coder/9249464e91db630b617f97adfa49de6e to your computer and use it in GitHub Desktop.
Json object format for highlight.js and append field comment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from 'lodash' | |
export function jsonformat(body) { | |
let htmlBlock = '' | |
let space = ' ' | |
let index = 0 | |
let last = false | |
let toString = Object.prototype.toString | |
let self = this | |
body = JSON.parse(JSON.stringify(body)) | |
htmlBlock += build(body, 1) | |
function build(data, level) { | |
let _level = level + 1 | |
let html = '' | |
let isArrayData = Array.isArray(data) | |
for (let attr in data) { | |
let value = data[attr] | |
let valueType = getType(value) | |
if (valueType === 'array') { | |
let htmlArray = '' | |
htmlArray += buildAttr(attr, level) + ': [' | |
if (value.length === 0) { | |
htmlArray += '],' + buildComment(attr) + '\n' | |
} else { | |
htmlArray += buildComment(attr) + '\n' | |
htmlArray += build(value, _level) | |
htmlArray = _.trimEnd(htmlArray, '\n') | |
htmlArray = _.trimEnd(htmlArray, ',') | |
htmlArray += buildEndChar(']', level) | |
} | |
html += htmlArray | |
} else if (valueType === 'object') { | |
let htmlObject = '' | |
let empty = Object.keys(value).length === 0 | |
htmlObject += isArrayData ? (padding(level) + '{') : (buildAttr(attr, level) + ': {') | |
if (empty) { | |
htmlObject += ',' + buildComment(attr) + '\n' | |
} else { | |
htmlObject += buildComment(attr) + '\n' | |
htmlObject += build(value, _level) | |
htmlObject = _.trimEnd(htmlObject, '\n') | |
htmlObject = _.trimEnd(htmlObject, ',') | |
htmlObject += buildEndChar('}', level) | |
} | |
html += htmlObject | |
} else { | |
html += (isArrayData ? padding(level) : (buildAttr(attr, level) + ': ')) + buildValue(valueType, value) + ',' + buildComment(attr) + '\n' | |
} | |
index++ | |
} | |
return html | |
} | |
function getType(value) { | |
return /\[object (\w+)\]/g.exec(toString.call(value))[1].toLowerCase() | |
} | |
function buildAttr(attr, level) { | |
return padding(level) + '<span class="hljs-attr">' + attr + '</span>' | |
} | |
function buildValue(type, value) { | |
value = type === 'string' ? '"' + value + '"' : value | |
type = type === 'null' ? 'literal' : type | |
return '<span class="hljs-' + type + '">' + value + '</span>' | |
} | |
function buildComment(attr) { | |
let result = '' | |
self.fields.forEach(field => { | |
if (field.name === attr) { | |
result = '<span class="hljs-comment"> // ' + field.description + '</span>' | |
} | |
}) | |
return result | |
} | |
function buildEndChar(char, level) { | |
return '\n' + padding(level) + '<span>' + char + '</span>,\n' | |
} | |
function padding(level) { | |
let result = '' | |
for (let i = 0; i < level; i++) { | |
result += space | |
} | |
return result | |
} | |
htmlBlock = _.trimEnd(htmlBlock, '\n') | |
htmlBlock = _.trimEnd(htmlBlock, ',') | |
htmlBlock = '{\n' + htmlBlock + '\n}' | |
return htmlBlock | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment