-
Notifications
You must be signed in to change notification settings - Fork 3
/
prepare-value.js
156 lines (151 loc) · 4.47 KB
/
prepare-value.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var util = require("util")
function prepareArray(array, options) {
return array.map(function prepareItem(i) {
return prepareValue(i, options) // eslint-disable-line no-use-before-define
})
}
function prepareObject(object, options) {
if (!object) {
return {}
}
const deep = {}
Object.keys(object).forEach(function prepareItem(key) {
deep[key] = prepareValue(object[key], options) // eslint-disable-line no-use-before-define
})
return deep
}
function prepareValue(value, options) {
if (!options) {
options = {}
}
if (!options.seen) {
options.seen = []
}
var type
var primitive
if (util.isArray(value)) {
type = Array.isArray(value) ? "Array" : util.getNativeClass(value)
primitive = "Array"
if (options.seen.indexOf(value) !== -1) {
type = "Circular"
value = []
} else {
options.seen.push(value)
value = prepareArray(util.toArray(value), options)
}
} else if (util.isBoolean(value)) {
type = typeof value === "boolean" ? "Boolean" : util.getNativeClass(value)
primitive = "Boolean"
value = Boolean(Number(value))
} else if (util.isNullOrUndefined(value) || Number.isNaN(value)) {
type = "Empty"
primitive = "Empty"
value = String(value)
} else if (util.isNumber(value)) {
type = typeof value === "number" ? "Number" : util.getNativeClass(value)
primitive = "Number"
value = Number(value)
} else if (util.isString(value)) {
type = typeof value === "string" ? "String" : util.getNativeClass(value)
primitive = "String"
value = String(value)
} else if (util.getNativeClass(value) === 'MOStruct') {
type = 'MOStruct'
primitive = "Object"
value = prepareObject(util.toObject(value), options)
} else if (util.isSymbol(value)) {
type = "Symbol"
primitive = "Symbol"
value = util.inspect(value)
} else if (util.isRegExp(value)) {
type = "RegExp"
primitive = "RegExp"
value = util.inspect(value)
} else if (util.isDate(value)) {
type = "Date"
primitive = "Date"
value = util.inspect(value)
} else if (util.isFunction(value)) {
type = typeof value === "function" ? "Function" : util.getNativeClass(value)
primitive = "Function"
value =
typeof value === "function" ? "[Function]" : util.getNativeClass(value)
} else if (util.isBuffer(value)) {
type = "Buffer"
primitive = "Buffer"
value = String(value)
} else if (util.isError(value)) {
type = "Error"
primitive = "Error"
value = {
message: value.message,
name: value.name,
stack: value.stack,
nativeException: prepareValue(value.nativeException, options)
}
} else if (util.isObject(value)) {
var nativeClass = util.getNativeClass(value)
type = nativeClass || "Object"
primitive = "Object"
if (options.seen.indexOf(value) !== -1) {
type = "Circular"
value = {}
} else if (value._isWrappedObject) {
options.seen.push(value)
type = value.type
const propertyList = value.constructor._DefinedPropertiesKey
const json = {}
Object.keys(propertyList).forEach(function customToJSON(k) {
if (!propertyList[k].exportable) {
return
}
if (typeof value[k] === 'undefined') {
return
}
json[k] = value[k]
if (json[k] && !json[k]._isWrappedObject && json[k].toJSON) {
json[k] = json[k].toJSON()
}
})
value = prepareObject(json, options)
} else {
options.seen.push(value)
value = prepareObject(util.toObject(value), options)
}
} else if (util.isNativeObject(value)) {
type = util.getNativeClass(value)
// special case for NSException
if (type === "NSException") {
primitive = "Error"
var stack = ""
var userInfo = value.userInfo && value.userInfo() ? value.userInfo() : {}
if (userInfo.stack) {
stack = String(userInfo.stack)
}
value = {
message: String(value.reason()),
name: String(value.name()),
stack: stack,
userInfo: prepareObject(util.toObject(userInfo), options)
}
} else if (value.class().mocha) {
primitive = "Mocha"
value = type
} else {
primitive = "Unknown"
value = type
}
} else {
type = "Unknown"
primitive = "Unknown"
value = type
}
return {
value: value,
type: type,
primitive: primitive
}
}
module.exports = prepareValue
module.exports.prepareObject = prepareObject
module.exports.prepareArray = prepareArray