Skip to content

Commit

Permalink
Adopting review comments - 3
Browse files Browse the repository at this point in the history
  • Loading branch information
SherwinVarghese committed Aug 17, 2023
1 parent c523218 commit a0b6809
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const mockInvalidDate1 = '32-09-2023';
const mockInvalidDate2 = '29-02-rddo';
const mockInvalidDate3 = '';
const mockInvalidDate4 = 'abcd';
const mockValidGreaterDate = '31-12-2023';
const mockValidLesserDate = '01-01-2023';

describe('DateValidationService', () => {
let service: DateValidationService;
Expand All @@ -22,14 +24,40 @@ describe('DateValidationService', () => {
expect(service).toBeTruthy();
});

it('should validate correct Dates', () => {
expect(service.isDateStringValid(mockValidDate)).toBeTruthy();
describe('isDateStringValid', () => {
it('should validate correct Dates', () => {
expect(service.isDateStringValid(mockValidDate)).toBeTruthy();
});

it('should invalidate wrong Dates', () => {
expect(service.isDateStringValid(mockInvalidDate1)).toBeFalsy();
expect(service.isDateStringValid(mockInvalidDate2)).toBeFalsy();
expect(service.isDateStringValid(mockInvalidDate3)).toBeFalsy();
expect(service.isDateStringValid(mockInvalidDate4)).toBeFalsy();
});
});

it('should invalidate wrong Dates', () => {
expect(service.isDateStringValid(mockInvalidDate1)).toBeFalsy();
expect(service.isDateStringValid(mockInvalidDate2)).toBeFalsy();
expect(service.isDateStringValid(mockInvalidDate3)).toBeFalsy();
expect(service.isDateStringValid(mockInvalidDate4)).toBeFalsy();
describe('isDateGreaterOrEqual', () => {
it('should return false for invalid dates', () => {
expect(service.isDateGreaterOrEqual(mockValidDate, '')).toBeFalsy();
});

it('should return false when source date is less than target', () => {
expect(
service.isDateGreaterOrEqual(mockValidLesserDate, mockValidDate)
).toBeFalsy();
});

it('should return true for equal dates', () => {
expect(
service.isDateGreaterOrEqual(mockValidDate, mockValidDate)
).toBeTruthy();
});

it('should return true when source date is greater than target', () => {
expect(
service.isDateGreaterOrEqual(mockValidGreaterDate, mockValidDate)
).toBeTruthy();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,6 @@ export class DateValidationService {
);
}

/**
* Compares 2 Date strings in the format 'dd-mm-yyy'
* @param date1 Date string in the format 'dd-mm-yyy'
* @param date2 Date string in the format 'dd-mm-yyy'
* @returns -1 if date2 is greater, 0 if both the dates are equal, 1 if date1 is greater, -2 for invalid inputs
*/
compareDateStrings(date1: string, date2: string): number {
if (date1.length === 0 || date2.length === 0) {
return -2;
}
const d1 = this.getDateFromDateString(date1);
const d2 = this.getDateFromDateString(date2);
if (d1 < d2) {
return -1;
}

return d1 > d2 ? 1 : 0;
}

/**
* Returns a Date object from a date string in the format 'dd-mm-yyy'
* @param value Date string in the format 'dd-mm-yyy'
Expand All @@ -60,6 +41,12 @@ export class DateValidationService {
* @returns true if `source` date is greater than or equal to `target` date
*/
isDateGreaterOrEqual(source: string, target: string): boolean {
return this.compareDateStrings(source, target) >= 0;
if (source.length === 0 || target.length === 0) {
return false;
}
const d1 = this.getDateFromDateString(source);
const d2 = this.getDateFromDateString(target);

return d1 < d2 ? false : true;
}
}

0 comments on commit a0b6809

Please sign in to comment.