Skip to content

Commit 241e1ff

Browse files
add 'current-date' as an option to the 'time-between' accessors
1 parent dd5f8dd commit 241e1ff

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

lib/node-rules.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@
8585
obj);
8686
};
8787

88+
var toDate = function (val) {
89+
switch (val) {
90+
case 'now': return moment.utc();
91+
case 'current-date': return moment.utc().startOf('day');
92+
default: return moment.utc(b);
93+
}
94+
}
8895

8996
var compileValue = function (facts, value) {
9097
if (!_.isObject(value) || _.isArray(value)) return [true, value];
@@ -122,15 +129,14 @@
122129
throw new Error('Invalid property for date diff: ' + key);
123130

124131
var [isValid, a] = compileValue(facts, dates[0]);
125-
if (!isValid || !(a === 'now' || moment(a).isValid()))
132+
if (!isValid || !(['now', 'current-date'].includes(a) || moment(a).isValid()))
126133
throw new Error('Invalid property for date diff: ' + key);
127134

128135
var [isValid, b] = compileValue(facts, dates[1]);
129-
if (!isValid || !(b === 'now' || moment(b).isValid()))
136+
if (!isValid || !(['now', 'current-date'].includes(b) || moment(b).isValid()))
130137
throw new Error('Invalid property for date diff: ' + key);
131138

132-
return [true, moment(a === 'now' ? undefined : a).diff(b === 'now' ? undefined : b, key.slice(1, -7), dates[2] === 'exact') +
133-
(dates[2] === 'inclusive' ? 1 : 0)];
139+
return [true, toDate(a).diff(toDate(b), key.slice(1, -7), dates[2] === 'exact') + (dates[2] === 'inclusive' ? 1 : 0)];
134140
}
135141

136142
return [false];

0 commit comments

Comments
 (0)