(
object,
func,
currentPath = [],
extraPath = undefined
)
| 38 | |
| 39 | // crawl a layout object or children array, apply a function on every object |
| 40 | export const crawlLayout = ( |
| 41 | object, |
| 42 | func, |
| 43 | currentPath = [], |
| 44 | extraPath = undefined |
| 45 | ) => { |
| 46 | if (Array.isArray(object)) { |
| 47 | // children array |
| 48 | object.forEach((child, i) => { |
| 49 | if (extraPath) { |
| 50 | const objOf = findIndex(p => includes('{}', p), extraPath); |
| 51 | if (objOf !== -1) { |
| 52 | const front = slice(0, objOf, extraPath); |
| 53 | const back = slice(objOf, extraPath.length, extraPath); |
| 54 | if (front.length) { |
| 55 | crawlLayout( |
| 56 | path(front, child), |
| 57 | func, |
| 58 | concat(currentPath, concat([i], front)), |
| 59 | back |
| 60 | ); |
| 61 | } else { |
| 62 | const backPath = back |
| 63 | .map(p => p.replace('{}', '')) |
| 64 | .filter(e => e); |
| 65 | let childObj, |
| 66 | childPath = concat([i], backPath); |
| 67 | if (backPath.length) { |
| 68 | childObj = path(backPath, child); |
| 69 | } else { |
| 70 | childObj = child; |
| 71 | } |
| 72 | for (const key in childObj) { |
| 73 | const value = childObj[key]; |
| 74 | crawlLayout( |
| 75 | value, |
| 76 | func, |
| 77 | concat(currentPath, childPath.concat([key])) |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | } else { |
| 82 | crawlLayout( |
| 83 | path(extraPath, child), |
| 84 | func, |
| 85 | concat(currentPath, concat([i], extraPath)) |
| 86 | ); |
| 87 | } |
| 88 | } else { |
| 89 | crawlLayout(child, func, append(i, currentPath)); |
| 90 | } |
| 91 | }); |
| 92 | } else if (type(object) === 'Object') { |
| 93 | func(object, currentPath); |
| 94 | |
| 95 | const children = path(propsChildren, object); |
| 96 | if (children) { |
| 97 | const newPath = concat(currentPath, propsChildren); |
no test coverage detected
searching dependent graphs…