Fix nested map issue
This commit is contained in:
parent
f76867da5e
commit
9e0d1d623e
|
@ -78,6 +78,9 @@ it("serializes simple objects", () => {
|
||||||
expect(result["d"].has(2)).toBe(false);
|
expect(result["d"].has(2)).toBe(false);
|
||||||
expect(result["e"].get("z")).toBe(8);
|
expect(result["e"].get("z")).toBe(8);
|
||||||
expect(result["e"].get("k")).toBeUndefined();
|
expect(result["e"].get("k")).toBeUndefined();
|
||||||
|
|
||||||
|
checkReversability(new Set(["a", new Set([1, "b"])]));
|
||||||
|
checkReversability(new Map([["a", new Map([[1, "test"]])]]));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("restores objects constructed from class", () => {
|
it("restores objects constructed from class", () => {
|
||||||
|
|
|
@ -184,33 +184,49 @@ function crawl(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (obj !== undefined && obj !== null && typeof obj != "function") {
|
if (obj !== undefined && obj !== null && typeof obj != "function") {
|
||||||
let result = callback(obj);
|
const result = callback(obj);
|
||||||
|
|
||||||
if (result === STOP_CRAWLING) {
|
if (result === STOP_CRAWLING) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(obj)) {
|
if (Array.isArray(obj)) {
|
||||||
let subresult = obj.map((value) => crawl(value, callback, replace, memo));
|
const subresult = obj.map((value) =>
|
||||||
|
crawl(value, callback, replace, memo)
|
||||||
|
);
|
||||||
if (replace) {
|
if (replace) {
|
||||||
subresult.forEach((value, index) => {
|
subresult.forEach((value, index) => {
|
||||||
obj[index] = value;
|
obj[index] = value;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else if (obj instanceof Set) {
|
} else if (obj instanceof Set) {
|
||||||
let subresult = new Set();
|
const subresult = new Set();
|
||||||
for (let item of obj) {
|
for (const item of obj) {
|
||||||
subresult.add(crawl(item, callback, replace, memo));
|
subresult.add(crawl(item, callback, replace, memo));
|
||||||
}
|
}
|
||||||
if (replace) {
|
if (replace) {
|
||||||
obj.clear();
|
obj.clear();
|
||||||
for (let item of subresult) {
|
for (const item of subresult) {
|
||||||
obj.add(item);
|
obj.add(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (obj instanceof Map) {
|
||||||
|
const subresult = new Map();
|
||||||
|
for (const [key, item] of obj.entries()) {
|
||||||
|
subresult.set(
|
||||||
|
crawl(key, callback, replace, memo),
|
||||||
|
crawl(item, callback, replace, memo),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (replace) {
|
||||||
|
obj.clear();
|
||||||
|
for (const [key, item] of subresult.entries()) {
|
||||||
|
obj.set(key, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (obj instanceof Object) {
|
} else if (obj instanceof Object) {
|
||||||
let subresult: any = {};
|
const subresult: any = {};
|
||||||
for (let key in obj) {
|
for (const key in obj) {
|
||||||
subresult[key] = crawl(obj[key], callback, replace, memo);
|
subresult[key] = crawl(obj[key], callback, replace, memo);
|
||||||
}
|
}
|
||||||
if (replace) {
|
if (replace) {
|
||||||
|
|
Loading…
Reference in a new issue