-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 年前'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) + ' 年前'; | ||
} | ||
} | ||
} |