@dexpenses/rule-conditions
Version:
Condition engine and parser for Dexpenses rules
34 lines • 1.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const luxon_1 = require("luxon");
const Operator_1 = require("../Operator");
const core_1 = require("@dexpenses/core");
class DateCondition {
constructor(dateField, op, value) {
this.dateField = dateField;
this.value = value;
this.cmp = Operator_1.parseOperator(op);
}
test(receipt) {
if (!receipt.date) {
return false;
}
const date = core_1.Receipt.getDate(receipt);
const dt = luxon_1.DateTime.fromJSDate(date, {
zone: 'Europe/Berlin',
});
if (!dt.isValid) {
return false;
}
const field = dt[this.dateField];
if (!field) {
throw new Error('unknown date time field: ' + this.dateField);
}
if (typeof field !== 'number') {
throw new Error('not a numeric field: ' + this.dateField);
}
return this.cmp(field, this.value);
}
}
exports.default = DateCondition;
//# sourceMappingURL=DateCondition.js.map