regex.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict'
  2. const reNomatch = /(?!.*)/
  3. function join (array, joiner) {
  4. return array
  5. .map(function (val) {
  6. return val.trim()
  7. })
  8. .filter(function (val) {
  9. return val.length
  10. })
  11. .join(joiner)
  12. }
  13. function getNotesRegex (noteKeywords, notesPattern) {
  14. if (!noteKeywords) {
  15. return reNomatch
  16. }
  17. const noteKeywordsSelection = join(noteKeywords, '|')
  18. if (!notesPattern) {
  19. return new RegExp('^[\\s|*]*(' + noteKeywordsSelection + ')[:\\s]+(.*)', 'i')
  20. }
  21. return notesPattern(noteKeywordsSelection)
  22. }
  23. function getReferencePartsRegex (issuePrefixes, issuePrefixesCaseSensitive) {
  24. if (!issuePrefixes) {
  25. return reNomatch
  26. }
  27. const flags = issuePrefixesCaseSensitive ? 'g' : 'gi'
  28. return new RegExp('(?:.*?)??\\s*([\\w-\\.\\/]*?)??(' + join(issuePrefixes, '|') + ')([\\w-]*\\d+)', flags)
  29. }
  30. function getReferencesRegex (referenceActions) {
  31. if (!referenceActions) {
  32. // matches everything
  33. return /()(.+)/gi
  34. }
  35. const joinedKeywords = join(referenceActions, '|')
  36. return new RegExp('(' + joinedKeywords + ')(?:\\s+(.*?))(?=(?:' + joinedKeywords + ')|$)', 'gi')
  37. }
  38. module.exports = function (options) {
  39. options = options || {}
  40. const reNotes = getNotesRegex(options.noteKeywords, options.notesPattern)
  41. const reReferenceParts = getReferencePartsRegex(options.issuePrefixes, options.issuePrefixesCaseSensitive)
  42. const reReferences = getReferencesRegex(options.referenceActions)
  43. return {
  44. notes: reNotes,
  45. referenceParts: reReferenceParts,
  46. references: reReferences,
  47. mentions: /@([\w-]+)/g
  48. }
  49. }