<commit_before>// @flow
import type { Modifier } from '../types';

// source: https://stackoverflow.com/questions/49875255
const order = modifiers => {
  // indexed by name
  const mapped = modifiers.reduce((mem, i) => {
    mem[i.name] = i;
    return mem;
  }, {});

  // inherit all dependencies for a given name
  const inherited = i => {
    return mapped[i] && mapped[i].requires
      ? mapped[i].requires.reduce((mem, i) => {
          return [...mem, i, ...inherited(i)];
        }, [])
      : [];
  };

  // order ...
  const ordered = modifiers.sort((a, b) => {
    return !!~inherited(b.name).indexOf(a.name) ? -1 : 1;
  });
  return ordered;
};

export default (modifiers: Array<Modifier<any>>): Array<Modifier<any>> => [
  ...order(modifiers.filter(({ phase }) => phase === 'read')),
  ...order(modifiers.filter(({ phase }) => phase === 'main')),
  ...order(modifiers.filter(({ phase }) => phase === 'afterMain')),
  ...order(modifiers.filter(({ phase }) => phase === 'write')),
];<commit_msg>perf: reduce a bit the bundle size<commit_after>// @flow
import type { Modifier } from '../types';

// source: https://stackoverflow.com/questions/49875255
const order = modifiers => {
  // indexed by name
  const mapped = modifiers.reduce((mem, i) => {
    mem[i.name] = i;
    return mem;
  }, {});

  // inherit all dependencies for a given name
  const inherited = i => {
    return mapped[i] && mapped[i].requires
      ? mapped[i].requires.reduce((mem, i) => {
          return [...mem, i, ...inherited(i)];
        }, [])
      : [];
  };

  // order ...
  const ordered = modifiers.sort((a, b) => {
    return !!~inherited(b.name).indexOf(a.name) ? -1 : 1;
  });
  return ordered;
};

export default (modifiers: Array<Modifier<any>>): Array<Modifier<any>> =>
  ['read', 'main', 'afterMain', 'write'].reduce(
    (acc, currentPhase) =>
      acc.concat(
        order(modifiers.filter(modifier => modifier.phase === currentPhase))
      ),
    []
  );
{'fraction_non_alphanumeric': 0.2121057985757884, 'fraction_numerical': 0.01017293997965412, 'mean_word_length': 2.834307992202729, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1753674', 'n_tokens_mistral': 661, 'n_tokens_neox': 639, 'n_words': 148}