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(omitSuffix): omitSuffix parameter for TimeAgoPipe #47

Merged
merged 1 commit into from
Jul 3, 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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Usage
-----

## amTimeAgo pipe
Takes an optional `omitSuffix` argument that defaults to `false`.

``` typescript
import {TimeAgoPipe} from 'angular2-moment';
Expand All @@ -36,6 +37,20 @@ import {TimeAgoPipe} from 'angular2-moment';

Prints `Last updated: a few seconds ago`

``` typescript
import {TimeAgoPipe} from 'angular2-moment';

@Component({
selector: 'app',
pipes: [TimeAgoPipe],
template: `
Last updated: <time>{{myDate | amTimeAgo:true}}</time>
`
})
```

Prints `Last updated: a few seconds`

## amCalendar pipe

``` typescript
Expand Down
6 changes: 5 additions & 1 deletion src/TimeAgoPipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'es6-shim';
import 'reflect-metadata';
import * as moment from 'moment';
import {TimeAgoPipe} from './TimeAgoPipe';

describe('TimeAgoPipe', () => {
Expand All @@ -14,6 +13,11 @@ describe('TimeAgoPipe', () => {
expect(pipe.transform(new Date())).toBe('a few seconds ago');
});

it('should omit the suffix if second parameter is truthy', () => {
const pipe = new TimeAgoPipe(null);
expect(pipe.transform(new Date(new Date().getTime() + 60000), true)).toBe('a minute');
});

it('should automatically update the text as time passes', () => {
const changeDetectorMock = jasmine.createSpyObj('ChangeDetectorRef', ['markForCheck']);
const pipe = new TimeAgoPipe(changeDetectorMock);
Expand Down
4 changes: 2 additions & 2 deletions src/TimeAgoPipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy {
constructor(private _cdRef: ChangeDetectorRef) {
}

transform(value: Date | moment.Moment, ...args: any[]): string {
transform(value: Date | moment.Moment, omitSuffix?: boolean): string {
const momentInstance = momentConstructor(value);
this._removeTimer();
const timeToUpdate = this._getSecondsUntilUpdate(momentInstance) * 1000;
this._currentTimer = window.setTimeout(() => this._cdRef.markForCheck(), timeToUpdate);
return momentConstructor(value).from(momentConstructor());
return momentConstructor(value).from(momentConstructor(), omitSuffix);
}

ngOnDestroy(): void {
Expand Down