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

Make prefix/suffix be included conditionally instead of always. #24

Open
wants to merge 1 commit 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
24 changes: 13 additions & 11 deletions lib/timeago.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// Licensed under the MIT license.
// https://github.com/pragmaticly/smart-time-ago/blob/master/LICENSE
//
// Generated by CoffeeScript 1.5.0

// Generated by CoffeeScript 1.6.3
(function() {
var TimeAgo;

Expand Down Expand Up @@ -82,15 +81,19 @@
};

TimeAgo.prototype.timeAgoInWords = function(timeString) {
var absolutTime;
var absolutTime, dim, prefix, suffix, timeInWords;
absolutTime = this.parse(timeString);
return "" + this.options.lang.prefixes.ago + (this.distanceOfTimeInWords(absolutTime)) + this.options.lang.suffix;
dim = this.getTimeDistanceInMinutes(absolutTime);
timeInWords = this.distanceOfTimeInWords(dim);
prefix = dim >= 0 ? this.options.lang.prefixes.ago : '';
suffix = dim < 0 ? this.options.lang.suffix : '';
return "" + prefix + timeInWords + suffix;
};

TimeAgo.prototype.parse = function(iso8601) {
var timeStr;
timeStr = $.trim(iso8601);
timeStr = timeStr.replace(/\.\d\d\d+/, "");
timeStr = timeStr.replace(/\.\d+/, "");
timeStr = timeStr.replace(/-/, "/").replace(/-/, "/");
timeStr = timeStr.replace(/T/, " ").replace(/Z/, " UTC");
timeStr = timeStr.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2");
Expand All @@ -99,13 +102,12 @@

TimeAgo.prototype.getTimeDistanceInMinutes = function(absolutTime) {
var timeDistance;
timeDistance = new Date().getTime() - absolutTime.getTime();
return Math.round((Math.abs(timeDistance) / 1000) / 60);
timeDistance = absolutTime.getTime() - new Date().getTime();
return Math.round((timeDistance / 1000) / 60);
};

TimeAgo.prototype.distanceOfTimeInWords = function(absolutTime) {
var dim;
dim = this.getTimeDistanceInMinutes(absolutTime);
TimeAgo.prototype.distanceOfTimeInWords = function(dim) {
dim = Math.abs(dim);
if (dim === 0) {
return "" + this.options.lang.prefixes.lt + " " + this.options.lang.units.minute;
} else if (dim === 1) {
Expand Down Expand Up @@ -191,4 +193,4 @@
}
};

}).call(this);
}).call(this);
14 changes: 9 additions & 5 deletions src/timeago.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ class TimeAgo

timeAgoInWords: (timeString) ->
absolutTime = @parse(timeString)
"#{@options.lang.prefixes.ago}#{@distanceOfTimeInWords(absolutTime)}#{@options.lang.suffix}"
dim = @getTimeDistanceInMinutes(absolutTime)
timeInWords = @distanceOfTimeInWords(dim)
prefix = if (dim >= 0) then @options.lang.prefixes.ago else ''
suffix = if (dim < 0) then @options.lang.suffix else ''
"#{prefix}#{timeInWords}#{suffix}"

parse: (iso8601) ->
timeStr = $.trim(iso8601)
Expand All @@ -76,12 +80,12 @@ class TimeAgo
new Date(timeStr);

getTimeDistanceInMinutes: (absolutTime) ->
timeDistance = new Date().getTime() - absolutTime.getTime()
Math.round((Math.abs(timeDistance) / 1000) / 60)
timeDistance = absolutTime.getTime() - new Date().getTime()
Math.round((timeDistance / 1000) / 60)

distanceOfTimeInWords: (absolutTime) ->
distanceOfTimeInWords: (dim) ->
#TODO support i18n.
dim = @getTimeDistanceInMinutes(absolutTime) #distance in minutes
dim = Math.abs(dim) #distance in minutes

if dim == 0
"#{ @options.lang.prefixes.lt } #{ @options.lang.units.minute }"
Expand Down
87 changes: 40 additions & 47 deletions test/specs/timeago_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ describe("TimeAgo", function(){
spyOn(timeAgo, 'getTimeDistanceInMinutes').andReturn(0);
});
it("should return 'less than a minute'", function(){
expect(timeAgo.distanceOfTimeInWords(new Date())).toEqual("less than a minute");
var dim = timeAgo.getTimeDistanceInMinutes(new Date());
expect(timeAgo.distanceOfTimeInWords(dim)).toEqual("less than a minute");
});
});

Expand All @@ -214,109 +215,101 @@ describe("TimeAgo", function(){
spyOn(timeAgo, 'getTimeDistanceInMinutes').andReturn(1);
});
it("should return '1 minute'", function(){
expect(timeAgo.distanceOfTimeInWords(new Date())).toEqual("1 minute");
var dim = timeAgo.getTimeDistanceInMinutes(new Date());
expect(timeAgo.distanceOfTimeInWords(dim)).toEqual("1 minute");
});
});

describe("context: dim >= 2 and dim <= 44", function(){
beforeEach(function(){
spyOn(timeAgo, 'getTimeDistanceInMinutes').andReturn(2);
});
it("should return '2 minutes'", function(){
expect(timeAgo.distanceOfTimeInWords(new Date())).toEqual("2 minutes");
expect(timeAgo.distanceOfTimeInWords(2)).toEqual("2 minutes");
});
});

describe("context: dim >= 45 and dim <= 89", function(){
beforeEach(function(){
spyOn(timeAgo, 'getTimeDistanceInMinutes').andReturn(89);
});
it("should return 'about 1 hour'", function(){
expect(timeAgo.distanceOfTimeInWords(new Date())).toEqual("about 1 hour");
expect(timeAgo.distanceOfTimeInWords(89)).toEqual("about 1 hour");
});
});

describe("context: dim >= 90 and dim <= 1439", function(){
beforeEach(function(){
spyOn(timeAgo, 'getTimeDistanceInMinutes').andReturn(120);
});
it("should return 'about 2 hours'", function(){
expect(timeAgo.distanceOfTimeInWords(new Date())).toEqual("about 2 hours");
expect(timeAgo.distanceOfTimeInWords(120)).toEqual("about 2 hours");
});
});

describe("context: dim >= 1440 and dim <= 2519", function(){
beforeEach(function(){
spyOn(timeAgo, 'getTimeDistanceInMinutes').andReturn(2519);
});
it("should return '1 day'", function(){
expect(timeAgo.distanceOfTimeInWords(new Date())).toEqual("1 day");
expect(timeAgo.distanceOfTimeInWords(2519)).toEqual("1 day");
});
});

describe("context: dim >= 2520 and dim <= 43199", function(){
beforeEach(function(){
spyOn(timeAgo, 'getTimeDistanceInMinutes').andReturn(2520);
});
it("should return '2 days'", function(){
expect(timeAgo.distanceOfTimeInWords(new Date())).toEqual("2 days");
expect(timeAgo.distanceOfTimeInWords(2520)).toEqual("2 days");
});
});

describe("context: dim >= 43200 and dim <= 86399", function(){
beforeEach(function(){
spyOn(timeAgo, 'getTimeDistanceInMinutes').andReturn(86399);
});
it("should return 'about 1 month'", function(){
expect(timeAgo.distanceOfTimeInWords(new Date())).toEqual("about 1 month");
expect(timeAgo.distanceOfTimeInWords(86399)).toEqual("about 1 month");
});
});

describe("context: dim >= 86400 and dim <= 525599", function(){
beforeEach(function(){
spyOn(timeAgo, 'getTimeDistanceInMinutes').andReturn(525599);
});
it("should return '12 months'", function(){
expect(timeAgo.distanceOfTimeInWords(new Date())).toEqual("12 months");
expect(timeAgo.distanceOfTimeInWords(525599)).toEqual("12 months");
});
});

describe("context: dim >= 525600 and dim <= 655199", function(){
beforeEach(function(){
spyOn(timeAgo, 'getTimeDistanceInMinutes').andReturn(525600);
});
it("should return 'about 1 year'", function(){
expect(timeAgo.distanceOfTimeInWords(new Date())).toEqual("about 1 year");
expect(timeAgo.distanceOfTimeInWords(525600)).toEqual("about 1 year");
});
});

describe("context: dim >= 655200 and dim <= 914399", function(){
beforeEach(function(){
spyOn(timeAgo, 'getTimeDistanceInMinutes').andReturn(655200);
});
it("should return 'over 1 year'", function(){
expect(timeAgo.distanceOfTimeInWords(new Date())).toEqual("over 1 year");
expect(timeAgo.distanceOfTimeInWords(655200)).toEqual("over 1 year");
});
});

describe("context: dim >= 914400 and dim <= 1051199", function(){
beforeEach(function(){
spyOn(timeAgo, 'getTimeDistanceInMinutes').andReturn(914400);
});
it("should return 'almost 2 years'", function(){
expect(timeAgo.distanceOfTimeInWords(new Date())).toEqual("almost 2 years");
expect(timeAgo.distanceOfTimeInWords(914400)).toEqual("almost 2 years");
});
});

describe("context: >= 1051200", function(){
beforeEach(function(){
spyOn(timeAgo, 'getTimeDistanceInMinutes').andReturn(1051200);
});
it("should return 'almost 2 years'", function(){
expect(timeAgo.distanceOfTimeInWords(new Date())).toEqual("about 2 years");
expect(timeAgo.distanceOfTimeInWords(1051200)).toEqual("about 2 years");
});
});

});

describe("string formatting of dates", function(){

beforeEach(function(){
timeAgo.options.lang.suffix = " ago";
timeAgo.options.lang.prefixes.ago = "Will start in ";
});

it("should prefix future dates and not suffix", function(){

var date = "2099-07-18T07:51:50Z";
var result = timeAgo.timeAgoInWords(date);

expect(result).toMatch(/^Will start in/);
expect(result).not.toMatch(/ago$/);
});

it("should not prefix past dates and add suffix", function(){

var date = "2012-07-18T07:51:50Z";
var result = timeAgo.timeAgoInWords(date);

expect(result).toMatch(/ago$/)
expect(result).not.toMatch(/^Will start in/)
});
});
});