Skip to content

Commit 9b95aa9

Browse files
committed
fix: Tests
1 parent f3662a4 commit 9b95aa9

4 files changed

Lines changed: 28 additions & 13 deletions

File tree

src/domain/schemas/GitTrailerSchema.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,48 @@ const buildKeyRegex = (keyPattern) => {
2525
}
2626
};
2727

28+
const normalizeKeyPattern = (keyPattern) => {
29+
if (keyPattern instanceof RegExp) {
30+
return keyPattern.source;
31+
}
32+
return keyPattern;
33+
};
34+
2835
/**
2936
* Creates a Git trailer schema bundle with customizable validation rules.
3037
* @param {Object} options - Configuration options
31-
* @param {string} options.keyPattern - Regex pattern string for key validation (will be anchored)
38+
* @param {string|RegExp} options.keyPattern - Regex pattern string or RegExp for key validation (will be anchored)
3239
* @param {number} options.keyMaxLength - Maximum length for trailer keys
3340
* @returns {{ schema: z.ZodObject, keyPattern: string, keyRegex: RegExp }}
3441
*/
3542
export function createGitTrailerSchemaBundle({ keyPattern = DEFAULT_KEY_PATTERN, keyMaxLength = 100 } = {}) {
36-
if (typeof keyPattern !== 'string' || keyPattern.length === 0) {
37-
throw new TypeError('keyPattern must be a non-empty string');
43+
const normalizedKeyPattern = normalizeKeyPattern(keyPattern);
44+
if (typeof normalizedKeyPattern !== 'string' || normalizedKeyPattern.length === 0) {
45+
throw new TypeError('keyPattern must be a non-empty string or RegExp');
3846
}
3947
if (!Number.isInteger(keyMaxLength) || keyMaxLength <= 0) {
4048
throw new TypeError('keyMaxLength must be a positive integer');
4149
}
4250

43-
const cacheKey = `${keyPattern}::${keyMaxLength}`;
51+
const cacheKey = `${normalizedKeyPattern}::${keyMaxLength}`;
4452
if (bundleCache.has(cacheKey)) {
4553
return bundleCache.get(cacheKey);
4654
}
4755

48-
const keyRegex = buildKeyRegex(keyPattern);
56+
const keyRegex = buildKeyRegex(normalizedKeyPattern);
4957
const bundle = {
5058
schema: z.object({
5159
key: z
5260
.string()
5361
.min(1)
5462
.max(keyMaxLength, 'Trailer key must not exceed character limit')
55-
.regex(keyRegex, `Trailer key must match the required pattern ${keyPattern}`),
63+
.regex(keyRegex, `Trailer key must match the required pattern ${normalizedKeyPattern}`),
5664
value: z
5765
.string()
5866
.min(1)
5967
.regex(/^[^\r\n]+$/, 'Trailer values cannot contain line breaks'),
6068
}),
61-
keyPattern,
69+
keyPattern: normalizedKeyPattern,
6270
keyRegex,
6371
};
6472

src/domain/services/TrailerCodecService.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,14 @@ export default class TrailerCodecService {
142142
_buildTrailers(lines) {
143143
return lines.reduce((acc, line) => {
144144
const match = this.parser.lineRegex.exec(line);
145-
if (match) {
146-
acc.push(this.trailerFactory(match[1], match[2], this.schemaBundle.schema));
145+
if (!match) {
146+
return acc;
147147
}
148+
149+
const key = match.groups?.key ?? match[1];
150+
const value = match.groups?.value ?? match[2];
151+
152+
acc.push(this.trailerFactory(key, value, this.schemaBundle.schema));
148153
return acc;
149154
}, []);
150155
}

src/domain/services/TrailerParser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default class TrailerParser {
1919
/**
2020
* @type {RegExp}
2121
*/
22-
this.lineRegex = new RegExp(`^(${keyPattern}):\\s*(.*)$`);
22+
this.lineRegex = new RegExp(`^(?<key>${keyPattern}):\\s*(?<value>[\\s\\S]*)$`);
2323
}
2424

2525
/**

src/domain/value-objects/GitTrailer.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ export default class GitTrailer {
1717
*/
1818
constructor(key, value, schema = GitTrailerSchema) {
1919
const actualSchema = this._validateSchema(schema);
20-
const normalizedKey = String(key ?? '').toLowerCase();
21-
const normalizedValue = String(value ?? '').trim();
20+
const rawKey = String(key ?? '');
21+
const normalizedKey = rawKey.toLowerCase();
22+
const rawValue = String(value ?? '');
23+
const normalizedValue = rawValue.trim();
2224

2325
try {
24-
actualSchema.parse({ key: normalizedKey, value: normalizedValue });
26+
actualSchema.parse({ key: rawKey, value: normalizedValue });
2527
this.key = normalizedKey;
2628
this.value = normalizedValue;
2729
} catch (error) {

0 commit comments

Comments
 (0)