function.js 593 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. /**
  3. * A empty function.
  4. *
  5. * @return {Function}
  6. * @public
  7. */
  8. exports.noop = function noop() {};
  9. /**
  10. * Get a function parameter's names.
  11. *
  12. * @param {Function} func
  13. * @param {Boolean} [useCache], default is true
  14. * @return {Array} names
  15. */
  16. exports.getParamNames = function getParamNames(func, cache) {
  17. cache = cache !== false;
  18. if (cache && func.__cache_names) {
  19. return func.__cache_names;
  20. }
  21. var str = func.toString();
  22. var names = str.slice(str.indexOf('(') + 1, str.indexOf(')')).match(/([^\s,]+)/g) || [];
  23. func.__cache_names = names;
  24. return names;
  25. };