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

Commit

Permalink
feat(options): all options can be a string
Browse files Browse the repository at this point in the history
This allows cli to work with options.
  • Loading branch information
stevemao committed Mar 9, 2015
1 parent 9d607d6 commit 2e108e4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 25 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ The maximum subject length.

##### headerPattern

Type: `regex` Default: `/^(\w*)(?:\(([\w\$\.\-\* ]*)\))?\: (.*)$/`
Type: `regex` or `string` Default: `/^(\w*)(?:\(([\w\$\.\-\* ]*)\))?\: (.*)$/`

Used to match header pattern. The first capturing group captures **type**, second captures **scope** and third captures **subject**
Used to match header pattern. The first capturing group captures **type**, second captures **scope** and third captures **subject**. If it's a `string` it will be converted to a `regex`.

##### closeKeywords

Expand All @@ -133,15 +133,15 @@ Type: `array` or `string` Default:
'resolved'
]`

This value is case **insensitive**.
This value is case **insensitive**. If it's a `string` it will be converted to an `array` separated by a comma.

Keywords that used to close issues.

##### breakKeywords

Type: `array` or `string` Default: `['BREAKING CHANGE']`

Keywords for breaking changes.
Keywords for breaking changes. If it's a `string` it will be converted to an `array` separated by a comma.


## CLI
Expand Down
27 changes: 17 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
'use strict';
var extend = require('lodash').extend;
var _ = require('lodash');
var parser = require('./lib/parser');
var through = require('through2');

function conventionalCommitsParser(options) {
options = extend({
if (options && !_.isEmpty(options)) {
var headerPattern = options.headerPattern;
if (typeof headerPattern === 'string') {
options.headerPattern = new RegExp(headerPattern);
}

if (typeof options.closeKeywords === 'string') {
options.closeKeywords = options.closeKeywords.split(',');
}

if (typeof options.breakKeywords === 'string') {
options.breakKeywords = options.breakKeywords.split(',');
}
}

options = _.extend({
maxSubjectLength: 80,
headerPattern: /^(\w*)(?:\(([\w\$\.\-\* ]*)\))?\: (.*)$/,
closeKeywords: [
Expand All @@ -23,14 +38,6 @@ function conventionalCommitsParser(options) {
]
}, options || {});

if (!Array.isArray(options.closeKeywords)) {
options.closeKeywords = [options.closeKeywords];
}

if (!Array.isArray(options.breakKeywords)) {
options.breakKeywords = [options.breakKeywords];
}

return through.obj(function(data, enc, cb) {
var commit = parser(data.toString(), options);

Expand Down
61 changes: 50 additions & 11 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,36 +83,75 @@ describe('conventionalCommitsParser', function() {
}));
});

var commits = [
'13f31602f396bc269076ab4d389cfd8ca94b20ba\n' +
'feat(ng-list) Allow custom separator\n' +
'bla bla bla\n\n' +
'Fix #123\nCloses #25\nfix #33\n',

'13f31602f396bc269076ab4d389cfd8ca94b20ba\n' +
'fix(ng-list) Another custom separator\n' +
'bla bla bla\n\n' +
'BREAKING CHANGES: some breaking changes\n',
];

it('should take options', function(done) {
var stream = through();
var commits = [
'13f31602f396bc269076ab4d389cfd8ca94b20ba\n' +
'feat(ng-list): Allow custom separator\n' +
'bla bla bla\n\n' +
'Fix #123\nCloses #25\nfix #33\n',

'13f31602f396bc269076ab4d389cfd8ca94b20ba\n' +
'feat(ng-list): Allow custom separator\n' +
'bla bla bla\n\n' +
'BREAKING CHANGES: some breaking changes\n',
];
var length = commits.length;

forEach(commits, function(commit) {
stream.write(commit);
});
stream.end();

stream
.pipe(conventionalCommitsParser({
headerPattern: /^(\w*)(?:\(([\w\$\.\-\* ]*)\))?\ (.*)$/,
closeKeywords: ['fix'],
breakKeywords: ['BREAKING CHANGES']
}))
.pipe(through.obj(function(chunk, enc, cb) {
if (--length === 1) {
expect(chunk.type).to.equal('feat');
expect(chunk.scope).to.equal('ng-list');
expect(chunk.subject).to.equal('Allow custom separator');
expect(chunk.closes).to.eql([123, 33]);
} else {
expect(chunk.type).to.equal('fix');
expect(chunk.scope).to.equal('ng-list');
expect(chunk.subject).to.equal('Another custom separator');
expect(chunk.breaks['BREAKING CHANGES']).to.equal('some breaking changes');
done();
}
cb();
}));
});

it('should take string options', function(done) {
var stream = through();
var length = commits.length;

forEach(commits, function(commit) {
stream.write(commit);
});
stream.end();

stream
.pipe(conventionalCommitsParser({
headerPattern: '^(\\w*)(?:\\(([\\w\\$\\.\\-\\* ]*)\\))?\\ (.*)$',
closeKeywords: 'fix',
breakKeywords: 'BREAKING CHANGES'
}))
.pipe(through.obj(function(chunk, enc, cb) {
if (--length === 1) {
expect(chunk.type).to.equal('feat');
expect(chunk.scope).to.equal('ng-list');
expect(chunk.subject).to.equal('Allow custom separator');
expect(chunk.closes).to.eql([123, 33]);
} else {
expect(chunk.type).to.equal('fix');
expect(chunk.scope).to.equal('ng-list');
expect(chunk.subject).to.equal('Another custom separator');
expect(chunk.breaks['BREAKING CHANGES']).to.equal('some breaking changes');
done();
}
Expand Down

0 comments on commit 2e108e4

Please sign in to comment.