Mitsuhiro Tanda пре 9 година
родитељ
комит
a8dd44918b
2 измењених фајлова са 19 додато и 6 уклоњено
  1. 12 6
      public/app/core/utils/rangeutil.ts
  2. 7 0
      public/test/core/utils/rangeutil_specs.ts

+ 12 - 6
public/app/core/utils/rangeutil.ts

@@ -82,8 +82,9 @@ function formatDate(date) {
 // now/d
 // if no to <expr> then to now is assumed
 export function describeTextRange(expr: any) {
+  let isLast = (expr.indexOf('+') !== 0);
   if (expr.indexOf('now') === -1) {
-    expr = 'now-' + expr;
+    expr = (isLast ? 'now-' : 'now') + expr;
   }
 
   let opt = rangeIndex[expr + ' to now'];
@@ -91,15 +92,20 @@ export function describeTextRange(expr: any) {
     return opt;
   }
 
-  opt = {from: expr, to: 'now'};
+  if (isLast) {
+    opt = {from: expr, to: 'now'};
+  } else {
+    opt = {from: 'now', to: expr};
+  }
 
-  let parts = /^now-(\d+)(\w)/.exec(expr);
+  let parts = /^now([-+])(\d+)(\w)/.exec(expr);
   if (parts) {
-    let unit = parts[2];
-    let amount = parseInt(parts[1]);
+    let unit = parts[3];
+    let amount = parseInt(parts[2]);
     let span = spans[unit];
     if (span) {
-      opt.display = 'Last ' + amount + ' ' + span.display;
+      opt.display = isLast ? 'Last ' : 'Next ';
+      opt.display += amount + ' ' + span.display;
       opt.section = span.section;
       if (amount > 1) {
         opt.display += 's';

+ 7 - 0
public/test/core/utils/rangeutil_specs.ts

@@ -31,6 +31,13 @@ describe("rangeUtil", () => {
       expect(info.from).to.be('now-13h')
     });
 
+    it('should handle non default future amount', () => {
+      var info = rangeUtil.describeTextRange('+3h');
+      expect(info.display).to.be('Next 3 hours')
+      expect(info.from).to.be('now')
+      expect(info.to).to.be('now+3h')
+    });
+
     it('should handle now/d', () => {
       var info = rangeUtil.describeTextRange('now/d');
       expect(info.display).to.be('Today so far');