function.js 747 B

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