memoizeStringOnly.js.flow 655 B

12345678910111213141516171819202122232425262728
  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 memoizeStringOnly
  8. * @flow
  9. * @typechecks static-only
  10. */
  11. 'use strict';
  12. /**
  13. * Memoizes the return value of a function that accepts one string argument.
  14. */
  15. function memoizeStringOnly<T>(callback: (s: string) => T): (s: string) => T {
  16. const cache = {};
  17. return function (string) {
  18. if (!cache.hasOwnProperty(string)) {
  19. cache[string] = callback.call(this, string);
  20. }
  21. return cache[string];
  22. };
  23. }
  24. module.exports = memoizeStringOnly;