You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
639 B

  1. // @flow
  2. import type { Modifier } from '../types';
  3. export default function mergeByName(
  4. modifiers: Array<$Shape<Modifier<any, any>>>
  5. ): Array<$Shape<Modifier<any, any>>> {
  6. const merged = modifiers.reduce((merged, current) => {
  7. const existing = merged[current.name];
  8. merged[current.name] = existing
  9. ? {
  10. ...existing,
  11. ...current,
  12. options: { ...existing.options, ...current.options },
  13. data: { ...existing.data, ...current.data },
  14. }
  15. : current;
  16. return merged;
  17. }, {});
  18. // IE11 does not support Object.values
  19. return Object.keys(merged).map(key => merged[key]);
  20. }