(
nodeObj: any,
platform: string,
{metadata, addIndexPath, parentPath}: TransformNodeOptions,
)
| 80 | * @returns Object containing unknown nodes and attributes |
| 81 | */ |
| 82 | export function transformNode( |
| 83 | nodeObj: any, |
| 84 | platform: string, |
| 85 | {metadata, addIndexPath, parentPath}: TransformNodeOptions, |
| 86 | ): NodesAndAttributes { |
| 87 | const unknownNodes: string[] = []; |
| 88 | const unknownAttrs: string[] = []; |
| 89 | if (util.isPlainObject(nodeObj)) { |
| 90 | const keys = Object.keys(nodeObj); |
| 91 | const childNodeNames = keys.filter(isNode); |
| 92 | const attrs = keys.filter(isAttr); |
| 93 | let thisIndexPath = parentPath || ''; |
| 94 | |
| 95 | if (attrs.length && addIndexPath) { |
| 96 | if (!attrs.includes(IDX_PREFIX)) { |
| 97 | throw new Error(`Index path is required but node found with no 'index' attribute`); |
| 98 | } |
| 99 | |
| 100 | thisIndexPath = `${parentPath || ''}/${nodeObj[IDX_PREFIX] as string}`; |
| 101 | nodeObj[IDX_PATH_PREFIX] = thisIndexPath; |
| 102 | } |
| 103 | |
| 104 | const transformFn = TRANSFORMS[platform as keyof typeof TRANSFORMS]; |
| 105 | if (transformFn) { |
| 106 | transformFn(nodeObj, metadata || ({} as TransformMetadata)); |
| 107 | } |
| 108 | unknownAttrs.push(...transformAttrs(nodeObj, attrs, platform)); |
| 109 | const unknowns = transformChildNodes(nodeObj, childNodeNames, platform, { |
| 110 | metadata, |
| 111 | addIndexPath, |
| 112 | parentPath: thisIndexPath, |
| 113 | }); |
| 114 | unknownAttrs.push(...unknowns.attrs); |
| 115 | unknownNodes.push(...unknowns.nodes); |
| 116 | } else if (Array.isArray(nodeObj)) { |
| 117 | for (const childObj of nodeObj) { |
| 118 | const {nodes, attrs} = transformNode(childObj, platform, { |
| 119 | metadata, |
| 120 | addIndexPath, |
| 121 | parentPath: parentPath || '', |
| 122 | }); |
| 123 | unknownNodes.push(...nodes); |
| 124 | unknownAttrs.push(...attrs); |
| 125 | } |
| 126 | } |
| 127 | return { |
| 128 | nodes: util.uniq(unknownNodes), |
| 129 | attrs: util.uniq(unknownAttrs), |
| 130 | }; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Transforms child nodes of a node object |
no test coverage detected