src/core/entity.js
/**
 * Flatten and filter compoments into a single array.
 *
 * flattenComponents(components: [...Function|[Function]) -> [...Function]
 */
Flatten and filter compoments into a single array.
Flatten and filter compoments into a single array .
function flattenComponents(initialState = {}, ...components){
  components = Array.isArray(components[0]) ? components[0] : components
  return {
    initialState,
    components: components.filter(filter).map(map).reduce(reduce, [])
  }
  function filter(component) {
    return 'function' == typeof component
  }
  function map(component) {
    if ('entityId' in component && Array.isArray(component.components)) {
      if (component.initialState && 'object' == typeof component.initialState) {
        extend(true, initialState, component.initialState)
      }
      return flattenComponents(initialState, component.components).components
    } else {
      return component
    }
  }
  function reduce(components, component) {
    return components.concat(component)
  }
}
function flattenComponents ( initialState = { } , ... components ) { components = Array . isArray ( components [ 0 ] ) ? components [ 0 ] : components return { initialState , components : components . filter ( filter ) . map ( map ) . reduce ( reduce , [ ] ) } function filter ( component ) { return 'function' == typeof component } function map ( component ) { if ( 'entityId' in component && Array . isArray ( component . components ) ) { if ( component . initialState && 'object' == typeof component . initialState ) { extend ( true , initialState , component . initialState ) } return flattenComponents ( initialState , component . components ) . components } else { return component } } function reduce ( components , component ) { return components . concat ( component ) } }
Flatten and filter compoments into a single array.
Flatten and filter compoments into a single array .