-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #284 from wmde/live-time-formatting
- Loading branch information
Showing
20 changed files
with
180 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { Ordinal } from '@src/utils/DynamicContent/formatters/Ordinal'; | ||
import { Currency } from '@src/utils/DynamicContent/formatters/Currency'; | ||
import { Integer } from '@src/utils/DynamicContent/formatters/Integer'; | ||
import { Time } from '@src/utils/DynamicContent/formatters/Time'; | ||
|
||
export interface Formatters { | ||
currency: Currency; | ||
ordinal: Ordinal; | ||
integer: Integer; | ||
time: Time; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface Time { | ||
getFormatted( date: Date ): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Time } from '@src/utils/DynamicContent/formatters/Time'; | ||
|
||
export class TimeDe implements Time { | ||
public getFormatted( date: Date ): string { | ||
return date.toLocaleString( 'de-DE', { hour: 'numeric', minute: 'numeric' } ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Time } from '@src/utils/DynamicContent/formatters/Time'; | ||
|
||
export class TimeEn implements Time { | ||
public getFormatted( date: Date ): string { | ||
return date.toLocaleString( 'en-GB', { hour: 'numeric', hour12: true, minute: 'numeric' } ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { TextGenerator } from '@src/utils/DynamicContent/generators/TextGenerator'; | ||
import { Translator } from '@src/Translator'; | ||
import { Ordinal } from '@src/utils/DynamicContent/formatters/Ordinal'; | ||
import { Time } from '@src/utils/DynamicContent/formatters/Time'; | ||
|
||
export class CurrentDateAndTime implements TextGenerator<Date> { | ||
private readonly _translator: Translator; | ||
private _ordinalFormatter: Ordinal; | ||
private _timeFormatter: Time; | ||
|
||
public constructor( translator: Translator, ordinalFormatter: Ordinal, timeFormatter: Time ) { | ||
this._translator = translator; | ||
this._ordinalFormatter = ordinalFormatter; | ||
this._timeFormatter = timeFormatter; | ||
} | ||
|
||
public getText( date: Date ): string { | ||
return this._translator.translate( 'date-month-time-' + ( date.getMonth() + 1 ), { | ||
day: this._ordinalFormatter.getFormatted( date.getDate() ), | ||
time: this._timeFormatter.getFormatted( date ) | ||
} ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export interface TextGenerator { | ||
getText(): string; | ||
/** | ||
* @param T - Defines the type of optional parameter that can be passed to getText() | ||
*/ | ||
export interface TextGenerator<T = void> { | ||
getText( parameter: T ): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { TimeDe } from '@src/utils/DynamicContent/formatters/TimeDe'; | ||
|
||
describe( 'TimeDe', () => { | ||
it( 'returns the formatted time', () => { | ||
const time = new TimeDe(); | ||
const date = new Date( 2023, 11, 1, 13, 42, 12 ); | ||
|
||
expect( time.getFormatted( date ) ).toBe( '13:42' ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { TimeEn } from '@src/utils/DynamicContent/formatters/TimeEn'; | ||
|
||
describe( 'TimeEn', () => { | ||
it( 'returns the formatted time', () => { | ||
const time = new TimeEn(); | ||
const date = new Date( 2023, 11, 1, 13, 42, 12 ); | ||
|
||
// In some test environments the output of Date.toLocaleString includes the code for a space, | ||
// but in others it doesn't. To fix that we manually replace it in this test | ||
expect( time.getFormatted( date ).replace( '\u202f', ' ' ) ).toStrictEqual( '1:42 pm' ); | ||
} ); | ||
} ); |
33 changes: 33 additions & 0 deletions
33
test/unit/utils/DynamicContent/generators/CurrentTime.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { Translator } from '@src/Translator'; | ||
import { CurrentDateAndTime } from '@src/utils/DynamicContent/generators/CurrentDateAndTime'; | ||
import { Ordinal } from '@src/utils/DynamicContent/formatters/Ordinal'; | ||
import { Time } from '@src/utils/DynamicContent/formatters/Time'; | ||
|
||
const translator = new Translator( { | ||
'date-month-time-1': 'Ick {{day}} - {{time}}', | ||
'date-month-time-2': 'Offle {{day}} - {{time}}', | ||
'date-month-time-10': 'Spune {{day}} - {{time}}', | ||
'date-month-time-11': 'Sektober {{day}} - {{time}}' | ||
} ); | ||
|
||
const staticOrdinal: Ordinal = { | ||
getFormatted: ( figure: number ) => figure + 'sth' | ||
}; | ||
|
||
const staticTime: Time = { | ||
getFormatted: ( date: Date ) => `${date.getHours()} ${date.getMinutes()}` | ||
}; | ||
|
||
describe( 'CurrentTime', () => { | ||
test.each( [ | ||
[ 1, 14, 13, 42, 'Ick 14sth - 13 42' ], | ||
[ 2, 27, 8, 16, 'Offle 27sth - 8 16' ], | ||
[ 10, 2, 1, 59, 'Spune 2sth - 1 59' ], | ||
[ 11, 8, 23, 0, 'Sektober 8sth - 23 0' ] | ||
] )( 'returns the proper month name, date, and time', ( month: number, day: number, hours: number, minutes: number, expected: string ) => { | ||
const currentDate = new CurrentDateAndTime( translator, staticOrdinal, staticTime ); | ||
|
||
expect( currentDate.getText( new Date( 2023, month - 1, day, hours, minutes, 0 ) ) ).toEqual( expected ); | ||
} ); | ||
} ); |