support future time range

This commit is contained in:
Mitsuhiro Tanda
2016-10-18 19:11:46 +09:00
parent 888619d74e
commit a8dd44918b
2 changed files with 19 additions and 6 deletions

View File

@@ -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';

View File

@@ -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');