partitionObject.js.flow 849 B

12345678910111213141516171819202122232425262728293031
  1. /**
  2. * Copyright 2015-present Facebook. All Rights Reserved.
  3. *
  4. * @providesModule partitionObject
  5. * @typechecks
  6. * @flow
  7. */
  8. 'use strict';
  9. var forEachObject = require('./forEachObject');
  10. /**
  11. * Partitions an object given a predicate. All elements satisfying the predicate
  12. * are part of the first returned object, and all elements that don't are in the
  13. * second.
  14. */
  15. function partitionObject<Tv>(object: { [key: string]: Tv }, callback: (value: Tv, key: string, object: { [key: string]: Tv }) => boolean, context?: any): [{ [key: string]: Tv }, { [key: string]: Tv }] {
  16. var first = {};
  17. var second = {};
  18. forEachObject(object, (value, key) => {
  19. if (callback.call(context, value, key, object)) {
  20. first[key] = value;
  21. } else {
  22. second[key] = value;
  23. }
  24. });
  25. return [first, second];
  26. }
  27. module.exports = partitionObject;