partitionObject.js 666 B

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