index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*!
  2. * extract-comments <https://github.com/jonschlinkert/extract-comments>
  3. *
  4. * Copyright (c) 2014 Jon Schlinkert, contributors.
  5. * Licensed under the MIT license.
  6. */
  7. 'use strict';
  8. var extend = require('extend-shallow');
  9. var Comments = require('./lib/comments');
  10. /**
  11. * Extract comments from the given `string`.
  12. *
  13. * ```js
  14. * extract(str, options);
  15. * ```
  16. * @param {String} `string`
  17. * @param {Object} `options` Pass `first: true` to return after the first comment is found.
  18. * @return {String}
  19. * @api public
  20. */
  21. function extract(str, options, fn) {
  22. if (typeof options === 'function') {
  23. fn = options;
  24. options = {};
  25. }
  26. var extracted = new Comments(options, fn);
  27. var res = extracted.extract(str);
  28. return res.comments || [];
  29. }
  30. /**
  31. * Extract block comments from the given `string`.
  32. *
  33. * ```js
  34. * extract.block(str, options);
  35. * ```
  36. * @name .block
  37. * @param {String} `string`
  38. * @param {Object} `options` Pass `first: true` to return after the first comment is found.
  39. * @return {String}
  40. * @api public
  41. */
  42. function block(str, options) {
  43. return extract(str, extend({line: false}, options));
  44. }
  45. /**
  46. * Extract line comments from the given `string`.
  47. *
  48. * ```js
  49. * extract.line(str, options);
  50. * ```
  51. * @name .line
  52. * @param {String} `string`
  53. * @param {Object} `options` Pass `first: true` to return after the first comment is found.
  54. * @return {String}
  55. * @api public
  56. */
  57. function line(str, options) {
  58. return extract(str, extend({block: false}, options));
  59. }
  60. /**
  61. * Extract the first comment from the given `string`.
  62. *
  63. * @name .first
  64. * @param {String} `string`
  65. * @param {Object} `options` Pass `first: true` to return after the first comment is found.
  66. * @return {String}
  67. * @api public
  68. */
  69. function first(str) {
  70. return extract(str, {first: true});
  71. }
  72. /**
  73. * Expose `extract`
  74. */
  75. module.exports = extract;
  76. /**
  77. * Expose convenience methods
  78. */
  79. module.exports.block = block;
  80. module.exports.line = line;
  81. module.exports.first = first;
  82. /**
  83. * Expose `Comments` constructor, to
  84. * allow custom plugins to be registered.
  85. */
  86. module.exports.Comments = Comments;