Dev: Small changes

This commit is contained in:
surunzi
2016-10-19 19:32:53 +08:00
parent e92f282b1b
commit f0b3cfe33c
3 changed files with 53 additions and 44 deletions

View File

@@ -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)
<span class="eruda-function">${encode(val).replace('function', '')}</span>
</li>`;
}
if (val === 'undefined' || val === 'Symbol')
if (val === 'undefined' || val === 'Symbol' || val === '(...)')
{
return `<li>
${wrapKey(key)}

View File

@@ -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)
{

View File

@@ -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()