orgdo
Version:
Command-line tool to manage the Todo lists
79 lines (78 loc) • 2.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
const RE_RDAYS = /^([\-><])?\d+$/;
const DAY_MS = 86400000;
function newTimerFilter(daystr) {
daystr = _.trim(daystr);
if (!RE_RDAYS.test(daystr)) {
throw Error("Invalid daystr");
}
const mark = daystr[0];
switch (mark) {
case ">":
return date => {
const time = date.getTime();
const now = Date.now();
return time > now + DAY_MS * parseFloat(daystr.slice(1));
};
case "-":
return date => {
const time = date.getTime();
const now = Date.now();
return time > now - DAY_MS * parseFloat(daystr.slice(1)) && time <= now;
};
case "<":
return date => {
const time = date.getTime();
const now = Date.now();
return time < now - DAY_MS * parseFloat(daystr.slice(1));
};
default:
return date => {
const time = date.getTime();
const now = Date.now();
return time > now && time < now + DAY_MS * parseFloat(daystr);
};
}
}
exports.newTimerFilter = newTimerFilter;
function newTagsFilter(tagsGroup) {
return (tags) => {
for (const tagstr of tagsGroup) {
if (!tagstr.split(",").some(tag => tags.indexOf(tag) > -1)) {
return false;
}
}
return true;
};
}
exports.newTagsFilter = newTagsFilter;
function newRegexpFilter(regexp) {
return (value) => new RegExp(regexp).test(value);
}
exports.newRegexpFilter = newRegexpFilter;
function newEqualAnyFilter(matches) {
return (value) => {
let matchesArr;
if (typeof matches === "string") {
matchesArr = [matches];
}
else {
matchesArr = matches;
}
return Boolean(matchesArr.find(v => v === value));
};
}
exports.newEqualAnyFilter = newEqualAnyFilter;
function combine(filters) {
return task => {
for (const f of filters) {
if (!f(task)) {
return false;
}
}
return true;
};
}
exports.combine = combine;