(data, field, condition)
| 154 | } |
| 155 | |
| 156 | function compareNumbers(data, field, condition) { |
| 157 | let newData = data; |
| 158 | |
| 159 | if (!condition.value && condition.value !== 0 |
| 160 | && (condition.operator !== "isNull" && condition.operator !== "isNotNull")) { |
| 161 | return data; |
| 162 | } |
| 163 | |
| 164 | // extract value |
| 165 | const getValue = (obj) => { |
| 166 | const selectors = field.split("."); |
| 167 | let value = obj; |
| 168 | for (let i = 0; i < selectors.length; i++) { |
| 169 | value = value[selectors[i]]; |
| 170 | } |
| 171 | |
| 172 | if (!value && (value !== 0 || value !== "0")) { |
| 173 | return null; |
| 174 | } |
| 175 | |
| 176 | if (typeof value === "number") return value; |
| 177 | |
| 178 | // now check if values should be converted to numbers |
| 179 | if (/^\d+$/.test(value)) { |
| 180 | return parseInt(value, 10); |
| 181 | } else if (/^\d+\.\d+$/.test(value)) { |
| 182 | return parseFloat(value); |
| 183 | } |
| 184 | |
| 185 | return { |
| 186 | value, |
| 187 | isString: true, |
| 188 | }; |
| 189 | }; |
| 190 | |
| 191 | switch (condition.operator) { |
| 192 | case "is": |
| 193 | newData = _.filter(newData, (o) => { |
| 194 | const val = getValue(o); |
| 195 | if (val?.isString) return `${val.value}` === `${condition.value}`; |
| 196 | return getValue(o) === parseFloat(condition.value); |
| 197 | }); |
| 198 | break; |
| 199 | case "isNot": |
| 200 | newData = _.filter(newData, (o) => { |
| 201 | const val = getValue(o); |
| 202 | if (val?.isString) return `${val.value}` !== `${condition.value}`; |
| 203 | return getValue(o) !== parseFloat(condition.value); |
| 204 | }); |
| 205 | break; |
| 206 | case "contains": |
| 207 | newData = _.filter(newData, (o) => { |
| 208 | const val = getValue(o); |
| 209 | if (val?.isString) return getValue(o)?.indexOf(condition.value) > -1; |
| 210 | return getValue(o) === parseFloat(condition.value); |
| 211 | }); |
| 212 | break; |
| 213 | case "notContains": |
no test coverage detected