Skip to content

Commit

Permalink
feat(pipes): 新增时间管道
Browse files Browse the repository at this point in the history
  • Loading branch information
stbui committed Sep 1, 2018
1 parent ce4a6ae commit 2860946
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/app/component/common/pipes/time-ago.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { TimeAgoPipe } from './time-ago.pipe';

describe('TimeAgoPipe', () => {
let pipe: TimeAgoPipe;
let time: number = Date.now();

beforeEach(() => {
pipe = new TimeAgoPipe();
});

it('should return "Invalid Date" with an invalid date', () => {
expect(pipe.transform(undefined, undefined)).toEqual('Invalid Date');
expect(pipe.transform(undefined, time)).toEqual('Invalid Date');
expect(pipe.transform('', undefined)).toEqual('Invalid Date');
expect(pipe.transform('', '')).toEqual('Invalid Date');
expect(pipe.transform({}, {})).toEqual('Invalid Date');
expect(pipe.transform('date', 'date')).toEqual('Invalid Date');
});

it('should return a time ago string', () => {
expect(pipe.transform(time, time)).toEqual('1 秒前');
expect(pipe.transform(new Date(time), time)).toEqual('1 秒前');
expect(pipe.transform(new Date(time).toString(), time)).toEqual('1 秒前');
expect(pipe.transform(time - 1000 * 37, time)).toEqual('37 秒前');
expect(pipe.transform(time - 1000 * 60, time)).toEqual('1 分钟前');
expect(pipe.transform(time - 1000 * 60 * 6, time)).toEqual('6 分钟前');
expect(pipe.transform(time - 1000 * 60 * 60, time)).toEqual('1 小时前');
expect(pipe.transform(time - 1000 * 60 * 60 * 13, time)).toEqual(
'13 小时前'
);
expect(pipe.transform(time - 1000 * 60 * 60 * 24, time)).toEqual('1 天前');
expect(pipe.transform(time - 1000 * 60 * 60 * 24 * 17, time)).toEqual(
'17 天前'
);
expect(pipe.transform(time - 1000 * 60 * 60 * 24 * 30, time)).toEqual(
'1 月前'
);
expect(pipe.transform(time - 1000 * 60 * 60 * 24 * 30 * 3, time)).toEqual(
'3 月前'
);
expect(pipe.transform(time - 1000 * 60 * 60 * 24 * 30 * 12, time)).toEqual(
'1 年前'
);
expect(
pipe.transform(time - 1000 * 60 * 60 * 24 * 30 * 12 * 4, time)
).toEqual('4 年前');
});
});
64 changes: 64 additions & 0 deletions src/app/component/common/pipes/time-ago.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'timeAgo'
})
export class TimeAgoPipe implements PipeTransform {
transform(value: any, args?: any): any {
const time = new Date(value);
let ref: Date = new Date(args);

if (!time.getTime()) {
return 'Invalid Date';
}

let startTime: number = isNaN(ref.getTime()) ? Date.now() : ref.getTime();
let diff: number = Math.floor((startTime - time.getTime()) / 1000);

if (diff < 2) {
return '1 秒前';
}
if (diff < 60) {
return Math.floor(diff) + ' 秒前';
}

diff = diff / 60;
if (diff < 2) {
return '1 分钟前';
}
if (diff < 60) {
return Math.floor(diff) + ' 分钟前';
}

diff = diff / 60;
if (diff < 2) {
return '1 小时前';
}
if (diff < 24) {
return Math.floor(diff) + ' 小时前';
}

diff = diff / 24;
if (diff < 2) {
return '1 天前';
}
if (diff < 30) {
return Math.floor(diff) + ' 天前';
}

diff = diff / 30;
if (diff < 2) {
return '1 月前';
}
if (diff < 12) {
return Math.floor(diff) + ' 月前';
}

diff = diff / 12;
if (diff < 2) {
return '1 年前';
} else {
return Math.floor(diff) + ' 年前';
}
}
}

0 comments on commit 2860946

Please sign in to comment.