(data, field, condition, timezone = "", timeInterval = "day")
| 4 | const determineType = require("../modules/determineType"); |
| 5 | |
| 6 | function compareDates(data, field, condition, timezone = "", timeInterval = "day") { |
| 7 | let newData = data; |
| 8 | let moment = null; |
| 9 | if (timezone) { |
| 10 | moment = (...args) => momentObj(...args).tz(timezone); |
| 11 | } else { |
| 12 | moment = (...args) => momentObj.utc(...args); |
| 13 | } |
| 14 | |
| 15 | if (!condition.value |
| 16 | && (condition.operator !== "isNull" && condition.operator !== "isNotNull")) { |
| 17 | return data; |
| 18 | } |
| 19 | |
| 20 | // extract value |
| 21 | const getValue = (obj) => { |
| 22 | const selectors = field.split("."); |
| 23 | let value = obj; |
| 24 | for (let i = 0; i < selectors.length; i++) { |
| 25 | value = value && value[selectors[i]]; |
| 26 | } |
| 27 | |
| 28 | if (!value) return null; |
| 29 | |
| 30 | // check for a potential timestamp format |
| 31 | if (parseInt(value, 10).toString() === value.toString() && value.toString().length === 10) { |
| 32 | return moment(value, "X"); |
| 33 | } |
| 34 | if (parseInt(value, 10).toString() === value.toString() && value.toString().length === 13) { |
| 35 | return moment(value, "x"); |
| 36 | } |
| 37 | |
| 38 | return momentObj.utc(value); |
| 39 | }; |
| 40 | |
| 41 | switch (condition.operator) { |
| 42 | case "is": |
| 43 | newData = _.filter(newData, (o) => { |
| 44 | const val = getValue(o); |
| 45 | |
| 46 | if (timeInterval === "day" || timeInterval === "week" || timeInterval === "month" || timeInterval === "year") { |
| 47 | return val |
| 48 | ? val.startOf("day").isSame(moment(condition.value).startOf("day")) |
| 49 | : false; |
| 50 | } |
| 51 | |
| 52 | return val ? val.isSame(condition.value, timeInterval) : false; |
| 53 | }); |
| 54 | break; |
| 55 | case "isNot": |
| 56 | newData = _.filter(newData, (o) => { |
| 57 | const val = getValue(o); |
| 58 | |
| 59 | if (timeInterval === "day" || timeInterval === "week" || timeInterval === "month" || timeInterval === "year") { |
| 60 | return val |
| 61 | ? !val.startOf("day").isSame(moment(condition.value).startOf("day")) |
| 62 | : false; |
| 63 | } |
no test coverage detected