emptyFunction.js.flow 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  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 emptyFunction
  8. * @flow
  9. */
  10. function makeEmptyFunction<T>(arg: T): (...args: Array<any>) => T {
  11. return function () {
  12. return arg;
  13. };
  14. }
  15. /**
  16. * This function accepts and discards inputs; it has no side effects. This is
  17. * primarily useful idiomatically for overridable function endpoints which
  18. * always need to be callable, since JS lacks a null-call idiom ala Cocoa.
  19. */
  20. const emptyFunction: (...args: Array<any>) => void = function () {};
  21. emptyFunction.thatReturns = makeEmptyFunction;
  22. emptyFunction.thatReturnsFalse = makeEmptyFunction(false);
  23. emptyFunction.thatReturnsTrue = makeEmptyFunction(true);
  24. emptyFunction.thatReturnsNull = makeEmptyFunction(null);
  25. emptyFunction.thatReturnsThis = function () {
  26. return this;
  27. };
  28. emptyFunction.thatReturnsArgument = function (arg) {
  29. return arg;
  30. };
  31. module.exports = emptyFunction;