Skip to content

Commit 2f2fb8f

Browse files
committed
Merge pull request rmm5t#129 from shish/cutoff-setting
Cutoff setting for leaving older dates alone. [Rebase]
2 parents e69b93c + efdbae1 commit 2f2fb8f

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

jquery.timeago.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
refreshMillis: 60000,
4242
allowFuture: false,
4343
localeTitle: false,
44+
cutoff: 0,
4445
strings: {
4546
prefixAgo: null,
4647
prefixFromNow: null,
@@ -150,8 +151,12 @@
150151

151152
function refresh() {
152153
var data = prepareData(this);
154+
var $s = $t.settings;
155+
153156
if (!isNaN(data.datetime)) {
154-
$(this).text(inWords(data.datetime));
157+
if ( $s.cutoff == 0 || distance(data.datetime) < $s.cutoff) {
158+
$(this).text(inWords(data.datetime));
159+
}
155160
}
156161
return this;
157162
}

test/index.html

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ <h2>Other formats</h2>
6363
<p><time id="testTimeTitle" class="timeago" datetime="2012-05-07T10:06:02+02:00" title="May 10, 2012 10:06">May 10, 2012</time></p>
6464
<p><time id="testTimeTitle2" class="timeago" datetime="2012-05-07T10:06:02+02:00">May 10, 2012</time></p>
6565

66+
<h2>Cutoff</h2>
67+
68+
<p>Date that is older than cutoff: <abbr class="timeago cutoff doCutoff" title="1978-12-18">(this should be displayed)</abbr></p>
69+
70+
<p>Date that is newer than cutoff: <abbr class="timeago loaded cutoff dontCutoff">(you shouldn't see this)</abbr></p>
71+
6672
<h2>Errors</h2>
6773

6874
<p>Bad (letters): <abbr class="bad timeago" title="bleh">(this should be displayed)</abbr>.</p>
@@ -212,9 +218,13 @@ <h2>Settings</h2>
212218

213219
prepareDynamicDates();
214220

215-
$("abbr.timeago").timeago();
221+
$("abbr.timeago").not("abbr.cutoff").timeago();
216222
$("time.timeago").timeago();
217223

224+
loadCutoffSetting();
225+
$("abbr.cutoff").timeago();
226+
unloadCutoffSetting();
227+
218228
var tooltip = $("#testTooltip").data("timeago");
219229

220230
$("abbr.todate").each(function () {
@@ -291,6 +301,20 @@ <h2>Settings</h2>
291301
ok(tooltip.datetime, "datetime set");
292302
});
293303

304+
module("Cutoff");
305+
306+
test("should not change dates older than cutoff setting", function () {
307+
ok(testElements("abbr.doCutoff", function (element) {
308+
return (element.html() === "(this should be displayed)");
309+
}), "Cutoff setting working fine");
310+
});
311+
312+
test("should change dates newer than cutoff setting", function () {
313+
ok(testElements("abbr.dontCutoff", function (element) {
314+
return (element.html() === "less than a minute ago");
315+
}), "Cutoff setting working fine");
316+
});
317+
294318
module("Tooltip");
295319

296320
test("should set title to original text contents", function () {

test/test_helpers.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ function unloadNumbers() {
2222
jQuery.timeago.settings.strings.numbers = [];
2323
}
2424

25+
function loadCutoffSetting() {
26+
jQuery.timeago.settings.cutoff = 7*24*60*60*1000;
27+
}
28+
29+
function unloadCutoffSetting() {
30+
jQuery.timeago.settings.cutoff = 0;
31+
}
32+
2533
function loadPigLatin() {
2634
jQuery.timeago.settings.strings = {
2735
suffixAgo: "ago-hay",

0 commit comments

Comments
 (0)