memoizeStringOnly.js 572 B

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