Cleanup the code determining value's type
This commit is contained in:
parent
d3bff7fffc
commit
ff1b7d18f6
2 changed files with 35 additions and 46 deletions
|
@ -50,50 +50,37 @@ let getObjectPrototype = function (object) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert a real value into meta data.
|
// Convert a real value into meta data.
|
||||||
var valueToMeta = function (sender, value, optimizeSimpleObject) {
|
let valueToMeta = function (sender, value, optimizeSimpleObject = false) {
|
||||||
var el, i, len, meta
|
// Determine the type of value.
|
||||||
if (optimizeSimpleObject == null) {
|
let meta = { type: typeof value }
|
||||||
optimizeSimpleObject = false
|
|
||||||
}
|
|
||||||
meta = {
|
|
||||||
type: typeof value
|
|
||||||
}
|
|
||||||
if (Buffer.isBuffer(value)) {
|
if (Buffer.isBuffer(value)) {
|
||||||
meta.type = 'buffer'
|
meta.type = 'buffer'
|
||||||
}
|
} else if (value === null) {
|
||||||
if (value === null) {
|
|
||||||
meta.type = 'value'
|
meta.type = 'value'
|
||||||
}
|
} else if (Array.isArray(value)) {
|
||||||
if (Array.isArray(value)) {
|
|
||||||
meta.type = 'array'
|
meta.type = 'array'
|
||||||
}
|
} else if (value instanceof Error) {
|
||||||
if (value instanceof Error) {
|
|
||||||
meta.type = 'error'
|
meta.type = 'error'
|
||||||
}
|
} else if (value instanceof Date) {
|
||||||
if (value instanceof Date) {
|
|
||||||
meta.type = 'date'
|
meta.type = 'date'
|
||||||
}
|
} else if (meta.type === 'object') {
|
||||||
if ((value != null ? value.constructor.name : void 0) === 'Promise') {
|
// Recognize certain types of objects.
|
||||||
meta.type = 'promise'
|
if (value.constructor != null && value.constructor.name === 'Promise') {
|
||||||
}
|
meta.type = 'promise'
|
||||||
|
} else if (value.hasOwnProperty('callee') && value.length != null) {
|
||||||
// Treat simple objects as value.
|
// Treat the arguments object as array.
|
||||||
if (optimizeSimpleObject && meta.type === 'object' && v8Util.getHiddenValue(value, 'simple')) {
|
meta.type = 'array'
|
||||||
meta.type = 'value'
|
} else if (optimizeSimpleObject && v8Util.getHiddenValue(value, 'simple')) {
|
||||||
}
|
// Treat simple objects as value.
|
||||||
|
meta.type = 'value'
|
||||||
// Treat the arguments object as array.
|
|
||||||
if (meta.type === 'object' && (value.hasOwnProperty('callee')) && (value.length != null)) {
|
|
||||||
meta.type = 'array'
|
|
||||||
}
|
|
||||||
if (meta.type === 'array') {
|
|
||||||
meta.members = []
|
|
||||||
for (i = 0, len = value.length; i < len; i++) {
|
|
||||||
el = value[i]
|
|
||||||
meta.members.push(valueToMeta(sender, el))
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fill the meta object according to value's type.
|
||||||
|
if (meta.type === 'array') {
|
||||||
|
meta.members = value.map((el) => valueToMeta(sender, el))
|
||||||
} else if (meta.type === 'object' || meta.type === 'function') {
|
} else if (meta.type === 'object' || meta.type === 'function') {
|
||||||
meta.name = value.constructor.name
|
meta.name = value.constructor ? value.constructor.name : ''
|
||||||
|
|
||||||
// Reference the original value if it's an object, because when it's
|
// Reference the original value if it's an object, because when it's
|
||||||
// passed to renderer we would assume the renderer keeps a reference of
|
// passed to renderer we would assume the renderer keeps a reference of
|
||||||
|
|
|
@ -45,17 +45,19 @@ var wrapArgs = function (args, visited) {
|
||||||
type: 'date',
|
type: 'date',
|
||||||
value: value.getTime()
|
value: value.getTime()
|
||||||
}
|
}
|
||||||
} else if ((value != null ? value.constructor.name : void 0) === 'Promise') {
|
|
||||||
return {
|
|
||||||
type: 'promise',
|
|
||||||
then: valueToMeta(function (v) { value.then(v) })
|
|
||||||
}
|
|
||||||
} else if ((value != null) && typeof value === 'object' && v8Util.getHiddenValue(value, 'atomId')) {
|
|
||||||
return {
|
|
||||||
type: 'remote-object',
|
|
||||||
id: v8Util.getHiddenValue(value, 'atomId')
|
|
||||||
}
|
|
||||||
} else if ((value != null) && typeof value === 'object') {
|
} else if ((value != null) && typeof value === 'object') {
|
||||||
|
if (value.constructor != null && value.constructor.name === 'Promise') {
|
||||||
|
return {
|
||||||
|
type: 'promise',
|
||||||
|
then: valueToMeta(function (v) { value.then(v) })
|
||||||
|
}
|
||||||
|
} else if (v8Util.getHiddenValue(value, 'atomId')) {
|
||||||
|
return {
|
||||||
|
type: 'remote-object',
|
||||||
|
id: v8Util.getHiddenValue(value, 'atomId')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ret = {
|
ret = {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
name: value.constructor.name,
|
name: value.constructor.name,
|
||||||
|
|
Loading…
Add table
Reference in a new issue