Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit aa1a3ea

Browse files
author
Charlike Mike Reagent
committed
fix: add docs
resolves #1 Signed-off-by: Charlike Mike Reagent <olsten.larck@gmail.com>
1 parent da4d07a commit aa1a3ea

File tree

4 files changed

+264
-4
lines changed

4 files changed

+264
-4
lines changed

.verb.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ For bugs reports and feature requests, [please create an issue][open-issue-url]
3333

3434
Project is [semantically](https://semver.org) & automatically released on [CircleCI][codecoverage-url] with [new-release][] and its [New Release](https://github.com/apps/new-release) GitHub App.
3535

36+
## Highlights
37+
38+
- [Conventional Commits](https://conventionalcommits.org/) compliant
39+
- Fast and lightweight in few lines of code
40+
- Infinitely extensible through plugins, [two built-in](#mappers)
41+
- Collecting mentions from commit message
42+
- Detection of breaking changes in commit
3643

3744
## Table of Contents
3845
<!-- toc -->

README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,20 @@ For bugs reports and feature requests, [please create an issue][open-issue-url]
3333

3434
Project is [semantically](https://semver.org) & automatically released on [CircleCI][codecoverage-url] with [new-release][] and its [New Release](https://github.com/apps/new-release) GitHub App.
3535

36+
## Highlights
37+
38+
- [Conventional Commits](https://conventionalcommits.org/) compliant
39+
- Fast and lightweight in few lines of code
40+
- Infinitely extensible through plugins, [two built-in](#mappers)
41+
- Collecting mentions from commit message
42+
- Detection of breaking changes in commit
43+
3644
## Table of Contents
3745
- [Install](#install)
3846
- [API](#api)
47+
* [.parse](#parse)
48+
* [.mappers](#mappers)
49+
* [.plugins](#plugins)
3950
- [Related Projects](#related-projects)
4051
- [Contributing](#contributing)
4152
- [Contributors](#contributors)
@@ -52,12 +63,130 @@ $ yarn add parse-commit-message
5263

5364
## API
5465

66+
### [.parse](src/index.js#L72)
67+
Parses given `commitMessage` to an object which can be populated with more things if needed, through `plugins`.
68+
69+
Plugins are functions like `(commit) => {}` and can return object with additional
70+
properties that will be included in the returned "commit" object.
71+
There are two built-in plugins - `increment` and `mentions` which are exposed
72+
as array at exposed `plugins` named export.
73+
The `commit.header` has also a `toString()` method concatinates
74+
the `header.scope`, `header.type` and `header.subject` properties.
75+
76+
**Params**
77+
78+
* `commitMessage` **{string}**: required, a whole commit message
79+
* `plugins` **{Array}**: optional, a list of functions that are passed with `commit` object
80+
* `returns` **{Object}**: with `{ header: { type, scope, subject }, body, footer }`
81+
82+
**Example**
83+
84+
```js
85+
import { parse } from 'parse-commit-message';
86+
87+
const commit = parse(`fix(crit): some huge change
88+
89+
Some awesome
90+
body here.
91+
92+
resolves #333
93+
`);
94+
95+
console.log(commit)
96+
// => {
97+
// header: {
98+
// type: 'fix',
99+
// scope: 'crit',
100+
// subject: 'some huge change'
101+
// toString: [Function: toString],
102+
// },
103+
// body: 'Some awesome\nbody here.',
104+
// footer: 'resolves #333',
105+
// }
106+
107+
console.log(commit.header.toString())
108+
// => 'fix(crit): some huge change'
109+
110+
// or adding one more plugin to the builtin ones
111+
const customPlugin = (commit) => {
112+
if (commit.header.type === 'fix') {
113+
return { fixed: 'yeah' };
114+
}
115+
return null;
116+
};
117+
const commit = parse('fix(wat): foo bar baz', plugins.concat(customPlugin));
118+
119+
console.log(commit.isBreaking) // => false
120+
console.log(commit.increment) // => 'patch'
121+
console.log(commit.header); // => { type: 'fix', subject: 'wat', subject: 'foo bar baz' }
122+
console.log(commit.fixed); // => 'yeah'
123+
```
124+
125+
### [.mappers](src/index.js#L228)
126+
An object with all mappers, such as `plugins` array, but named. This objects is like `{ increment, mentions }` where they are plugins that can be passed as second argument to `.parse`.
127+
128+
1. The `mappers.increment` adds `isBreaking` and `increment` properties
129+
to the final returned "commit" object:
130+
- `isBreaking` is `Boolean` that indicates if commit is containing `BREAKING CHANGE: ` or
131+
the `type` of the commit is `break`, `breaking` or `major`
132+
- `increment` is a `String` that can be `'patch'`, `'minor'` or `'major'`
133+
2. The `mappers.mentions` adds `mentions` property to the end result object
134+
- `mentions` is an array of objects like `{ handle: String, mention: String, index: Number }`,
135+
see [collect-mentions][]
136+
137+
**Example**
138+
139+
```js
140+
import { parse, mappers } from 'parse-commit-message';
141+
142+
const commit = parse('fix: BREAKING CHANGE: huge refactor', mappers.increment);
143+
144+
console.log(commit);
145+
// => {
146+
// header: { type: 'fix', scope: undefined, subject: 'BREAKING CHANGE: huge refactor' },
147+
// body: null,
148+
// footer: null,
149+
// isBreaking: true,
150+
// increment: 'major'
151+
// }
152+
153+
const str = `feat(whoa): thanks to @foobie for this
154+
155+
awesome @zazzy and @quxie make this release to happen
156+
157+
resolves #123
158+
`
159+
const cmt = parse(str, mappers.mentions);
160+
161+
console.log(cmt.header.type); // => 'feat'
162+
console.log(cmt.header.scope); // => 'whoa'
163+
console.log(cmt.header.subject); // => 'hanks to @foobie for this'
164+
console.log(cmt.body); // => 'awesome @zazzy and @quxie make this release to happen'
165+
console.log(cmt.footer); // => 'resolves #123'
166+
console.log(cmt.mentions[0]); // => { handle: '@foobie', mention: 'foobie' }
167+
console.log(cmt.mentions[1]); // => { handle: '@zazzy', mention: 'zazzy' }
168+
console.log(cmt.mentions[2]); // => { handle: '@quxie', mention: 'quxie' }
169+
```
170+
171+
### [.plugins](src/index.js#L250)
172+
A list of all plugins, such as `mappers` but no names, so can be passed directly to the `.parse` as second argument.
173+
174+
**Example**
175+
176+
```js
177+
import { parse, plugins } from 'parse-commit-message';
178+
179+
const commit = parse('fix: okey', plugins)
180+
console.log(commit)
181+
```
182+
55183
**[back to top](#thetop)**
56184

57185
## Related Projects
58186
Some of these projects are used here or were inspiration for this one, others are just related. So, thanks for your existance!
59187
- [asia](https://www.npmjs.com/package/asia): Blazingly fast, magical and minimalist testing framework, for Today and Tomorrow | [homepage](https://github.com/olstenlarck/asia#readme "Blazingly fast, magical and minimalist testing framework, for Today and Tomorrow")
60188
- [charlike](https://www.npmjs.com/package/charlike): Small, fast, simple and streaming project scaffolder for myself, but not… [more](https://github.com/tunnckoCore/charlike) | [homepage](https://github.com/tunnckoCore/charlike "Small, fast, simple and streaming project scaffolder for myself, but not only. Supports hundreds of template engines through the @JSTransformers API or if you want custom `render` function passed through options")
189+
- [collect-mentions](https://www.npmjs.com/package/collect-mentions): Collect mentions from a given text string, using battle-tested `mentions-regex` package | [homepage](https://github.com/olstenlarck/collect-mentions "Collect mentions from a given text string, using battle-tested `mentions-regex` package")
61190
- [gitcommit](https://www.npmjs.com/package/gitcommit): Lightweight and joyful `git commit` replacement. Conventional Commits compliant. | [homepage](https://github.com/tunnckoCore/gitcommit "Lightweight and joyful `git commit` replacement. Conventional Commits compliant.")
62191
- [new-release](https://www.npmjs.com/package/new-release): A stable alternative to [semantic-release][]. Only handles NPM publishing and nothing… [more](https://github.com/tunnckoCore/new-release#readme) | [homepage](https://github.com/tunnckoCore/new-release#readme "A stable alternative to [semantic-release][]. Only handles NPM publishing and nothing more. For creating GitHub releases use the Semantic Release GitHub App")
63192
- [xaxa](https://www.npmjs.com/package/xaxa): Zero-config linting, powered by few amazing unicorns, AirBnB & Prettier. | [homepage](https://github.com/olstenlarck/xaxa "Zero-config linting, powered by few amazing unicorns, AirBnB & Prettier.")
@@ -133,5 +262,6 @@ _This file was generated by [verb-generate-readme](https://github.com/verbose/ve
133262
[open-issue-url]: https://github.com/olstenlarck/parse-commit-message/issues/new
134263
[author-link]: https://i.am.charlike.online
135264

265+
[collect-mentions]: https://github.com/olstenlarck/collect-mentions
136266
[new-release]: https://github.com/tunnckoCore/new-release
137267
[semantic-release]: https://github.com/semantic-release/semantic-release

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"gitcommit",
8888
"new-release",
8989
"xaxa",
90+
"collect-mentions",
9091
"charlike"
9192
]
9293
},
@@ -98,4 +99,4 @@
9899
"semantic-release"
99100
]
100101
}
101-
}
102+
}

src/index.js

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,68 @@ import arrayify from 'arrayify';
77
import collectMentions from 'collect-mentions';
88

99
/**
10+
* Parses given `commitMessage` to an object which can be populated with
11+
* more things if needed, through `plugins`.
1012
*
11-
* @param {string} commitMessage required, a whole commit message
12-
* @param {Array} plugins optional, a list of functions that get passed with `commit` object
13+
* Plugins are functions like `(commit) => {}` and can return object with additional
14+
* properties that will be included in the returned "commit" object.
15+
*
16+
* There are two built-in plugins - `increment` and `mentions` which are exposed
17+
* as array at exposed `plugins` named export.
18+
*
19+
* The `commit.header` has also a `toString()` method concatinates
20+
* the `header.scope`, `header.type` and `header.subject` properties.
21+
*
22+
* **Example**
23+
*
24+
* ```js
25+
* import { parse } from 'parse-commit-message';
26+
*
27+
* const commit = parse(`fix(crit): some huge change
28+
*
29+
* Some awesome
30+
* body here.
31+
*
32+
* resolves #333
33+
* `);
34+
*
35+
* console.log(commit)
36+
* // => {
37+
* // header: {
38+
* // type: 'fix',
39+
* // scope: 'crit',
40+
* // subject: 'some huge change'
41+
* // toString: [Function: toString],
42+
* // },
43+
* // body: 'Some awesome\nbody here.',
44+
* // footer: 'resolves #333',
45+
* // }
46+
*
47+
* console.log(commit.header.toString())
48+
* // => 'fix(crit): some huge change'
49+
*
50+
* // or adding one more plugin to the builtin ones
51+
* const customPlugin = (commit) => {
52+
* if (commit.header.type === 'fix') {
53+
* return { fixed: 'yeah' };
54+
* }
55+
* return null;
56+
* };
57+
* const commit = parse('fix(wat): foo bar baz', plugins.concat(customPlugin));
58+
*
59+
* console.log(commit.isBreaking) // => false
60+
* console.log(commit.increment) // => 'patch'
61+
* console.log(commit.header); // => { type: 'fix', subject: 'wat', subject: 'foo bar baz' }
62+
* console.log(commit.fixed); // => 'yeah'
63+
* ```
64+
*
65+
* @name .parse
66+
* @param {string} `commitMessage` required, a whole commit message
67+
* @param {Array} `plugins` optional, a list of functions that are passed with `commit` object
68+
* @return {Object} with `{ header: { type, scope, subject }, body, footer }`
69+
* @api public
1370
*/
71+
1472
function parse(commitMessage, plugins) {
1573
if (typeof commitMessage !== 'string') {
1674
throw new TypeError(
@@ -116,15 +174,79 @@ function mentionsMapper({ header, body, footer }) {
116174

117175
/**
118176
* An object with all mappers, such as `plugins` array, but named.
177+
* This objects is like `{ increment, mentions }` where they are plugins
178+
* that can be passed as second argument to `.parse`.
179+
*
180+
* 1. The `mappers.increment` adds `isBreaking` and `increment` properties
181+
* to the final returned "commit" object:
182+
* - `isBreaking` is `Boolean` that indicates if commit is containing `BREAKING CHANGE: ` or
183+
* the `type` of the commit is `break`, `breaking` or `major`
184+
* - `increment` is a `String` that can be `'patch'`, `'minor'` or `'major'`
185+
*
186+
* 2. The `mappers.mentions` adds `mentions` property to the end result object
187+
* - `mentions` is an array of objects like `{ handle: String, mention: String, index: Number }`,
188+
* see [collect-mentions][]
189+
*
190+
* **Example**
191+
*
192+
* ```js
193+
* import { parse, mappers } from 'parse-commit-message';
194+
*
195+
* const commit = parse('fix: BREAKING CHANGE: huge refactor', mappers.increment);
196+
*
197+
* console.log(commit);
198+
* // => {
199+
* // header: { type: 'fix', scope: undefined, subject: 'BREAKING CHANGE: huge refactor' },
200+
* // body: null,
201+
* // footer: null,
202+
* // isBreaking: true,
203+
* // increment: 'major'
204+
* // }
205+
*
206+
* const str = `feat(whoa): thanks to @foobie for this
207+
*
208+
* awesome @zazzy and @quxie make this release to happen
209+
*
210+
* resolves #123
211+
* `
212+
* const cmt = parse(str, mappers.mentions);
213+
*
214+
* console.log(cmt.header.type); // => 'feat'
215+
* console.log(cmt.header.scope); // => 'whoa'
216+
* console.log(cmt.header.subject); // => 'hanks to @foobie for this'
217+
* console.log(cmt.body); // => 'awesome @zazzy and @quxie make this release to happen'
218+
* console.log(cmt.footer); // => 'resolves #123'
219+
* console.log(cmt.mentions[0]); // => { handle: '@foobie', mention: 'foobie' }
220+
* console.log(cmt.mentions[1]); // => { handle: '@zazzy', mention: 'zazzy' }
221+
* console.log(cmt.mentions[2]); // => { handle: '@quxie', mention: 'quxie' }
222+
* ```
223+
*
224+
* @name .mappers
225+
* @api public
119226
*/
227+
120228
const mappers = {
121229
increment: incrementMapper,
122230
mentions: mentionsMapper,
123231
};
124232

125233
/**
126-
* A list of all plugins, such as `mappers` but no names.
234+
* A list of all plugins, such as `mappers` but no names,
235+
* so can be passed directly to the `.parse` as second argument.
236+
*
237+
* **Example**
238+
*
239+
* ```js
240+
* import { parse, plugins } from 'parse-commit-message';
241+
*
242+
* const commit = parse('fix: okey', plugins)
243+
* console.log(commit)
244+
* ```
245+
*
246+
* @name .plugins
247+
* @api public
127248
*/
249+
128250
const plugins = Object.keys(mappers).map((name) => mappers[name]);
129251

130252
/**

0 commit comments

Comments
 (0)