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

Added TimeToPipe #203

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 9 additions & 7 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,8 @@
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"assets": [
],
"styles": [
],
"assets": [],
"styles": [],
"scripts": []
}
},
Expand Down Expand Up @@ -111,6 +109,10 @@
}
}
}
}},
"defaultProject": "angular-test"
}
}
},
"defaultProject": "angular-test",
"cli": {
"analytics": "703d0878-2616-4784-9639-9ce9b07555eb"
}
}
4 changes: 3 additions & 1 deletion src/ng-pipes/pipes/date/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NgModule } from '@angular/core';
import { TimeAgoPipe } from './time-ago';
import { TimeToPipe } from './time-to.pipe';

export const DATE_PIPES = [TimeAgoPipe];
export const DATE_PIPES = [TimeAgoPipe, TimeToPipe];

@NgModule({
declarations: DATE_PIPES,
Expand All @@ -11,3 +12,4 @@ export const DATE_PIPES = [TimeAgoPipe];
export class NgDatePipesModule {}

export { TimeAgoPipe } from './time-ago';
export { TimeToPipe } from './time-to.pipe';
110 changes: 110 additions & 0 deletions src/ng-pipes/pipes/date/time-to.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { TimeToPipe } from './time-to.pipe';

describe('TimeToPipe', () => {
let pipe: TimeToPipe;
const today = new Date();
const past = new Date(today.getTime() - 1000 * 20);

const recentlyString = 'just now';
const pastString = 'in the past';

const fewSecondsAgoDate = new Date(today.getTime() + 5 * 1000);
const aMinuteAgoDate = new Date(today.getTime() + 60 * 1000);

const fewMinutesToString = 'in 5 minutes';
const fewMinutesToDate = new Date(new Date().getTime() + 5 * 60 * 1000);

const anHourToString = 'in an hour';
const anHourToDate = new Date(new Date().getTime() + 60 * 60 * 1000);

const fewHoursToString = 'in 5 hours';
const fewHoursToDate = new Date(new Date().getTime() + 5 * 60 * 60 * 1000);

const tomorrowString = 'tomorrow';
const tomorrowDate = new Date(new Date().setDate(new Date().getDate() + 1));

const fewDaysToString = 'in 3 days';
const fewDaysToDate = new Date(new Date().setDate(new Date().getDate() + 3));

const nextWeekString = 'next week';
const nextWeekDate = new Date(new Date().setDate(new Date().getDate() + 7));

const fewWeeksToString = 'in 2 weeks';
const fewWeeksToDate = new Date(new Date().setDate(new Date().getDate() + 14));

const nextMonthString = 'next month';
const nextMonthDate = new Date(new Date().setDate(new Date().getDate() + 30));

const fewMonthsToString = 'in 5 months';
const fewMonthsToDate = new Date(new Date().setDate(new Date().getDate() + 30 * 5));

const nextYearString = 'next year';
const nextYearDate = new Date(new Date().setDate(new Date().getDate() + 366));

const fewYearsToString = 'in 5 years';
const fewYearsToDate = new Date(new Date().setDate(new Date().getDate() + 366 * 5));

beforeAll(() => {
pipe = new TimeToPipe();
});

it('should return just now', () => {
expect(pipe.transform(new Date())).toEqual(recentlyString);
});

it('should return just now', () => {
expect(pipe.transform(fewSecondsAgoDate)).toEqual(recentlyString);
});

it('should return just now', () => {
expect(pipe.transform(aMinuteAgoDate)).toEqual(recentlyString);
});

it('should return in the past', () => {
expect(pipe.transform(past)).toEqual(pastString);
});

it('should return in 5 minutes', () => {
expect(pipe.transform(fewMinutesToDate)).toEqual(fewMinutesToString);
});

it('should return in an hour', () => {
expect(pipe.transform(anHourToDate)).toEqual(anHourToString);
});

it('should return in 5 hours', () => {
expect(pipe.transform(fewHoursToDate)).toEqual(fewHoursToString);
});

it('should return tomorrow', () => {
expect(pipe.transform(tomorrowDate)).toEqual(tomorrowString);
});

it('should return in 5 days', () => {
expect(pipe.transform(fewDaysToDate)).toEqual(fewDaysToString);
});

it('should return next week', () => {
expect(pipe.transform(nextWeekDate)).toEqual(nextWeekString);
});

it('should return in 2 weeks', () => {
expect(pipe.transform(fewWeeksToDate)).toEqual(fewWeeksToString);
});

it('should return next month', () => {
expect(pipe.transform(nextMonthDate)).toEqual(nextMonthString);
});

it('should return in 5 months', () => {
expect(pipe.transform(fewMonthsToDate)).toEqual(fewMonthsToString);
});

it('should return next year', () => {
expect(pipe.transform(nextYearDate)).toEqual(nextYearString);
});

it('should return in 5 years', () => {
expect(pipe.transform(fewYearsToDate)).toEqual(fewYearsToString);
});
});
40 changes: 40 additions & 0 deletions src/ng-pipes/pipes/date/time-to.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'timeTo',
})
export class TimeToPipe implements PipeTransform {
private static YEAR_MS: number = 1000 * 60 * 60 * 24 * 7 * 4 * 12;
private static MAPPER: any = [
{ single: 'next year', many: 'years', div: 1 },
{ single: 'next month', many: 'months', div: 12 },
{ single: 'next week', many: 'weeks', div: 4 },
{ single: 'tomorrow', many: 'days', div: 7 },
{ single: 'in an hour', many: 'hours', div: 24 },
{ single: 'just now', many: 'minutes', div: 60 },
];

transform(inputDate: any): string {
if (!inputDate || (!inputDate.getTime && !inputDate.toDate)) {
return 'Invalid date';
}

const future = inputDate.toDate ? inputDate.toDate() : inputDate.getTime();
const now = +new Date();

if (future < now) {
return 'in the past';
}

for (let i = 0, l = TimeToPipe.MAPPER.length, ms = future - now, div = TimeToPipe.YEAR_MS; i < l; i++) {
const elm = TimeToPipe.MAPPER[i];

const unit = Math.round(ms / (div /= elm.div));
if (unit >= 1) {
return unit === 1 ? elm.single : `in ${unit} ${elm.many}`;
}
}

return 'just now';
}
}