Skip to content

Commit

Permalink
feat(cdk): i18n for date fillers
Browse files Browse the repository at this point in the history
  • Loading branch information
MarsiBarsi committed Nov 24, 2020
1 parent 499e9f3 commit 1e1b845
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
18 changes: 18 additions & 0 deletions projects/cdk/date-time/date-fillers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {inject, InjectionToken} from '@angular/core';
import {RANGE_SEPARATOR_CHAR} from './date-time';

// TODO: think about yyyy.mm.dd format
export const TUI_DATE_FILLER = new InjectionToken<string>('date filler for Taiga UI', {
factory: () => `dd.mm.yyyy`,
});

export const TUI_DATE_RANGE_FILLER = new InjectionToken<string>(
'date range filler for Taiga UI',
{
factory: () => {
const dateFiller = inject(TUI_DATE_FILLER);

return `${dateFiller}${RANGE_SEPARATOR_CHAR}${dateFiller}`;
},
},
);
5 changes: 0 additions & 5 deletions projects/cdk/date-time/date-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@ export const MIN_YEAR = 0;

export const MAX_YEAR = 9999;

// TODO: i18n
export const DATE_FILLER = `дд.мм.гггг`;

export const RANGE_SEPARATOR_CHAR = `${CHAR_NO_BREAK_SPACE}${CHAR_EN_DASH}${CHAR_NO_BREAK_SPACE}`;

export const DATE_RANGE_FILLER = `${DATE_FILLER}${RANGE_SEPARATOR_CHAR}${DATE_FILLER}`;

export const MILLISECONDS_IN_SECOND = 1000;

export const SECONDS_IN_MINUTE = 60;
Expand Down
16 changes: 10 additions & 6 deletions projects/cdk/date-time/day-range.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {tuiAssert} from '@taiga-ui/cdk/classes';
import {DATE_FILLER, DATE_RANGE_FILLER, RANGE_SEPARATOR_CHAR} from './date-time';
import {RANGE_SEPARATOR_CHAR} from './date-time';
import {TuiDay} from './day';
import {TuiMonthRange} from './month-range';

Expand Down Expand Up @@ -61,20 +61,24 @@ export class TuiDayRange extends TuiMonthRange {
/**
* Parse and correct a day range in string format
*
* @param rangeString a string of dates in a format DD.MM.Yyyy - DD.MM.Yyyy
* @param rangeString a string of dates in a format dd.mm.yyyy - dd.mm.yyyy
* @return normalized day range object
*/
static normalizeParse(rangeString: string): TuiDayRange {
const leftDay = TuiDay.normalizeParse(rangeString.slice(0, DATE_FILLER.length));
static normalizeParse(
rangeString: string,
dateFiller: string,
dateRangeFiller: string,
): TuiDayRange {
const leftDay = TuiDay.normalizeParse(rangeString.slice(0, dateFiller.length));

if (rangeString.length < DATE_RANGE_FILLER.length) {
if (rangeString.length < dateRangeFiller.length) {
return new TuiDayRange(leftDay, leftDay);
}

return TuiDayRange.sort(
leftDay,
TuiDay.normalizeParse(
rangeString.slice(DATE_FILLER.length + RANGE_SEPARATOR_CHAR.length),
rangeString.slice(dateFiller.length + RANGE_SEPARATOR_CHAR.length),
),
);
}
Expand Down
21 changes: 21 additions & 0 deletions projects/cdk/date-time/test/date-fillers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {TestBed} from '@angular/core/testing';
import {TUI_DATE_FILLER, TUI_DATE_RANGE_FILLER} from '../date-fillers';
import {RANGE_SEPARATOR_CHAR} from '../date-time';

describe('Date Fillers', () => {
it('TUI_DATE_FILLER is a global token with default value', () => {
TestBed.configureTestingModule({});

const result = TestBed.get(TUI_DATE_FILLER);

expect(result).toBe('dd.mm.yyyy');
});

it('TUI_DATE_RANGE_FILLER is a global token with default value', () => {
TestBed.configureTestingModule({});

const result = TestBed.get(TUI_DATE_RANGE_FILLER);

expect(result).toBe('dd.mm.yyyy' + RANGE_SEPARATOR_CHAR + 'dd.mm.yyyy');
});
});
13 changes: 13 additions & 0 deletions projects/cdk/date-time/test/day-range.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {RANGE_SEPARATOR_CHAR} from '../date-time';
import {TuiDay} from '../day';
import {TuiDayRange} from '../day-range';

const DEFAULT_DATE_FILLER = `dd.mm.yyyy`;
const DEFAULT_DATE_RANGE_FILLER = `${DEFAULT_DATE_FILLER}${RANGE_SEPARATOR_CHAR}${DEFAULT_DATE_FILLER}`;

describe('TuiDayRange', () => {
describe('static method', () => {
describe('sort returns', () => {
Expand Down Expand Up @@ -41,6 +44,8 @@ describe('TuiDayRange', () => {
it('corresponding range in normal case', () => {
const result: TuiDayRange = TuiDayRange.normalizeParse(
`12.03.4567${RANGE_SEPARATOR_CHAR}07.06.9321`,
DEFAULT_DATE_FILLER,
DEFAULT_DATE_RANGE_FILLER,
);

expect(result.from.day).toBe(12);
Expand All @@ -54,6 +59,8 @@ describe('TuiDayRange', () => {
it('corresponding range ignoring wrong delimiters', () => {
const result: TuiDayRange = TuiDayRange.normalizeParse(
`12#03#4567#!#07#06#9321`,
DEFAULT_DATE_FILLER,
DEFAULT_DATE_RANGE_FILLER,
);

expect(result.from.day).toBe(12);
Expand All @@ -67,6 +74,8 @@ describe('TuiDayRange', () => {
it('properly normalized range', () => {
const result: TuiDayRange = TuiDayRange.normalizeParse(
`00.00.0000${RANGE_SEPARATOR_CHAR}99.99.9999`,
DEFAULT_DATE_FILLER,
DEFAULT_DATE_RANGE_FILLER,
);

expect(result.from.day).toBe(1);
Expand All @@ -80,6 +89,8 @@ describe('TuiDayRange', () => {
it('sorted range if the order is wrong', () => {
const result: TuiDayRange = TuiDayRange.normalizeParse(
`07.06.9321${RANGE_SEPARATOR_CHAR}12.03.4567`,
DEFAULT_DATE_FILLER,
DEFAULT_DATE_RANGE_FILLER,
);

expect(result.from.day).toBe(12);
Expand All @@ -93,6 +104,8 @@ describe('TuiDayRange', () => {
it('one day range if rangeString is not long enough', () => {
const result: TuiDayRange = TuiDayRange.normalizeParse(
`07.06.9321${RANGE_SEPARATOR_CHAR}12.03.4`,
DEFAULT_DATE_FILLER,
DEFAULT_DATE_RANGE_FILLER,
);

expect(result.from.day).toBe(7);
Expand Down

0 comments on commit 1e1b845

Please sign in to comment.