From f0b3cfe33c6217ed4edf758cf8441cb9ab9d76ab Mon Sep 17 00:00:00 2001 From: surunzi Date: Wed, 19 Oct 2016 19:32:53 +0800 Subject: [PATCH] Dev: Small changes --- src/lib/JsonViewer.es6 | 18 ++++++------ src/lib/getAbstract.es6 | 16 ++++------- src/lib/stringify.es6 | 63 ++++++++++++++++++++++++----------------- 3 files changed, 53 insertions(+), 44 deletions(-) diff --git a/src/lib/JsonViewer.es6 b/src/lib/JsonViewer.es6 index 4b0383d..4111417 100644 --- a/src/lib/JsonViewer.es6 +++ b/src/lib/JsonViewer.es6 @@ -84,12 +84,7 @@ function createEl(key, val, map, firstLevel = false) isUnenumerable = true; } - if (util.isArr(val)) - { - type = 'array'; - let lastVal = util.last(val); - if (util.isStr(lastVal) && util.startWith(lastVal, 'erudaJson')) id = lastVal; - } + if (util.isArr(val)) type = 'array'; function wrapKey(key) { @@ -110,7 +105,14 @@ function createEl(key, val, map, firstLevel = false) } if (util.isObj(val)) { - if (val.erudaId) id = val.erudaId; + if (val.erudaId) + { + id = val.erudaId; + } else + { + id = util.uniqId('erudaJson'); + val.erudaId = id; + } let circularId = val.erudaCircular; if (id) map[id] = val; let objAbstract = val['erudaObjAbstract'] || util.upperFirst(type); @@ -140,7 +142,7 @@ function createEl(key, val, map, firstLevel = false) ${encode(val).replace('function', '')} `; } - if (val === 'undefined' || val === 'Symbol') + if (val === 'undefined' || val === 'Symbol' || val === '(...)') { return `
  • ${wrapKey(key)} diff --git a/src/lib/getAbstract.es6 b/src/lib/getAbstract.es6 index cbf23ce..9d92c08 100644 --- a/src/lib/getAbstract.es6 +++ b/src/lib/getAbstract.es6 @@ -35,6 +35,7 @@ export default function getAbstract(obj, { let wrapKey = key => keyWrapper + strEscape(key) + wrapperEnd, wrapNum = num => numWrapper + num + wrapperEnd, + wrapRegExp = (str) => strWrapper + str + wrapperEnd, wrapBool = bool => boolWrapper + bool + wrapperEnd, wrapNull = str => nullWrapper + str + wrapperEnd; @@ -98,6 +99,7 @@ export default function getAbstract(obj, { isArr = (type == '[object Array]'), isObj = (type == '[object Object]'), isNum = (type == '[object Number]'), + isRegExp = (type == '[object RegExp]'), isSymbol = (type == '[object Symbol]'), isBool = (type == '[object Boolean]'); @@ -107,6 +109,9 @@ export default function getAbstract(obj, { } else if (isStr) { json = wrapStr(util.escapeJsonStr(obj)); + } else if (isRegExp) + { + json = wrapRegExp(util.escapeJsonStr(obj.toString())); } else if (isArr) { if (doStringify) @@ -158,15 +163,6 @@ export default function getAbstract(obj, { } else if (obj === undefined) { json = wrapStr('undefined'); - } else if (type === '[object HTMLAllCollection]') - { - // https://docs.webplatform.org/wiki/dom/HTMLAllCollection - // Might cause a performance issue when stringify a dom element. - json = wrapStr('[object HTMLAllCollection]'); - } else if (type === '[object HTMLDocument]' && level > 1) - { - // Same as reason above. - json = wrapStr('[object HTMLDocument]'); } else { try { @@ -184,7 +180,7 @@ export default function getAbstract(obj, { json += parts.join(', ') + objEllipsis + ' }'; } else { - json = wrapStr(obj); + json = type.replace(/(\[object )|]/g, ''); } } catch (e) { diff --git a/src/lib/stringify.es6 b/src/lib/stringify.es6 index 5aafd47..1d77c59 100644 --- a/src/lib/stringify.es6 +++ b/src/lib/stringify.es6 @@ -11,7 +11,7 @@ export default function stringify(obj, { } = {}) { let json = '', - type = '', + type, parts = [], names = [], proto, @@ -25,15 +25,10 @@ export default function stringify(obj, { let passOpts = {visitor, getterVal, unenumerable, level: level + 1}, passProtoOpts = {visitor, getterVal, topObj, unenumerable, level: level + 1}; - let wrapKey = key => `"${key}"`, - wrapStr = str => `"${util.toStr(str)}"`; + let wrapKey = key => `"${util.escapeJsonStr(key)}"`, + wrapStr = str => `"${util.escapeJsonStr(util.toStr(str))}"`; - try { - type = ({}).toString.call(obj); - } catch (e) - { - type = '[object Object]'; - } + type = getType(obj); var isFn = (type == '[object Function]'), isStr = (type == '[object String]'), @@ -50,17 +45,8 @@ export default function stringify(obj, { json = stringify(circularObj.abstract, {circularMarker: true}); } else if (isStr) { - json = wrapStr(util.escapeJsonStr(obj)); - } else if (isArr) - { - id = visitor.visit(obj); - visitor.updateAbstract(id, [`erudaCircular ${id}`]); - - json = '['; - util.each(obj, val => parts.push(`${stringify(val, passOpts)}`)); - parts.push(`"${id}"`); - json += parts.join(', ') + ']'; - } else if (isObj || isFn) + json = wrapStr(obj); + } else if (isArr || isObj || isFn) { id = visitor.visit(obj); @@ -86,7 +72,7 @@ export default function stringify(obj, { names = names.filter(val => ['arguments', 'caller'].indexOf(val) < 0); } json = '{ '; - objAbstract = isFn ? getFnAbstract(obj) : type.replace(/(\[object )|]/g, ''); + objAbstract = getObjAbstract(obj); visitor.updateAbstract(id, { erudaObjAbstract: objAbstract, erudaCircular: id @@ -132,7 +118,7 @@ export default function stringify(obj, { } json = '{ '; - objAbstract = type.replace(/(\[object )|]/g, ''); + objAbstract = getObjAbstract(obj); visitor.updateAbstract(id, { erudaObjAbstract: objAbstract, erudaCircular: id @@ -167,9 +153,9 @@ export default function stringify(obj, { function objIteratee(name) { let unenumerable = !util.contain(keys, name) ? 'erudaUnenumerable ' : '', - key = wrapKey(unenumerable + util.escapeJsonStr(name)), - getKey = wrapKey(unenumerable + util.escapeJsonStr('get ' + name)), - setKey = wrapKey(unenumerable + util.escapeJsonStr('set ' + name)); + key = wrapKey(unenumerable + name), + getKey = wrapKey(unenumerable + 'get ' + name), + setKey = wrapKey(unenumerable + 'set ' + name); let descriptor = Object.getOwnPropertyDescriptor(obj, name), hasGetter = descriptor && descriptor.get, @@ -177,6 +163,7 @@ export default function stringify(obj, { if (!getterVal && hasGetter) { + parts.push(`${key}: "(...)"`); parts.push(`${getKey}: ${stringify(descriptor.get, passOpts)}`); } else { @@ -252,7 +239,7 @@ function getFnAbstract(fn) let fnStr = fn.toString(); if (fnStr.length > 500) fnStr = fnStr.slice(0, 500) + '...'; - return util.escapeJsonStr(extractFnHead(fnStr).replace('function', '')); + return extractFnHead(fnStr).replace('function', ''); } function canBeProto(obj) @@ -263,6 +250,30 @@ function canBeProto(obj) return emptyObj && proto && proto !== Object.prototype; } +function getObjAbstract(obj) +{ + if (util.isFn(obj)) return getFnAbstract(obj); + if (util.isRegExp(obj)) return obj.toString(); + + let type = getType(obj); + + return type.replace(/(\[object )|]/g, '') +} + +function getType(obj) +{ + let type; + + try { + type = ({}).toString.call(obj); + } catch (e) + { + type = '[object Object]'; + } + + return type; +} + class Visitor { constructor()