someObject.js.flow 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @providesModule someObject
  8. * @flow
  9. * @typechecks
  10. */
  11. 'use strict';
  12. var hasOwnProperty = Object.prototype.hasOwnProperty;
  13. /**
  14. * Executes the provided `callback` once for each enumerable own property in the
  15. * object until it finds one where callback returns a truthy value. If such a
  16. * property is found, `someObject` immediately returns true. Otherwise, it
  17. * returns false.
  18. *
  19. * The `callback` is invoked with three arguments:
  20. *
  21. * - the property value
  22. * - the property name
  23. * - the object being traversed
  24. *
  25. * Properties that are added after the call to `someObject` will not be
  26. * visited by `callback`. If the values of existing properties are changed, the
  27. * value passed to `callback` will be the value at the time `someObject`
  28. * visits them. Properties that are deleted before being visited are not
  29. * visited.
  30. */
  31. function someObject(object: ?Object, callback: (value: any, name: string, object: Object) => any, context?: any): boolean {
  32. for (var name in object) {
  33. if (hasOwnProperty.call(object, name)) {
  34. if (callback.call(context, object[name], name, object)) {
  35. return true;
  36. }
  37. }
  38. }
  39. return false;
  40. }
  41. module.exports = someObject;