From a3233e5c84c421594672df1b06ecd9717d8c52b2 Mon Sep 17 00:00:00 2001 From: surunzi Date: Wed, 19 Apr 2017 14:18:32 +0800 Subject: [PATCH] Dev: Console object keys order --- src/lib/getAbstract.es6 | 9 ++++++--- src/lib/stringify.es6 | 35 +++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/lib/getAbstract.es6 b/src/lib/getAbstract.es6 index 4e0af28..5ca2769 100644 --- a/src/lib/getAbstract.es6 +++ b/src/lib/getAbstract.es6 @@ -7,7 +7,7 @@ export default function getAbstract(obj, { level = 0, getterVal = false, unenumerable = true - } = {}) +} = {}) { let json = '', type = '', @@ -71,7 +71,6 @@ export default function getAbstract(obj, { return; } } - if (typeof topObj[name] === 'function') return; parts.push(`${key}: ${getAbstract(topObj[name], passOpts)}`); i++; } @@ -89,6 +88,7 @@ export default function getAbstract(obj, { isNum = (type == '[object Number]'), isRegExp = (type == '[object RegExp]'), isSymbol = (type == '[object Symbol]'), + isFn = (type == '[object Function]'), isBool = (type == '[object Boolean]'); if (circular) @@ -100,6 +100,9 @@ export default function getAbstract(obj, { } else if (isRegExp) { json = wrapRegExp(util.escapeJsonStr(obj.toString())); + } else if (isFn) + { + json = wrapStr('function'); } else if (isArr) { if (doStringify) @@ -179,7 +182,7 @@ export default function getAbstract(obj, { return json; } -const SPECIAL_VAL = ['(...)', 'undefined', 'Symbol', 'Object']; +const SPECIAL_VAL = ['(...)', 'undefined', 'Symbol', 'Object', 'function']; function canBeProto(obj) { diff --git a/src/lib/stringify.es6 b/src/lib/stringify.es6 index 6875107..35e71e5 100644 --- a/src/lib/stringify.es6 +++ b/src/lib/stringify.es6 @@ -8,7 +8,7 @@ export default function stringify(obj, { circularMarker = false, getterVal = false, unenumerable = true - } = {}) +} = {}) { let json = '', type, @@ -56,16 +56,17 @@ export default function stringify(obj, { id = visitor.visit(obj); } - allKeys = Object.getOwnPropertyNames(obj); - keys = Object.keys(obj); + names = getKeys(obj); + keys = names.keys; + allKeys = names.allKeys; names = unenumerable ? allKeys : keys; + proto = Object.getPrototypeOf(obj); if (circularMarker && proto === Object.prototype) proto = null; if (proto) { proto = `${wrapKey('erudaProto')}: ${stringify(proto, passProtoOpts)}`; } - names.sort(sortObjName); if (isFn) { // We don't need these properties to display for functions. @@ -125,9 +126,12 @@ export default function stringify(obj, { }); parts.push(`${wrapKey('erudaObjAbstract')}: ${wrapStr(objAbstract)}`); if (!circularMarker) parts.push(`"erudaId": "${id}"`); - allKeys = Object.getOwnPropertyNames(obj); - keys = Object.keys(obj); + + names = getKeys(obj); + keys = names.keys; + allKeys = names.allKeys; names = unenumerable ? allKeys : keys; + proto = Object.getPrototypeOf(obj); if (circularMarker && proto === Object.prototype) proto = null; if (proto) @@ -140,7 +144,6 @@ export default function stringify(obj, { proto = `${wrapKey('erudaProto')}: ${wrapStr(e.message)}`; } } - names.sort(sortObjName); util.each(names, objIteratee); if (proto) parts.push(proto); json += parts.join(', ') + ' }'; @@ -185,8 +188,18 @@ export default function stringify(obj, { return json; } +function getKeys(obj) +{ + let allKeys = Object.getOwnPropertyNames(obj), + keys = Object.keys(obj).sort(sortObjName); + + allKeys = keys.concat(util.filter(allKeys, val => !util.contain(keys, val)).sort(sortObjName)); + + return {keys, allKeys}; +} + // $, upperCase, lowerCase, _ -var sortObjName = (a, b) => +function sortObjName(a, b) { let lenA = a.length, lenB = b.length, @@ -203,8 +216,9 @@ var sortObjName = (a, b) => if (lenA > lenB) return 1; if (lenA < lenB) return -1; + return 0; -}; +} function cmpCode(a, b) { @@ -218,7 +232,8 @@ function cmpCode(a, b) function transCode(code) { - if (code === 95) return 91; + // _ should be placed after lowercase chars. + if (code === 95) return 123; return code; }