Skip to content
This repository has been archived by the owner on Feb 5, 2018. It is now read-only.

Commit

Permalink
fix(newlines): preserve newlines in a part
Browse files Browse the repository at this point in the history
Extra newlines between parts will be trimmed.

Fixes #15
  • Loading branch information
stevemao committed Sep 12, 2015
1 parent 9962dda commit beb3d05
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
21 changes: 9 additions & 12 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 = {};
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down
41 changes: 35 additions & 6 deletions test/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit beb3d05

Please sign in to comment.