index.js 844 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*!
  2. * parse-author <https://github.com/jonschlinkert/parse-author>
  3. *
  4. * Copyright (c) 2014-2017, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. 'use strict';
  8. var regex = require('author-regex');
  9. module.exports = function(str) {
  10. if (typeof str !== 'string') {
  11. throw new TypeError('expected author to be a string');
  12. }
  13. if (!str || !/\w/.test(str)) {
  14. return {};
  15. }
  16. var match = [].concat.apply([], regex().exec(str));
  17. var author = {};
  18. if (match[1]) {
  19. author.name = match[1];
  20. }
  21. for (var i = 2; i < match.length; i++) {
  22. var val = match[i];
  23. if (i % 2 === 0 && val && match[i + 1]) {
  24. if (val.charAt(0) === '<') {
  25. author.email = match[i + 1];
  26. i++;
  27. } else if (val.charAt(0) === '(') {
  28. author.url = match[i + 1];
  29. i++;
  30. }
  31. }
  32. }
  33. return author;
  34. };