Skip to content

Commit 659b8eb

Browse files
committed
fix(cz-git): fixed use both maxHeaderLength and maxSubjectLengt
link #1h
1 parent 766bde5 commit 659b8eb

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

‎src/until.ts‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,30 +143,31 @@ const getEmojiStrLength = (options: CommitizenGitOptions, type?: string): number
143143
return item?.emoji ? item.emoji.length + 1 : 0;
144144
};
145145

146+
const countLength = (target: number, typeLength: number, scope: number, emojiLength: number) =>
147+
target - typeLength - 2 - scope - emojiLength;
148+
146149
export const getMaxSubjectLength = (
147150
type: Answers["type"],
148151
scope: Answers["scope"],
149152
options: CommitizenGitOptions
150153
) => {
151154
let optionMaxLength = Infinity;
155+
const typeLength = type?.length ? type.length : 0;
156+
const scopeLength = scope ? scope.length + 2 : 0;
157+
const emojiLength = options.useEmoji ? getEmojiStrLength(options, type) : 0;
152158
const maxHeaderLength = options?.maxHeaderLength ? options?.maxHeaderLength : Infinity;
153159
const maxSubjectLength = options?.maxSubjectLength ? options?.maxSubjectLength : Infinity;
154160
if (options?.maxHeaderLength === 0 || options?.maxSubjectLength === 0) {
155161
return 0;
156162
} else if (maxHeaderLength === Infinity) {
157-
optionMaxLength = maxSubjectLength !== Infinity ? maxSubjectLength : Infinity;
163+
return maxSubjectLength !== Infinity ? maxSubjectLength : Infinity;
158164
} else {
159-
optionMaxLength = maxHeaderLength < maxSubjectLength ? maxHeaderLength : maxSubjectLength;
165+
optionMaxLength =
166+
countLength(maxHeaderLength, typeLength, scopeLength, emojiLength) < maxSubjectLength
167+
? maxHeaderLength
168+
: maxSubjectLength;
160169
}
161-
return (
162-
optionMaxLength -
163-
(type?.length ? type.length : 0) -
164-
// `()`
165-
(scope ? scope.length + 2 : 0) -
166-
// `: `
167-
2 -
168-
(options.useEmoji ? getEmojiStrLength(options, type) : 0)
169-
);
170+
return countLength(optionMaxLength, typeLength, scopeLength, emojiLength);
170171
};
171172

172173
const filterCustomEmptyByOption = (

‎test/until.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe("getMaxSubjectLength", () => {
1717

1818
test("use commitlint rule get subject max length", () => {
1919
const options = { maxSubjectLength: 100 };
20-
expect(getMaxSubjectLength(answer.type, answer.scope, options)).toEqual(86);
20+
expect(getMaxSubjectLength(answer.type, answer.scope, options)).toEqual(100);
2121
});
2222

2323
test("when use both maxsubject rule and maxheader rule", () => {
@@ -33,6 +33,12 @@ describe("getMaxSubjectLength", () => {
3333
maxSubjectLength: 120
3434
})
3535
).toEqual(86);
36+
expect(
37+
getMaxSubjectLength(answer.type, answer.scope, {
38+
maxHeaderLength: 100,
39+
maxSubjectLength: 110
40+
})
41+
).toEqual(86);
3642
expect(
3743
getMaxSubjectLength(answer.type, answer.scope, {
3844
maxHeaderLength: Infinity,
@@ -44,7 +50,7 @@ describe("getMaxSubjectLength", () => {
4450
maxHeaderLength: Infinity,
4551
maxSubjectLength: 100
4652
})
47-
).toEqual(86);
53+
).toEqual(100);
4854
expect(
4955
getMaxSubjectLength(answer.type, answer.scope, {
5056
maxHeaderLength: 100,

0 commit comments

Comments
 (0)