index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict'
  2. const isMatch = require('lodash.ismatch')
  3. const modifyValues = require('modify-values')
  4. function modifyValue (val) {
  5. if (typeof val === 'string') {
  6. return val.trim()
  7. }
  8. return val
  9. }
  10. function conventionalCommitsFilter (commits) {
  11. if (!Array.isArray(commits)) {
  12. throw new TypeError('Expected an array')
  13. }
  14. let ret = []
  15. const ignores = []
  16. const remove = []
  17. commits.forEach(function (commit) {
  18. if (commit.revert) {
  19. ignores.push(commit)
  20. }
  21. ret.push(commit)
  22. })
  23. // Filter out reverted commits
  24. ret = ret.filter(function (commit) {
  25. let ignoreThis = false
  26. commit = commit.raw ? modifyValues(commit.raw, modifyValue) : modifyValues(commit, modifyValue)
  27. ignores.some(function (ignoreCommit) {
  28. const ignore = modifyValues(ignoreCommit.revert, modifyValue)
  29. ignoreThis = isMatch(commit, ignore)
  30. if (ignoreThis) {
  31. remove.push(ignoreCommit.hash)
  32. }
  33. return ignoreThis
  34. })
  35. return !ignoreThis
  36. })
  37. // Filter out the commits that reverted something otherwise keep the revert commits
  38. ret = ret.filter(function (commit) {
  39. return remove.indexOf(commit.hash) !== 0
  40. })
  41. return ret
  42. }
  43. module.exports = conventionalCommitsFilter