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

Adding time-fmt filter : (3000 => 50 minutes ago), updating Readme.md and angular-filter.js #30

Open
wants to merge 2 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
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,22 @@ Converts kilobytes into formatted display<br/>
<!--result
1 MB
1.00126 GB

```
###timefmt
Converts time difference into format<br/>
**Usage:** ```seconds | timeFmt```,
```html
<p>{{ 4 | timeFmt }}</p>
<p>{{ 59 | timeFmt }}</p>
<p>{{ 7234 | timeFmt }}</p>
<p>{{ 691200 | timeFmt }}</p>
<p>{{ 8640000 | timeFmt }}</p>
<!--result
just now
59 seconds ago
2 hours ago
1 week ago
3 months ago
```
#Boolean
>Used for boolean expression in chaining filters
Expand Down
72 changes: 72 additions & 0 deletions dist/angular-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,78 @@ angular.module('a8m.math.sum', [])

});


/**
* @ngdoc filter
* @name timeFmt
* @kind function
*
* @description
* converting time in seconds to appropriate format (34 minutes ago)
*/

angular.module('a8m.math.timeFmt', ['a8m.math'])

.filter('timeFmt', ['$math', function ($math) {
return function (seconds) {

if(isNumber(seconds) && isFinite(seconds) && seconds % 1 === 0 && seconds >= 0) { // seconds is for sure an integer

if(seconds < 5){
return 'just now';
}else if(seconds < 60) { // within 1 minute so seconds
return seconds + ' seconds ago';
} else if(seconds < 3600) { // within 1 hour so minutes
var minute = $math.round(seconds / 60);
if(minute > 1){
return minute + ' minutes ago';
}else{
return '1 minute ago';
}
} else if(seconds < 86400){ // within 1 day so hours
var hour = $math.round(seconds / 3600);
if(hour > 1){
return hour + ' hours ago';
}else{
return '1 hour ago';
}
} else if(seconds < 604800){ // within one week so days
var day = $math.round(seconds / 86400);
if(day > 1){
return day + ' days ago';
}else{
return '1 day ago';
}
} else if(seconds < 2592000){ // within one month so weeks
var week = $math.round(seconds / 604800);
if(week > 1){
return week + ' weeks ago';
}else{
return '1 week ago';
}
} else if(seconds < 31536000){ // within one year so months
var month = $math.round(seconds / 2592000);
if(month > 1){
return month + ' months ago';
}else{
return '1 month ago';
}
} else{
// years
var year = $math.round(seconds / 31536000);
if(year > 1){
return year + ' years ago';
}else{
return '1 year ago';
}
}
} else {
return "NaN";
}
}

}]);

/**
* @ngdoc filter
* @name endsWith
Expand Down
70 changes: 70 additions & 0 deletions src/_filter/math/time-fmt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @ngdoc filter
* @name timeFmt
* @kind function
*
* @description
* Convert difference in time to appropriate format
* 286 => 4 minutes ago
*/

angular.module('a8m.math.timeFmt', ['a8m.math'])

.filter('timeFmt', ['$math', function ($math) {
return function (seconds) {

if(isNumber(seconds) && isFinite(seconds) && seconds % 1 === 0 && seconds >= 0) { // seconds is for sure an integer
if(seconds < 5){
return 'just now';
}else if(seconds < 60) { // within 1 minute so seconds
return seconds + ' seconds ago';
} else if(seconds < 3600) { // within 1 hour so minutes
var minute = $math.round(seconds / 60);
if(minute > 1){
return minute + ' minutes ago';
}else{
return '1 minute ago';
}
} else if(seconds < 86400){ // within 1 day so hours
var hour = $math.round(seconds / 3600);
if(hour > 1){
return hour + ' hours ago';
}else{
return '1 hour ago';
}
} else if(seconds < 604800){ // within one week so days
var day = $math.round(seconds / 86400);
if(day > 1){
return day + ' days ago';
}else{
return '1 day ago';
}
} else if(seconds < 2592000){ // within one month so weeks
var week = $math.round(seconds / 604800);
if(week > 1){
return week + ' weeks ago';
}else{
return '1 week ago';
}
} else if(seconds < 31536000){ // within one year so months
var month = $math.round(seconds / 2592000);
if(month > 1){
return month + ' months ago';
}else{
return '1 month ago';
}
} else{
// years
var year = $math.round(seconds / 31536000);
if(year > 1){
return year + ' years ago';
}else{
return '1 year ago';
}
}
} else {
return "NaN";
}
}

}]);
31 changes: 31 additions & 0 deletions test/spec/filter/math/time-fmt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

describe('timeFmtFilter', function () {

var filter;

beforeEach(module('a8m.math.timeFmt'));

beforeEach(inject(function ($filter) {
filter = $filter('timeFmt');
}));

it('should return the correct display from number of seconds', function() {
expect(filter(0)).toEqual("just now");
expect(filter(5)).toEqual("5 seconds ago");
expect(filter(1024)).toEqual("17 minutes ago");
expect(filter(3600)).toEqual("1 hour ago");
expect(filter(42588)).toEqual("12 hours ago");
expect(filter(169200)).toEqual("2 days ago");
expect(filter(691200)).toEqual("1 week ago");
expect(filter(8640000)).toEqual("3 months ago");
expect(filter(133081920)).toEqual("4 years ago");
});

it('should return NaN if seconds is not a number', function(){
expect(filter("0")).toEqual("NaN");
expect(filter([0])).toEqual("NaN");
expect(filter({number:0})).toEqual("NaN");
});

});