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) referenceTime and formats for am calendar #64

Merged
merged 6 commits into from
Dec 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ Prints `Last updated: a few seconds ago`
Prints `Last updated: a few seconds`

## amCalendar pipe
Takes optional `formats` argument (defaults to now)
and `referenceTime` argument that could be output formats object or callback function.
See [momentjs docs](http://momentjs.com/docs/#/displaying/calendar-time/) for details.

``` typescript
@Component({
Expand All @@ -98,7 +101,37 @@ Prints `Last updated: a few seconds`
})
```

Prints `Last updated: Today at 14:00`
Prints `Last updated: Today at 14:00` (default referenceTime is today by default)

``` typescript
@Component({
selector: 'app',
template: `
Last updated: <time>{{myDate | amCalendar:nextDay }}</time>
`
})
export class AppComponent {
nextDay: Date;

constructor() {
this.nextDay = new Date();
nextDay.setDate(nextDay.getDate() + 1);
}
}
```

Prints `Last updated: Yesterday at 14:00` (referenceTime is tomorrow)

``` typescript
@Component({
selector: 'app',
template: `
Last updated: <time>{{myDate | amCalendar:{sameDay:'[Same Day at] h:mm A'} }}</time>
`
})
```

Prints `Last updated: Same Day at 2:00 PM`

## amDateFormat pipe

Expand Down
19 changes: 19 additions & 0 deletions src/calendar.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,24 @@ describe('CalendarPipe', () => {
it('should transform the start of the current day to "Today at 12:00 AM"', () => {
expect(pipe.transform(moment().startOf('day'))).toBe('Today at 12:00 AM');
});

it('should transform the start of the current day to "Yesterday at 12:00 AM"', () => {
let testDate = moment().startOf('day');
let referenceTime = moment().clone().add(1, 'day');
expect(pipe.transform(testDate, referenceTime)).toBe('Yesterday at 12:00 AM');
});

it('should transform date to "January 13th 2016, 1:23:45 AM"', () => {
let testDate = new Date(2016, 0, 13, 1, 23, 45);
let formats = { sameElse: 'MMMM Do YYYY, h:mm:ss A' };
expect(pipe.transform(testDate, formats)).toBe('January 13th 2016, 1:23:45 AM');
});

it('should support any order of arguments', () => {
let testDate = moment();
let referenceTime = moment().clone().add(1, 'day');
let formats = { lastDay: '[Last day at] h:mm A' };
expect(pipe.transform(testDate, formats, referenceTime)).toBe(pipe.transform(testDate, referenceTime, formats));
});
});
});
15 changes: 14 additions & 1 deletion src/calendar.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,20 @@ export class CalendarPipe implements PipeTransform, OnDestroy {
}

transform(value: Date | moment.Moment, ...args: any[]): any {
return momentConstructor(value).calendar();
let formats: any = null;
let referenceTime: any = null;

for (let i = 0, len = args.length; i < len; i++) {
if (args[i] !== null) {
if (typeof args[i] === 'object' && !moment.isMoment(args[i])) {
formats = args[i];
} else {
referenceTime = momentConstructor(args[i]);
}
}
}

return momentConstructor(value).calendar(referenceTime, formats);
}

ngOnDestroy(): void {
Expand Down