index.js 902 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * type-name - Just a reasonable typeof
  3. *
  4. * https://github.com/twada/type-name
  5. *
  6. * Copyright (c) 2014-2016 Takuto Wada
  7. * Licensed under the MIT license.
  8. * https://github.com/twada/type-name/blob/master/LICENSE
  9. */
  10. 'use strict';
  11. var toStr = Object.prototype.toString;
  12. function funcName (f) {
  13. if (f.name) {
  14. return f.name;
  15. }
  16. var match = /^\s*function\s*([^\(]*)/im.exec(f.toString());
  17. return match ? match[1] : '';
  18. }
  19. function ctorName (obj) {
  20. var strName = toStr.call(obj).slice(8, -1);
  21. if ((strName === 'Object' || strName === 'Error') && obj.constructor) {
  22. return funcName(obj.constructor);
  23. }
  24. return strName;
  25. }
  26. function typeName (val) {
  27. var type;
  28. if (val === null) {
  29. return 'null';
  30. }
  31. type = typeof val;
  32. if (type === 'object') {
  33. return ctorName(val);
  34. }
  35. return type;
  36. }
  37. module.exports = typeName;