Skip to content

Commit

Permalink
feat(format): Add formatting feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lamabiker committed Mar 13, 2020
1 parent 2e8158a commit de451fe
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
24 changes: 18 additions & 6 deletions projects/ng-date-interval/src/lib/date-interval.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,27 @@ describe('DateIntervalPipe', () => {
expect(result).toEqual(`From 6 to May 11, 2019`);
});

it('should disgard same month/year when format specified', () => {
const dates = [new Date('2019-05-06'), new Date('2019-05-11')];
const result = sut.transform(dates, 'backward', 'dd MMMM y');
expect(result).toEqual(`From 06 May 2019 to 11 May 2019`);
});

it('should handle same year with single value', () => {
const dates = [new Date('2019-05-06')];
const result = sut.transform(dates);
expect(result).toEqual(`Since May 6, 2019`);
});

it('should handle same year with specified format (no days)', () => {
const dates = [new Date('2019-05-06'), new Date('2019-06-06')];
const result = sut.transform(dates, 'forward', 'MMM y');
expect(result).toEqual(`From May to Jun 2019`);
});

it('should handle same year with specified format (leading zero days)', () => {
const dates = [new Date('2019-05-06'), new Date('2019-06-06')];
const result = sut.transform(dates, 'forward', 'dd MMM y');
expect(result).toEqual(`From 06 May to 06 Jun 2019`);
});

it('should handle same year and same month with specified format (no days)', () => {
const dates = [new Date('2019-05-06'), new Date('2019-05-12')];
const result = sut.transform(dates, 'forward', 'MMM y');
expect(result).toEqual(`May 2019`);
});
});
37 changes: 28 additions & 9 deletions projects/ng-date-interval/src/lib/date-interval.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Pipe, PipeTransform, LOCALE_ID, Inject, Input } from '@angular/core';
import { ValidInput, InputDatesArray, DateOutlook } from '../types/types';
import { ValidInput, InputDatesArray, DateOutlook, LocaleDateStringOptions } from '../types/types';
import { formatDate } from '@angular/common';
import sentences from '../langs';

Expand Down Expand Up @@ -33,25 +33,30 @@ export class DateIntervalPipe implements PipeTransform {

const [startDate, endDate] = this.normalizedInput.map(this.formatDates.bind(this));

return !startDate && !endDate ? '' : this.interpolate(this.sentence, { startDate, endDate });
// If the specified format omits the days, but the interval is within
// the same month and year, return the formatted end date only
if (this.isSameYear && this.isSameMonth && !this.showDay) return endDate;

if (!startDate && !endDate) return '';

return this.interpolate(this.sentence, { startDate, endDate });
}

private formatDates(item: Date | string, index: number): string {
if (!item) {
return null;
}

if (this.format !== defautDateFormat) {
return formatDate(item, this.format, this.locale);
}

if (this.isSameYear && index === 0) {
const options = { month: 'short', day: 'numeric' };
let format = this.format === 'mediumDate' ? 'MMM d, y' : this.format;

format = this.removeDateFragment(format, 'y');

if (this.isSameMonth) {
delete options.month;
format = this.removeDateFragment(format, 'm');
}

return new Date(item).toLocaleDateString(this.locale, options);
return formatDate(item, format, this.locale);
}

return formatDate(item, this.format, this.locale);
Expand Down Expand Up @@ -85,6 +90,16 @@ export class DateIntervalPipe implements PipeTransform {
return str;
}

private removeDateFragment(str: string, key: 'd' | 'm' | 'y'): string {
const keyRegExp = new RegExp(`${key}`, 'gi');

return (str = str
.replace(keyRegExp, '')
.trim()
.replace(/^,+/, '')
.replace(/,+$/, ''));
}

// GETTERS

private get isSameYear(): boolean {
Expand All @@ -111,6 +126,10 @@ export class DateIntervalPipe implements PipeTransform {
return month1 === month2;
}

private get showDay(): boolean {
return this.format.toLowerCase().indexOf('d') > -1;
}

private get sentence(): string {
let type = 'default';

Expand Down
5 changes: 5 additions & 0 deletions projects/ng-date-interval/src/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export type InputDatesArray = Array<string | Date>;
export type ValidInput = Array<string | Date> | string | Date;
export type DateOutlook = 'backward' | 'forward';
export interface LocaleDateStringOptions {
year?: 'numeric' | '2-digit';
month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';
day?: 'numeric' | '2-digit';
}

0 comments on commit de451fe

Please sign in to comment.