Skip to content

Commit

Permalink
Merge pull request #103 from amitsingh-007/f-two-digit-templates
Browse files Browse the repository at this point in the history
Breaking: Generate tag with minimum 2 digits
  • Loading branch information
amitsingh-007 authored Jun 15, 2024
2 parents 517026f + 34d0310 commit 443e71a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"rules": {
"no-console": "off",
"import/prefer-default-export": "off",
"no-param-reassign": "off"
"no-param-reassign": "off",
"@typescript-eslint/lines-between-class-members": "off"
}
}
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

29 changes: 12 additions & 17 deletions src/services/releaseService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IAllowedTemplate } from '../types';
import { IPartsData } from '../types/template';
import TagBuilder from './tagBuilder';
import { parseTemplate } from './templateService';

const hasItemChanged = (old: number, cur: number) => old !== -1 && old !== cur;
Expand Down Expand Up @@ -29,22 +30,6 @@ const getNewPartsData = (partsData: IPartsData) => {
};
};

const generateNewTagFromOld = (
partsData: IPartsData,
tagTemplate: string,
tagPrefix: string
) => {
const { curFullYear, curShortYear, curMonth, curDay, newItr } =
getNewPartsData(partsData);
const newReleaseTag = tagTemplate
.replaceAll(IAllowedTemplate.fullYear, curFullYear.toString())
.replaceAll(IAllowedTemplate.shortYear, curShortYear.toString())
.replaceAll(IAllowedTemplate.month, curMonth.toString())
.replaceAll(IAllowedTemplate.day, curDay.toString())
.replaceAll(IAllowedTemplate.itr, newItr.toString());
return `${tagPrefix}${newReleaseTag}`;
};

export const getNewReleaseTag = (
tagPrefix: string,
tagTemplate: string | null | undefined,
Expand All @@ -54,5 +39,15 @@ export const getNewReleaseTag = (
throw new Error('Template not found');
}
const oldPartsData = parseTemplate(tagTemplate, oldReleaseTag, tagPrefix);
return generateNewTagFromOld(oldPartsData, tagTemplate, tagPrefix);
const { curFullYear, curShortYear, curMonth, curDay, newItr } =
getNewPartsData(oldPartsData);

return new TagBuilder(tagTemplate)
.inject(IAllowedTemplate.fullYear, curFullYear)
.inject(IAllowedTemplate.shortYear, curShortYear)
.inject(IAllowedTemplate.month, curMonth)
.inject(IAllowedTemplate.day, curDay)
.inject(IAllowedTemplate.itr, newItr)
.addPrefix(tagPrefix)
.build();
};
34 changes: 34 additions & 0 deletions src/services/tagBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class TagBuilder {
private tagTemplate: string;
private prefix: string | undefined;

constructor(tagTemplate: string) {
this.tagTemplate = `${tagTemplate}`; // Deep copy
}

private static format(value: number) {
return value < 10 ? `0${value}` : `${value}`;
}

public inject(templatePart: string, value: number) {
this.tagTemplate = this.tagTemplate.replaceAll(
templatePart,
TagBuilder.format(value)
);
return this;
}

public addPrefix(prefix: string) {
this.prefix = prefix;
return this;
}

public build() {
if (!this.prefix) {
return this.tagTemplate;
}
return `${this.prefix}${this.tagTemplate}`;
}
}

export default TagBuilder;
4 changes: 2 additions & 2 deletions tests/getReleaseTag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('test valid cases without prefix', () => {
it('should return tag when no old release tag exists', () => {
vi.setSystemTime(new Date('2022-10-13'));
const actualTag = getNewReleaseTag('', 'yy.mm.dd.i', null);
expect(actualTag).toBe('22.10.13.1');
expect(actualTag).toBe('22.10.13.01');
});
it.each(validTemplates)(
'should return changed itr for same date for template: %s',
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('test valid cases with prefix', () => {
it('should return tag when no old release tag exists', () => {
vi.setSystemTime(new Date('2022-10-13'));
const actualTag = getNewReleaseTag('v', 'yy.mm.dd.i', null);
expect(actualTag).toBe('v22.10.13.1');
expect(actualTag).toBe('v22.10.13.01');
});
it.each(validTemplates)(
'should return changed itr for same date for template: %s',
Expand Down
15 changes: 7 additions & 8 deletions tests/utils/testCase.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { IAllowedTemplate } from '../../src/types';

const format = (value: number) => (value < 10 ? `0${value}` : `${value}`);

const getReleaseTag = (template: string, date: Date, itr: number) =>
template
.replaceAll(IAllowedTemplate.fullYear, date.getFullYear().toString())
.replaceAll(
IAllowedTemplate.shortYear,
(date.getFullYear() % 100).toString()
)
.replaceAll(IAllowedTemplate.month, (date.getMonth() + 1).toString())
.replaceAll(IAllowedTemplate.day, date.getDate().toString())
.replaceAll(IAllowedTemplate.itr, itr.toString());
.replaceAll(IAllowedTemplate.fullYear, format(date.getFullYear()))
.replaceAll(IAllowedTemplate.shortYear, format(date.getFullYear() % 100))
.replaceAll(IAllowedTemplate.month, format(date.getMonth() + 1))
.replaceAll(IAllowedTemplate.day, format(date.getDate()))
.replaceAll(IAllowedTemplate.itr, format(itr));

export const getTestCase = ({
oldDate = new Date(),
Expand Down

0 comments on commit 443e71a

Please sign in to comment.