This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove MD5 reference (was used for path hashing)
Object metadata cannot be based on paths, has to be based on reference, because the same object can appear at multiple paths - Luckily our serializer retains object references across serializations, so this can work for us
- Loading branch information
Showing
7 changed files
with
43 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,36 @@ | ||
import {Path} from '../tree'; | ||
|
||
export type Apply = (path: Path, value) => void; | ||
import {isScalar} from './scalar'; | ||
|
||
export const recurse = (initialPath: Path, object, exec: Apply) => { | ||
export type Apply = (value) => void; | ||
|
||
// Recursive traversal of an object tree, but will not traverse circular references or DOM elements | ||
export const recurse = (object, apply: Apply) => { | ||
const visited = new Set(); | ||
|
||
const apply = (path: Path, value, fn: Apply) => { | ||
if (value == null || visited.has(value)) { | ||
const visit = value => { | ||
if (value == null || isScalar(value) || /Element/.test(Object.prototype.toString.call(value))) { | ||
return; | ||
} | ||
|
||
if (visited.has(value)) { // circular loop | ||
return; | ||
} | ||
|
||
visited.add(value); | ||
|
||
fn(path, value); | ||
apply(value); | ||
|
||
if (Array.isArray(value) || value instanceof Set) { | ||
(<any>value).forEach((v, k) => apply(path.concat([k]), v, fn)); | ||
(<any>value).forEach((v, k) => visit(v)); | ||
} | ||
else if (value instanceof Map) { | ||
value.forEach((v, k) => apply(path.concat([k]), v, fn)); | ||
value.forEach((v, k) => visit(v)); | ||
} | ||
else { | ||
Object.keys(value).forEach(k => apply(path.concat([k]), value[k], fn)); | ||
Object.keys(value).forEach(k => visit(value[k])); | ||
} | ||
}; | ||
|
||
apply(initialPath, object, exec); | ||
visit(object); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters