UNPKG

3.42 kBJavaScriptView Raw
1'use strict'
2
3var ObjectID = require('mongodb').ObjectID,
4 Binary = require('mongodb').Binary
5
6/**
7 * @param {*} value
8 * @param {boolean} [useColors=false]
9 * @param {string} [highlightPath]
10 * @returns {string}
11 */
12module.exports = function (value, useColors, highlightPath) {
13 var finalStr = '',
14 indentLevel = 0,
15 highlight = false
16
17 var hasBreak = true
18 var getNL = function () {
19 var i, nl = useColors && highlight ? '\x1b[0m\n\x1b[41m' : '\n'
20 for (i = 0; i < indentLevel; i++) {
21 nl += ' '
22 }
23 return nl
24 }
25 var escapeKey = function (key) {
26 if (key.match(/^[a-z_$][a-z_$0-9]*$/i)) {
27 return key
28 } else {
29 return '"' + key.replace(/"/g, '\\"') + '"'
30 }
31 }
32 var pushStr = function (str, breakBefore, breakAfter, color) {
33 if (!hasBreak && breakBefore) {
34 finalStr += getNL()
35 }
36 if (color && useColors) {
37 finalStr += '\x1b[3' + color + 'm' + str + '\x1b[37m'
38 } else {
39 finalStr += str
40 }
41 if (breakAfter) {
42 finalStr += getNL()
43 }
44 hasBreak = breakAfter
45 }
46 var colors = {
47 key: '4;1',
48 number: '3;1',
49 string: '2',
50 id: '5;1',
51 bin: '6;1',
52 date: '6',
53 regex: '1;1'
54 }
55 var pushJsonValue = function (value, path) {
56 var needComma, subpath
57
58 if (path === highlightPath) {
59 highlight = true
60 finalStr += '\x1b[41m'
61 }
62
63 if (value === false) {
64 pushStr('false', false, false, colors.key)
65 } else if (value === true) {
66 pushStr('true', false, false, colors.key)
67 } else if (value === undefined) {
68 pushStr('undefined', false, false, colors.key)
69 } else if (value === null) {
70 pushStr('null', false, false, colors.key)
71 } else if (typeof value === 'number') {
72 pushStr(String(value), false, false, colors.number)
73 } else if (typeof value === 'string') {
74 pushStr('\'' + value.replace(/'/g, '\\\'') + '\'', false, false, colors.string)
75 } else if (Array.isArray(value) && !value.length) {
76 pushStr('[]')
77 } else if (Array.isArray(value)) {
78 indentLevel++
79 pushStr('[', false, true)
80 value.forEach(function (value, i) {
81 var subpath = path ? path + '.' + i : String(i)
82 if (i) {
83 pushStr(',', false, true)
84 }
85 pushJsonValue(value, subpath)
86 })
87 indentLevel--
88 pushStr(']', true)
89 } else if (value instanceof ObjectID) {
90 pushStr(value.toString(), false, false, colors.id)
91 } else if (value instanceof Binary) {
92 pushStr(value.toString('base64'), false, false, colors.bin)
93 } else if (value instanceof Date) {
94 pushStr(value.toISOString(), false, false, colors.date)
95 } else if (value instanceof RegExp) {
96 pushStr(String(value), false, false, colors.regex)
97 } else if (typeof value === 'function') {
98 pushStr(value.name || String(value), false, false)
99 } else if (!Object.keys(value).length) {
100 pushStr('{}')
101 } else {
102 indentLevel++
103 pushStr('{', false, true)
104 needComma = false
105 Object.keys(value).sort().forEach(function (key) {
106 if (!needComma) {
107 needComma = true
108 } else {
109 pushStr(',', false, true)
110 }
111 subpath = path ? path + '.' + key : key
112 pushStr(escapeKey(key), false, false, colors.key)
113 pushStr(': ')
114 pushJsonValue(value[key], subpath)
115 })
116 indentLevel--
117 pushStr('}', true)
118 }
119
120 if (path === highlightPath) {
121 highlight = false
122 finalStr += '\x1b[0m'
123 }
124 }
125 pushJsonValue(value, '')
126
127 return finalStr
128}
\No newline at end of file