diff --git a/lib/parser.js b/lib/parser.js index 8b6501b..b003da1 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -30,7 +30,7 @@ function parser(raw, options, regex) { var currentProcessedField; var revertMatch; var otherFields = {}; - var lines = _.compact(raw.split('\n')); + var lines = raw.trim().split('\n'); var continueNote = false; var isBody = true; var headerCorrespondence = _.map(options.headerCorrespondence, function(part) { @@ -44,8 +44,6 @@ function parser(raw, options, regex) { var reReferenceParts = regex.referenceParts; var reReferences = regex.references; - raw = lines.join('\n'); - // msg parts var header = lines.shift(); var headerParts = {}; @@ -194,13 +192,6 @@ function parser(raw, options, regex) { } }); - if (!body) { - body = null; - } - if (!footer) { - footer = null; - } - // does this commit revert any other commit? revertMatch = raw.match(options.revertPattern); @@ -214,10 +205,16 @@ function parser(raw, options, regex) { revert = null; } + _.map(notes, function(note) { + note.text = note.text.trim(); + + return note; + }); + var msg = _.merge(headerParts, { header: header, - body: body, - footer: footer, + body: body ? body.trim() : null, + footer: footer ? footer.trim() : null, notes: notes, references: references, revert: revert diff --git a/test/parser.spec.js b/test/parser.spec.js index 507d6c9..5904b77 100644 --- a/test/parser.spec.js +++ b/test/parser.spec.js @@ -95,18 +95,47 @@ describe('parser', function() { }); it('should trim extra newlines', function() { - expect(msg).to.eql(parser( + expect(parser( '\n\n\n\n\n\n\nfeat(scope): broadcast $destroy event on scope destruction\n\n\n' + '\n\n\nperf testing shows that in chrome this change adds 5-15% overhead\n' + '\n\n\nwhen destroying 10k nested scopes where each scope has a $destroy listener\n\n' + '\n\n\n\nBREAKING AMEND: some breaking change\n' + - '\n\nKills #1, #123\n' + - '\n\n\nkilled #25\n\n\n\n\n' + - '\nhandle #33, Closes #100, Handled #3 kills repo#77\n' + - 'kills stevemao/conventional-commits-parser#1', + '\n\n\n\nBREAKING AMEND: An awesome breaking change\n\n\n```\ncode here\n```' + + '\n\nKills #1\n' + + '\n\n\nkilled #25\n\n\n\n\n', options, reg - )); + )).to.eql({ + header: 'feat(scope): broadcast $destroy event on scope destruction', + body: 'perf testing shows that in chrome this change adds 5-15% overhead\n\n\n\nwhen destroying 10k nested scopes where each scope has a $destroy listener', + footer: 'BREAKING AMEND: some breaking change\n\n\n\n\nBREAKING AMEND: An awesome breaking change\n\n\n```\ncode here\n```\n\nKills #1\n\n\n\nkilled #25', + notes: [{ + title: 'BREAKING AMEND', + text: 'some breaking change' + }, { + title: 'BREAKING AMEND', + text: 'An awesome breaking change\n\n\n```\ncode here\n```' + }], + references: [{ + action: 'Kills', + owner: null, + repository: null, + issue: '1', + raw: '#1', + prefix: '#' + }, { + action: 'killed', + owner: null, + repository: null, + issue: '25', + raw: '#25', + prefix: '#' + }], + revert: null, + scope: 'scope', + subject: 'broadcast $destroy event on scope destruction', + type: 'feat' + }); }); describe('header', function() {