partitionObject.js.flow 857 B

123456789101112131415161718192021222324252627282930313233343536373839
  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: {
  16. [key: string]: Tv
  17. }, callback: (value: Tv, key: string, object: {
  18. [key: string]: Tv
  19. }) => boolean, context?: any): [{
  20. [key: string]: Tv
  21. }, {
  22. [key: string]: Tv
  23. }] {
  24. var first = {};
  25. var second = {};
  26. forEachObject(object, (value, key) => {
  27. if (callback.call(context, value, key, object)) {
  28. first[key] = value;
  29. } else {
  30. second[key] = value;
  31. }
  32. });
  33. return [first, second];
  34. }
  35. module.exports = partitionObject;