* @param seen: A Map of known object mappings (i.e., memoized results of * `mapFn()`) * @param containedIn: An set containing objects on the reference path currently * being processed (used to detect cycles).
(
input: any, mapFn: (x: any) => DeepMapResult,
seen: Map<any, any> = new Map(), containedIn: Set<{}> = new Set())
| 60 | * being processed (used to detect cycles). |
| 61 | */ |
| 62 | function deepMapInternal( |
| 63 | input: any, mapFn: (x: any) => DeepMapResult, |
| 64 | seen: Map<any, any> = new Map(), containedIn: Set<{}> = new Set()): any| |
| 65 | any[] { |
| 66 | if (input == null) { |
| 67 | return null; |
| 68 | } |
| 69 | if (typeof Blob === 'function' && input instanceof Blob) { |
| 70 | return input.slice(); |
| 71 | } |
| 72 | |
| 73 | if (containedIn.has(input)) { |
| 74 | throw new Error('Circular references are not supported.'); |
| 75 | } |
| 76 | if (seen.has(input)) { |
| 77 | return seen.get(input); |
| 78 | } |
| 79 | const result = mapFn(input); |
| 80 | |
| 81 | if (result.recurse && result.value !== null) { |
| 82 | throw new Error( |
| 83 | 'A deep map function may not return both a value and recurse=true.'); |
| 84 | } |
| 85 | |
| 86 | if (!result.recurse) { |
| 87 | seen.set(input, result.value); |
| 88 | return result.value; |
| 89 | } else if (isIterable(input)) { |
| 90 | // tslint:disable-next-line:no-any |
| 91 | const mappedIterable: any|any[] = Array.isArray(input) ? [] : {}; |
| 92 | containedIn.add(input); |
| 93 | for (const k in input) { |
| 94 | const child = input[k]; |
| 95 | const childResult = deepMapInternal(child, mapFn, seen, containedIn); |
| 96 | mappedIterable[k] = childResult; |
| 97 | } |
| 98 | containedIn.delete(input); |
| 99 | if (input.__proto__) { |
| 100 | mappedIterable.__proto__ = input.__proto__; |
| 101 | } |
| 102 | return mappedIterable; |
| 103 | } else { |
| 104 | throw new Error(`Can't recurse into non-iterable type: ${input}`); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // TODO(soergel, kangyizhang) Reconsider naming of deepZip() to avoid confusion |
| 109 | // with zip() |