All files / src guard.js

100% Statements 19/19
100% Branches 15/15
100% Functions 5/5
100% Lines 14/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 601x   1x   1x                 3x   2x               4x                   2x       1x 7x 6x   2x       2x       2x         1x          
const chalk = require('chalk')
 
const constants = require('./constants')
 
const errors = {
  title: chalk.red('Enter a valid commit title'),
  message: chalk.red('Enter a valid commit message'),
  referenceGithub: chalk.red(
    'Enter the number of the reference without the #. Eg: 12'
  ),
  referenceJira: chalk.red('Enter the JIRA reference key, such as ABC-123')
}
 
const title = (title) => (!title || title.includes('`')) ? errors.title : true
 
const message = (message) => message.includes('`') ? errors.message : true
 
/**
* Checks that the given github reference is valid
* https://help.github.com/articles/autolinked-references-and-urls/
* @param {Number} ref - Represents a github reference
* @returns {Boolean}
*/
const githubGuard = (ref) => ref.match(/(^[1-9][0-9]*)+$/)
  ? true
  : errors.referenceGithub
 
/**
* Checks that the given jira reference is valid
* https://confluence.atlassian.com/jirasoftwarecloud/linking-issues-776997756.html
* @param {Number} ref - Represents a jira reference
* @returns {Boolean}
*/
const jiraGuard = (ref) => ref.match(/^([A-Z][A-Z0-9]{1,9}-[0-9]+)$/g)
  ? true
  : errors.referenceJira
 
const reference = (reference, mode) => {
  if (!reference) return true
  switch (mode) {
    case constants.GITHUB: {
      return githubGuard(reference)
    }
 
    case constants.JIRA: {
      return jiraGuard(reference)
    }
 
    default: {
      return githubGuard(reference)
    }
  }
}
 
module.exports = {
  title,
  message,
  reference
}