Skip to content
This repository has been archived by the owner on May 16, 2020. It is now read-only.

Commit

Permalink
Date filtering with multiple formats
Browse files Browse the repository at this point in the history
  • Loading branch information
kashav committed Apr 24, 2016
1 parent 7289e3c commit 3208cbb
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/api/athletics/routes/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export default function filter(req, res, next) {
filter.$and[i] = { $or: q[i].trim().split(' OR ') }
let mapReduceOr = []
for (let j = 0; j < filter.$and[i].$or.length; j++) {
let part = filter.$and[i].$or[j].trim().split(':')
let query = filter.$and[i].$or[j].trim()
let part = [query.slice(0, query.indexOf(':')), query.slice(query.indexOf(':') + 1)]

let x = formatPart(part[0], part[1])

if (x.isValid) {
Expand Down Expand Up @@ -154,11 +156,20 @@ function formatPart(key, part) {
if (['date', 'start', 'end'].indexOf(key) > -1) {
// Dates

let dateValues = part.value.split(',')
let d = dateValues.concat(new Array(7 - dateValues.length).fill(0))
let value = part.value
let dateValue = undefined

if (value.indexOf(',') !== -1) {
// Date format is Y,m,d,H,M,S
let d = part.value.split(',')
d = d.concat(new Array(7 - d.length).fill(0))
dateValue = new Date(d[0], d[1]-1, d[2], d[3]-4, d[4], d[5], d[6], d[7])
} else {
// Date format is ISO-8601, milliseconds since 01-01-1970, or empty
dateValue = part.value
}

// Months[1] start at index 0 (Jan->0, Dec->11), hours[3] are altered for EST
let date = new Date(d[0], d[1]-1, d[2], d[3]-4, d[4], d[5], d[6], d[7])
let date = dateValue ? new Date(dateValue) : new Date

if (isNaN(date)) {
response.isValid = false
Expand Down

0 comments on commit 3208cbb

Please sign in to comment.