function sortFactories(factories) {
  var factoriesByName = {};
  factories.forEach(function (factory) {
    factoriesByName[factory.fn] = factory;
  });

  function containsDependency(factory, dependency) {
    // TODO: detect circular references
    if (isFactory(factory)) {
      if ((0, _array.contains)(factory.dependencies, dependency.fn || dependency.name)) {
        return true;
      }

      if (factory.dependencies.some(function (d) {
        return containsDependency(factoriesByName[d], dependency);
      })) {
        return true;
      }
    }

    return false;
  }

  var sorted = [];

  function addFactory(factory) {
    var index = 0;

    while (index < sorted.length && !containsDependency(sorted[index], factory)) {
      index++;
    }

    sorted.splice(index, 0, factory);
  } // sort regular factory functions


  factories.filter(isFactory).forEach(addFactory); // sort legacy factory functions AFTER the regular factory functions

  factories.filter(function (factory) {
    return !isFactory(factory);
  }).forEach(addFactory);
  return sorted;
} // TODO: comment or cleanup if unused in the end
/**
 * Sort all factories such that when loading in order, the dependencies are resolved.
 *
 * @param {Array} factories
 * @returns {Array} Returns a new array with the sorted factories.
 */
Sort all factories such that when loading in order, the dependencies are resolved.
@param {Array} factories
@returns {Array} Returns a new array with the sorted factories.
Sort all factories such that when loading in order the dependencies are resolved . @param { Array } factories @returns { Array } Returns a new array with the sorted factories .
function sortFactories(factories) {
  var factoriesByName = {};
  factories.forEach(function (factory) {
    factoriesByName[factory.fn] = factory;
  });
  function containsDependency(factory, dependency) {
    if (isFactory(factory)) {
      if ((0, _array.contains)(factory.dependencies, dependency.fn || dependency.name)) {
        return true;
      }
      if (factory.dependencies.some(function (d) {
        return containsDependency(factoriesByName[d], dependency);
      })) {
        return true;
      }
    }
    return false;
  }
  var sorted = [];
  function addFactory(factory) {
    var index = 0;
    while (index < sorted.length && !containsDependency(sorted[index], factory)) {
      index++;
    }
    sorted.splice(index, 0, factory);
  } 
  factories.filter(isFactory).forEach(addFactory); 
  factories.filter(function (factory) {
    return !isFactory(factory);
  }).forEach(addFactory);
  return sorted;
} 
function sortFactories ( factories ) { var factoriesByName = { } ; factories . forEach ( function ( factory ) { factoriesByName [ factory . fn ] = factory ; } ) ; function containsDependency ( factory , dependency ) { if ( isFactory ( factory ) ) { if ( ( 0 , _array . contains ) ( factory . dependencies , dependency . fn || dependency . name ) ) { return true ; } if ( factory . dependencies . some ( function ( d ) { return containsDependency ( factoriesByName [ d ] , dependency ) ; } ) ) { return true ; } } return false ; } var sorted = [ ] ; function addFactory ( factory ) { var index = 0 ; while ( index < sorted . length && ! containsDependency ( sorted [ index ] , factory ) ) { index ++ ; } sorted . splice ( index , 0 , factory ) ; } factories . filter ( isFactory ) . forEach ( addFactory ) ; factories . filter ( function ( factory ) { return ! isFactory ( factory ) ; } ) . forEach ( addFactory ) ; return sorted ; }
Sort all factories such that when loading in order, the dependencies are resolved.
Sort all factories such that when loading in order the dependencies are resolved .
// TODO: detect circular references // sort regular factory functions // sort legacy factory functions AFTER the regular factory functions // TODO: comment or cleanup if unused in the end