plain.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. var callable = require("es5-ext/object/valid-callable")
  3. , forEach = require("es5-ext/object/for-each")
  4. , extensions = require("./lib/registered-extensions")
  5. , configure = require("./lib/configure-map")
  6. , resolveLength = require("./lib/resolve-length");
  7. module.exports = function self(fn /*, options */) {
  8. var options, length, conf;
  9. callable(fn);
  10. options = Object(arguments[1]);
  11. if (options.async && options.promise) {
  12. throw new Error("Options 'async' and 'promise' cannot be used together");
  13. }
  14. // Do not memoize already memoized function
  15. if (hasOwnProperty.call(fn, "__memoized__") && !options.force) return fn;
  16. // Resolve length;
  17. length = resolveLength(options.length, fn.length, options.async && extensions.async);
  18. // Configure cache map
  19. conf = configure(fn, length, options);
  20. // Bind eventual extensions
  21. forEach(extensions, function (extFn, name) {
  22. if (options[name]) extFn(options[name], conf, options);
  23. });
  24. if (self.__profiler__) self.__profiler__(conf);
  25. conf.updateEnv();
  26. return conf.memoized;
  27. };