Skip to content

Commit

Permalink
feat(action): only show era if supported by calendar (DSP-1725) (#302)
Browse files Browse the repository at this point in the history
* feat(action): improve date pipe so it can handle calendar dates without era

* test(action): clarify spec name
  • Loading branch information
tobiasschweizer committed Jun 10, 2021
1 parent 4c3d1b3 commit 74ceafb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Expand Up @@ -17,7 +17,7 @@ describe('KnoradatePipe', () => {
expect(convertedDate).toEqual('10.10.1993');
});

it('should return the correct format depending on the format provided', () => {
it('should return the correct format for a date with day precision depending on the format provided', () => {
const date = new KnoraDate('GREGORIAN', 'AD', 1776, 7, 4);

let convertedDate = pipe.transform(date, 'dd.MM.YYYY');
Expand Down Expand Up @@ -54,6 +54,22 @@ describe('KnoradatePipe', () => {
expect(dateWithDisplayOptions).toEqual('04.07.1776 AD GREGORIAN');
});

it ('should return a string with the desired display options for a date without era', () => {
const date = new KnoraDate('ISLAMIC', 'noEra', 1441, 7, 4);

let dateWithDisplayOptions = pipe.transform(date, 'dd.MM.YYYY', 'era');

expect(dateWithDisplayOptions).toEqual('04.07.1441');

dateWithDisplayOptions = pipe.transform(date, 'dd.MM.YYYY', 'calendar');

expect(dateWithDisplayOptions).toEqual('04.07.1441 ISLAMIC');

dateWithDisplayOptions = pipe.transform(date, 'dd.MM.YYYY', 'all');

expect(dateWithDisplayOptions).toEqual('04.07.1441 ISLAMIC');
});

it ('should return a string with only the month and the year', () => {
const date = new KnoraDate('GREGORIAN', 'AD', 1776, 7);

Expand Down
Expand Up @@ -6,20 +6,18 @@ import { KnoraDate } from '@dasch-swiss/dsp-js';
})
export class KnoraDatePipe implements PipeTransform {

formattedString: string;

transform(date: KnoraDate, format?: string, displayOptions?: 'era' | 'calendar' | 'all'): string {
if (!(date instanceof KnoraDate)) {
console.error('Non-KnoraDate provided. Expected a valid KnoraDate');
return '';
}

this.formattedString = this.getFormattedString(date, format);
const formattedString = this.getFormattedString(date, format);

if (displayOptions) {
return this.addDisplayOptions(date, this.formattedString, displayOptions);
return this.addDisplayOptions(date, formattedString, displayOptions);
} else {
return this.formattedString;
return formattedString;
}
}

Expand All @@ -36,11 +34,11 @@ export class KnoraDatePipe implements PipeTransform {
addDisplayOptions(date: KnoraDate, value: string, options: string): string {
switch (options) {
case 'era':
return value + ' ' + date.era;
return value + (date.era !== 'noEra' ? ' ' + date.era : '');
case 'calendar':
return value + ' ' + date.calendar;
case 'all':
return value + ' ' + date.era + ' ' + date.calendar;
return value + (date.era !== 'noEra' ? ' ' + date.era : '') + ' ' + date.calendar;
}
}

Expand Down

0 comments on commit 74ceafb

Please sign in to comment.