Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(action): only show era if supported by calendar (DSP-1725) #302

Merged
merged 3 commits into from Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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