Skip to content

Why is the tree reduced instead of mapped in handleExtendsNodes? #26

@HoldYourWaffle

Description

@HoldYourWaffle

Why does handleExtendsNodes use the Array.reduce function instead of Array.map? As far as I can see it's always a direct transformation, the purpose of Array.map.
Also, shouldn't the line m = m.concat(...) just be m.push(...)?

Using map and push instead of concat would simplify the code from this (I removed irrelevant lines):

function handleExtendsNodes (tree, options, ctx) {
  return tree.reduce((m, node) => {
    if (node.name !== 'extends') {
      if (Array.isArray(node.content)) {
        node.content = handleExtendsNodes(node.content, options, ctx)
        m.push(node)
      } else {
        m.push(node)
      }
      return m
    }

    // ... snip ...

    m = m.concat(mergeExtendsAndLayout(layoutTree, handleExtendsNodes(node.content, options, ctx), ctx))
    return m
  }, [])
}

to this:

function handleExtendsNodes (tree, options, ctx) {
  return tree.map(node => {
    if (node.name !== 'extends') {
      if (Array.isArray(node.content)) {
        node.content = handleExtendsNodes(node.content, options, ctx)
	  }
	  return node
    }

    // ... snip ...

    return mergeExtendsAndLayout(layoutTree, handleExtendsNodes(node.content, options, ctx), ctx)
  })
}

I haven't tested this code so I'm not 100% sure it works as intended, but I don't see any reason why there's the need for reduce. I might however be missing something (probably very obvious), hence the question.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions