Skip to content

Commit 7ee0b7e

Browse files
committed
refactor: move functions to a new parseExistng module
1 parent 75966a9 commit 7ee0b7e

4 files changed

Lines changed: 134 additions & 126 deletions

File tree

src/generate/parseExisting.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Parse an existing commit message.
3+
*
4+
* This allows the pieces to be reused in the new message.
5+
*/
6+
7+
/*
8+
* Split message into prefix and description.
9+
*
10+
* Require a colon to exist to detect type prefix. i.e. 'ci' will be considered a description, but
11+
* 'ci:' will be considered a prefix. This keeps the check simpler as we don't have to match against
12+
* every type and we don't have to check if we are part of a word e.g. 'circus'.
13+
*
14+
* TODO: also support Jira number e.g. '[ABCD-123]', '[ABCD-123] my description', and '[ABCD-123]
15+
* docs: my description'.
16+
*/
17+
function _splitPrefixDesc(value: string) {
18+
let prefix: string, description: string;
19+
20+
if (value.includes(":")) {
21+
[prefix, description] = value.split(":", 2);
22+
} else {
23+
[prefix, description] = ["", value];
24+
}
25+
26+
return { prefix, description };
27+
}
28+
29+
/**
30+
* Split a prefix (before a colon) into a custom prefix and Conventional Commit type prefix.
31+
*/
32+
function _splitPrefixes(value: string) {
33+
const [customPrefix, typePrefix] = value.includes(" ")
34+
? value.split(" ", 2)
35+
: ["", value];
36+
37+
return { customPrefix, typePrefix };
38+
}
39+
40+
/**
41+
* Separate a message prefixs if any and the description.
42+
*/
43+
export function splitMsg(msg: string) {
44+
const { prefix, description } = _splitPrefixDesc(msg);
45+
const { customPrefix, typePrefix } = _splitPrefixes(prefix);
46+
47+
return { customPrefix, typePrefix, description: description.trim() };
48+
}

src/prepareCommitMsg.ts

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { lookupDiffIndexAction } from "./generate/action";
1313
import { getConventionType } from "./generate/convCommit";
1414
import { countFilesDesc } from "./generate/count";
1515
import { namedFilesDesc, oneChange } from "./generate/message";
16+
import { splitMsg } from "./generate/parseExisting";
1617
import { parseDiffIndex } from "./git/parseOutput";
1718
import { AGGREGATE_MIN, CONVENTIONAL_TYPE } from "./lib/constants";
1819
import { equal } from "./lib/utils";
@@ -35,48 +36,6 @@ export function _cleanJoin(first: string, second: string) {
3536
return `${first} ${second}`.trim();
3637
}
3738

38-
/*
39-
* Split message into prefix and description.
40-
*
41-
* Require a colon to exist to detect type prefix. i.e. 'ci' will be considered a description, but
42-
* 'ci:' will be considered a prefix. This keeps the check simpler as we don't have to match against
43-
* every type and we don't have to check if we are part of a word e.g. 'circus'.
44-
*
45-
* TODO: also support Jira number e.g. '[ABCD-123]', '[ABCD-123] my description', and '[ABCD-123]
46-
* docs: my description'.
47-
*/
48-
function _splitPrefixDesc(value: string) {
49-
let prefix: string, description: string;
50-
51-
if (value.includes(":")) {
52-
[prefix, description] = value.split(":", 2);
53-
} else {
54-
[prefix, description] = ["", value];
55-
}
56-
57-
return { prefix, description };
58-
}
59-
60-
/**
61-
* Split a prefix (before a colon) into a custom prefix and Conventional Commit type prefix.
62-
*/
63-
function _splitPrefixes(value: string) {
64-
const [customPrefix, typePrefix] = value.includes(" ")
65-
? value.split(" ", 2)
66-
: ["", value];
67-
68-
return { customPrefix, typePrefix };
69-
}
70-
71-
/**
72-
* Separate a message prefixs if any and the description.
73-
*/
74-
export function _splitMsg(msg: string) {
75-
const { prefix, description } = _splitPrefixDesc(msg);
76-
const { customPrefix, typePrefix } = _splitPrefixes(prefix);
77-
78-
return { customPrefix, typePrefix, description: description.trim() };
79-
}
8039

8140
/**
8241
* Determine the Conventional Commit type prefix for a file change.
@@ -221,7 +180,7 @@ export function _combineOldAndNew(
221180
customPrefix: oldCustomPrefix,
222181
typePrefix: oldType,
223182
description: oldDesc,
224-
} = _splitMsg(oldMsg);
183+
} = splitMsg(oldMsg);
225184

226185
const descResult = _cleanJoin(autoDesc, oldDesc);
227186

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import * as assert from "assert";
2+
import { splitMsg } from "../../generate/parseExisting";
3+
4+
describe("Split a message into components", function () {
5+
describe("#splitMsg", function () {
6+
it("handles a description alone", function () {
7+
assert.deepStrictEqual(splitMsg("abc def"), {
8+
customPrefix: "",
9+
typePrefix: "",
10+
description: "abc def",
11+
});
12+
assert.deepStrictEqual(splitMsg("[ABCD-1234]"), {
13+
customPrefix: "",
14+
typePrefix: "",
15+
description: "[ABCD-1234]",
16+
});
17+
});
18+
19+
it("handles a prefix alone", function () {
20+
assert.deepStrictEqual(splitMsg("docs:"), {
21+
customPrefix: "",
22+
typePrefix: "docs",
23+
description: "",
24+
});
25+
assert.deepStrictEqual(splitMsg("feat:"), {
26+
customPrefix: "",
27+
typePrefix: "feat",
28+
description: "",
29+
});
30+
31+
assert.deepStrictEqual(splitMsg("docs: "), {
32+
customPrefix: "",
33+
typePrefix: "docs",
34+
description: "",
35+
});
36+
});
37+
38+
it("separates a prefix and description", function () {
39+
assert.deepStrictEqual(splitMsg("docs: abc"), {
40+
customPrefix: "",
41+
typePrefix: "docs",
42+
description: "abc",
43+
});
44+
assert.deepStrictEqual(splitMsg("docs: abc def"), {
45+
customPrefix: "",
46+
typePrefix: "docs",
47+
description: "abc def",
48+
});
49+
assert.deepStrictEqual(splitMsg("feat: abc def"), {
50+
customPrefix: "",
51+
typePrefix: "feat",
52+
description: "abc def",
53+
});
54+
});
55+
56+
describe("separates a custom prefix, conventional type, and description", function () {
57+
it("handles a Jira number with hard brackets", function () {
58+
assert.deepStrictEqual(splitMsg("[ABCD-1234] docs: abc def"), {
59+
customPrefix: "[ABCD-1234]",
60+
typePrefix: "docs",
61+
description: "abc def",
62+
});
63+
});
64+
65+
it("handles a Jira number with no hard brackets", function () {
66+
assert.deepStrictEqual(splitMsg("ABCD-1234 docs: abc def"), {
67+
customPrefix: "ABCD-1234",
68+
typePrefix: "docs",
69+
description: "abc def",
70+
});
71+
});
72+
73+
// TODO:
74+
// it("handles a two words before the type", function () {
75+
// assert.deepStrictEqual(splitMsg("ABCD 1234 docs: abc def"), {
76+
// customPrefix: "ABCD 1234",
77+
// typePrefix: "docs",
78+
// description: "abc def",
79+
// });
80+
// });
81+
});
82+
});
83+
});

src/test/prepareCommitMsg.test.ts

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import {
1414
_msgCount,
1515
_msgFromChanges,
1616
_msgNamed,
17-
_newMsg,
18-
_splitMsg,
17+
_newMsg
1918
} from "../prepareCommitMsg";
2019

2120
describe("Join strings cleanly", function () {
@@ -42,87 +41,6 @@ describe("Join strings cleanly", function () {
4241
});
4342
});
4443

45-
describe("Split a message into components", function () {
46-
describe("#_splitMsg", function () {
47-
it("handles a description alone", function () {
48-
assert.deepStrictEqual(_splitMsg("abc def"), {
49-
customPrefix: "",
50-
typePrefix: "",
51-
description: "abc def",
52-
});
53-
assert.deepStrictEqual(_splitMsg("[ABCD-1234]"), {
54-
customPrefix: "",
55-
typePrefix: "",
56-
description: "[ABCD-1234]",
57-
});
58-
});
59-
60-
it("handles a prefix alone", function () {
61-
assert.deepStrictEqual(_splitMsg("docs:"), {
62-
customPrefix: "",
63-
typePrefix: "docs",
64-
description: "",
65-
});
66-
assert.deepStrictEqual(_splitMsg("feat:"), {
67-
customPrefix: "",
68-
typePrefix: "feat",
69-
description: "",
70-
});
71-
72-
assert.deepStrictEqual(_splitMsg("docs: "), {
73-
customPrefix: "",
74-
typePrefix: "docs",
75-
description: "",
76-
});
77-
});
78-
79-
it("separates a prefix and description", function () {
80-
assert.deepStrictEqual(_splitMsg("docs: abc"), {
81-
customPrefix: "",
82-
typePrefix: "docs",
83-
description: "abc",
84-
});
85-
assert.deepStrictEqual(_splitMsg("docs: abc def"), {
86-
customPrefix: "",
87-
typePrefix: "docs",
88-
description: "abc def",
89-
});
90-
assert.deepStrictEqual(_splitMsg("feat: abc def"), {
91-
customPrefix: "",
92-
typePrefix: "feat",
93-
description: "abc def",
94-
});
95-
});
96-
97-
describe("separates a custom prefix, conventional type, and description", function () {
98-
it("handles a Jira number with hard brackets", function () {
99-
assert.deepStrictEqual(_splitMsg("[ABCD-1234] docs: abc def"), {
100-
customPrefix: "[ABCD-1234]",
101-
typePrefix: "docs",
102-
description: "abc def",
103-
});
104-
});
105-
106-
it("handles a Jira number with no hard brackets", function () {
107-
assert.deepStrictEqual(_splitMsg("ABCD-1234 docs: abc def"), {
108-
customPrefix: "ABCD-1234",
109-
typePrefix: "docs",
110-
description: "abc def",
111-
});
112-
});
113-
114-
// TODO:
115-
// it("handles a two words before the type", function () {
116-
// assert.deepStrictEqual(_splitMsg("ABCD 1234 docs: abc def"), {
117-
// customPrefix: "ABCD 1234",
118-
// typePrefix: "docs",
119-
// description: "abc def",
120-
// });
121-
// });
122-
});
123-
});
124-
});
125-
12644
describe("Prepare commit message", function () {
12745
describe("#_msgNamed", function () {
12846
describe("single file changes", function () {

0 commit comments

Comments
 (0)