"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.memoize = exports["default"] = void 0;
var _isIdentical = _interopRequireDefault(require("../Array/isIdentical"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
/**
 * Memoizes the return value of a function for the given arguments
 * Currently it stores only the result for the last call, it's really simple, fast and also saves memory.
 * It's different to the lodash (underscore) implementation, because that uses only the first
 * argument of the memoized function by default, and provides a way for advanced cache resolving.
 * A possible improvement can be in the future the possibility of optionally using a "map"
 * to store the results for every different input value (or limited number).
 * @param {function} fn
 * @returns {function(...[*]=)}
 * @throws TypeError If the given argument is not a function
 */
var memoize = exports.memoize = function memoize(fn) {
  if (typeof fn !== 'function') {
    throw new TypeError('Memoize accepts only function as argument!');
  }
  var prevArgs = [];
  var value = undefined;
  return function () {
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    if (!(0, _isIdentical["default"])(args, prevArgs)) {
      value = fn.apply(void 0, args);
      prevArgs = args;
    }
    return value;
  };
};
var _default = exports["default"] = memoize;