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 allowPast settings & tests #169

Merged
merged 3 commits into from Mar 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions index.html
Expand Up @@ -116,6 +116,15 @@ <h3>How?</h3>
<pre>
jQuery.timeago.settings.allowFuture = true;</pre>

<p class="how">
To disable timestamps in the past, use the <tt>allowPast</tt> setting.
This setting is set to true by default. When set to false, if the time is in the past then instead of displaying a message like "5 minutes ago" a static message will be displayed.
The staic message displayed can be configured with the <tt>strings.inPast</tt> setting:
</p>
<pre>
jQuery.timeago.settings.strings.inPast = "time has elapsed";
jQuery.timeago.settings.allowPast = false;</pre>

<h3>Excusez-moi?</h3>
<p>
Yes, timeago has locale/i18n/language support. Here are some <a href="https://github.com/rmm5t/jquery-timeago/tree/master/locales">configuration examples</a>. Please submit a <a href="https://github.com/rmm5t/jquery-timeago">GitHub pull request</a> for corrections or additional languages.
Expand Down
12 changes: 12 additions & 0 deletions jquery.timeago.js
Expand Up @@ -39,6 +39,7 @@
$.extend($.timeago, {
settings: {
refreshMillis: 60000,
allowPast: true,
allowFuture: false,
localeTitle: false,
cutoff: 0,
Expand All @@ -47,6 +48,7 @@
prefixFromNow: null,
suffixAgo: "ago",
suffixFromNow: "from now",
inPast: 'any moment now',
seconds: "less than a minute",
minute: "about a minute",
minutes: "%d minutes",
Expand All @@ -62,7 +64,12 @@
numbers: []
}
},

inWords: function(distanceMillis) {
if(!this.settings.allowPast && ! this.settings.allowFuture) {
throw 'timeago allowPast and allowFuture settings can not both be set to false.';
}

var $l = this.settings.strings;
var prefix = $l.prefixAgo;
var suffix = $l.suffixAgo;
Expand All @@ -73,6 +80,10 @@
}
}

if(!this.settings.allowPast && distanceMillis >= 0) {
return this.settings.strings.inPast;
}

var seconds = Math.abs(distanceMillis) / 1000;
var minutes = seconds / 60;
var hours = minutes / 60;
Expand Down Expand Up @@ -101,6 +112,7 @@
if ($l.wordSeparator === undefined) { separator = " "; }
return $.trim([prefix, words, suffix].join(separator));
},

parse: function(iso8601) {
var s = $.trim(iso8601);
s = s.replace(/\.\d+/,""); // remove milliseconds
Expand Down
51 changes: 51 additions & 0 deletions test/index.html
Expand Up @@ -190,6 +190,19 @@ <h2>Settings</h2>
<li><abbr id="testMillisSettings9" class="tomillis" title="120"></abbr> [120 sec]</li>
</ul>

<h2>Do Not Allow Future</h2>

<ul>
<li><abbr id="testAllowFutureFalse1" class="doNotAllowFuture" title="2012-01-01T09:24:17Z">(you shouldn't see this)</abbr></li>
</ul>

<h2>Do Not Allow Past</h2>

<ul>
<li><abbr id="testAllowPastFalse1" class="doNotAllowPast" title="2008-01-01T09:24:17Z">(you shouldn't see this)</abbr></li>
<li><abbr id="testAllowPastAndFutureFalse" title="2008-01-01T09:24:17Z"></abbr></li>
</ul>

<h2>Disposal</h2>
<p><abbr class="disposal disposed"></abbr></p>
<p><abbr class="disposal notDisposed"></abbr></p>
Expand Down Expand Up @@ -243,6 +256,14 @@ <h2>Disposal</h2>
$("abbr.tonumbers").each(toWords);
unloadNumbers();

loadDoNotAllowFuture();
$("abbr.doNotAllowFuture").timeago();
unloadDoNotAllowFuture();

loadDoNotAllowPast();
$("abbr.doNotAllowPast").timeago();
unloadDoNotAllowPast();

setupDisposal();

loadYoungOldYears();
Expand Down Expand Up @@ -595,6 +616,36 @@ <h2>Disposal</h2>
ok($("#testNullSpaces1").html().match(/^2minutesago$/), "Settings correctly applied");
});

module("Allow Future and Past");

// if allowFuture is false, then a minute into the future is actually reported as a minute in the past.
// we did not want to alter the current behavior and break backwards compatability
test("allow future false with a future date moves time backwards", function () {
equal($("#testAllowFutureFalse1").html(), "2 years ago");
});

test("allow future false with a future date moves time backwards", function () {
equal($("#testAllowPastFalse1").html(), "in the past");
});

test("throws if allowPast and allowFuture are both false", function () {
// setup
var origAllowFuture = $.timeago.settings.allowFuture;
var origAllowPast = $.timeago.settings.allowPast;
$.timeago.settings.allowFuture = false;
$.timeago.settings.allowPast = false;

try {
throws(function () {
$("#testAllowPastAndFutureFalse").timeago();
});
} finally {
// teardown
$.timeago.settings.allowFuture = origAllowFuture;
$.timeago.settings.allowPast = origAllowPast;
}
});

module("Disposal");

asyncTest("disposal", function() {
Expand Down
46 changes: 46 additions & 0 deletions test/test_helpers.js
Expand Up @@ -131,3 +131,49 @@ function loadYoungOldYears() {
years: function(value) { return (value < 21) ? "%d young years" : "%d old years"; }
});
}

function loadDoNotAllowFuture() {
var mockDateToUse = "2010-01-01";
$.timeago.settings.allowFuture = false;
enableMockedDate(mockDateToUse);
}

function unloadDoNotAllowFuture() {
$.timeago.settings.allowFuture = true;
disableMockedDate();
}

function loadDoNotAllowPast() {
var mockDateToUse = "2010-01-01";
$.timeago.settings.allowFuture = true;
$.timeago.settings.allowPast = false;
$.timeago.settings.strings.inPast = "in the past";
enableMockedDate(mockDateToUse);
}

function unloadDoNotAllowPast() {
$.timeago.settings.allowFuture = true;
$.timeago.settings.allowPast = true;
disableMockedDate();
}

function enableMockedDate(dateToReturn) {
var mockDate = dateToReturn;
window.NativeDate = Date;
window.Date = function () {
if(arguments.length === 0) {
return new window.NativeDate(mockDate);
} else if(arguments.length === 1) {
return new window.NativeDate(arguments[0]);
} else if(arguments.length === 7) {
return new window.NativeDate(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]);
} else {
throw "Mocking Date with this number of parameters is not implemented.";
}
}
}

function disableMockedDate() {
window.Date = window.NativeDate;
delete window.NativeDate;
}