mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
Added toJSONObject util; (#5247)
Fixed AxiosError.toJSON method to avoid circular references; Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
+33
-1
@@ -592,6 +592,37 @@ const toFiniteNumber = (value, defaultValue) => {
|
||||
return Number.isFinite(value) ? value : defaultValue;
|
||||
}
|
||||
|
||||
const toJSONObject = (obj) => {
|
||||
const stack = new Array(10);
|
||||
|
||||
const visit = (source, i) => {
|
||||
|
||||
if (isObject(source)) {
|
||||
if (stack.indexOf(source) >= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!('toJSON' in source)) {
|
||||
stack[i] = source;
|
||||
const target = isArray(source) ? [] : {};
|
||||
|
||||
forEach(source, (value, key) => {
|
||||
const reducedValue = visit(value, i + 1);
|
||||
!isUndefined(reducedValue) && (target[key] = reducedValue);
|
||||
});
|
||||
|
||||
stack[i] = undefined;
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
return visit(obj, 0);
|
||||
}
|
||||
|
||||
export default {
|
||||
isArray,
|
||||
isArrayBuffer,
|
||||
@@ -637,5 +668,6 @@ export default {
|
||||
toFiniteNumber,
|
||||
findKey,
|
||||
global: _global,
|
||||
isContextDefined
|
||||
isContextDefined,
|
||||
toJSONObject
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user