Skip to content
This repository

New feature: Cutoff time for applying inWords #21

Closed
wants to merge 1 commit into from

4 participants

Martin Solli Philipp Kyeck Tomasz Nurkiewicz Ryan McGeary
Martin Solli

I wanted to control when timeago applies inWords. Older dates should appear with their original representation, and newer dates, for instance newer than a week, should get the fuzzy timestamp.

This commit adds a setting, cutoff, that can be set to the number of milliseconds that marks the cutoff between using the original and the fuzzy date.

I've tried to use your coding style, and passing tests are included, even if this is the first time I've written Javascript unit tests. Hope you can use it!

-martin

Martin Solli Cutoff setting for leaving older dates alone.
The cutoff setting, if set to something other than 0, ensures that dates
older than this number of milliseconds is not touched by timeago.
e634e2c
Philipp Kyeck

+1 this is definitely missing

Tomasz Nurkiewicz

+1, pretty cool feature.

Ryan McGeary
Owner
rmm5t commented March 07, 2013

Sorry, I haven't done a great job of monitoring most of these PRs lately, but I'm about to refocus some efforts to this library again in an effort for a 2.0 release (might be a full rewrite). In the meantime, if someone can rebase this, I'll pull it in and release a new 1.x release.

Ryan McGeary rmm5t referenced this pull request March 07, 2013
Closed

Ageout config #116

Ryan McGeary
Owner
rmm5t commented March 07, 2013

Also, it's worth noting that this feature isn't really necessary. You can always opt to avoid the class on the time elements that are older than a particular date from the server side. That, or add a cutoff class for older elements server side. However, this is an often asked for feature, so I'll concede, especially for the case where content is more static. I'm going to consolidate all the related issues/PRs toward this implementation to get it rebased and pulled in.

Ryan McGeary rmm5t closed this in #129 May 18, 2013
Ryan McGeary
Owner
rmm5t commented May 18, 2013

Long time no action, but v1.2.0 now incorporates this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 27, 2010
Martin Solli Cutoff setting for leaving older dates alone.
The cutoff setting, if set to something other than 0, ensures that dates
older than this number of milliseconds is not touched by timeago.
e634e2c
This page is out of date. Refresh to see the latest.
7  jquery.timeago.js
@@ -25,6 +25,7 @@
25 25
     settings: {
26 26
       refreshMillis: 60000,
27 27
       allowFuture: false,
  28
+      cutoff: 0,
28 29
       strings: {
29 30
         prefixAgo: null,
30 31
         prefixFromNow: null,
@@ -111,8 +112,12 @@
111 112
 
112 113
   function refresh() {
113 114
     var data = prepareData(this);
  115
+    var $s = $t.settings;
  116
+
114 117
     if (!isNaN(data.datetime)) {
115  
-      $(this).text(inWords(data.datetime));
  118
+      if ( $s.cutoff == 0 || distance(data.datetime) < $s.cutoff) {
  119
+        $(this).text(inWords(data.datetime));
  120
+      }
116 121
     }
117 122
     return this;
118 123
   }
26  test/index.html
@@ -73,6 +73,12 @@ <h2 id="qunit-userAgent"></h2>
73 73
     <p>Date only (default tooltip): <abbr id="defaultTooltip" class="timeago" title="2008-02-26"> </abbr>.</p>
74 74
     <p>Timestsamp (with millis): <abbr class="timeago" title="1978-12-18T17:17:00.021Z">(you shouldn't see this)</abbr>.</p>
75 75
 
  76
+    <h2>Cutoff</h2>
  77
+
  78
+    <p>Date that is older than cutoff: <abbr class="timeago cutoff doCutoff" title="1978-12-18">(this should be displayed)</abbr></p>
  79
+
  80
+    <p>Date that is newer than cutoff: <abbr class="timeago loaded cutoff dontCutoff">(you shouldn't see this)</abbr></p>
  81
+
76 82
     <h2>Errors</h2>
77 83
 
78 84
     <p>Bad (letters): <abbr class="bad timeago" title="bleh">(this should be displayed)</abbr>.</p>
@@ -212,9 +218,13 @@ <h2 id="qunit-userAgent"></h2>
212 218
 
213 219
       prepareDynamicDates();
214 220
 
215  
-      $("abbr.timeago").timeago();
  221
+      $("abbr.timeago").not("abbr.cutoff").timeago();
216 222
       $("time.timeago").timeago();
217 223
 
  224
+      loadCutoffSetting();
  225
+      $("abbr.cutoff").timeago();
  226
+      unloadCutoffSetting();
  227
+
218 228
       var tooltip = $("#testTooltip").data("timeago");
219 229
 
220 230
       $("abbr.todate").each(function () {
@@ -285,6 +295,20 @@ <h2 id="qunit-userAgent"></h2>
285 295
         ok(tooltip.datetime, "datetime set");
286 296
       });
287 297
 
  298
+      module("Cutoff");
  299
+
  300
+      test("should not change dates older than cutoff setting", function () {
  301
+        ok(testElements("abbr.doCutoff", function (element) {
  302
+          return (element.html() === "(this should be displayed)");
  303
+        }), "Cutoff setting working fine");
  304
+      });
  305
+
  306
+      test("should change dates newer than cutoff setting", function () {
  307
+        ok(testElements("abbr.dontCutoff", function (element) {
  308
+          return (element.html() === "less than a minute ago");
  309
+        }), "Cutoff setting working fine");
  310
+      });
  311
+
288 312
       module("Tooltip");
289 313
 
290 314
       test("should set title to original text contents", function () {
8  test/test_helpers.js
@@ -22,6 +22,14 @@ function unloadNumbers() {
22 22
   jQuery.timeago.settings.strings.numbers = [];
23 23
 }
24 24
 
  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
+
25 33
 function loadPigLatin() {
26 34
   jQuery.timeago.settings.strings = {
27 35
     suffixAgo: "ago-hay",
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.