All files / lib/query render-precalculated-statistics.js

100% Statements 15/15
100% Branches 8/8
100% Functions 6/6
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 462x 2x     2x         2x   2x 2x         2x             2x 3x             3x 13x 4x   9x         13x     2x  
const _ = require('lodash')
const { isValidISODateString, isValidDate } = require('iso-datestring-validator')
const {
  StatisticsFields
} = require('../helpers/fields')
 
function renderPrecalculatedStatisticsResponse (input, options) {
  const {
    statistics
  } = input
 
  const normalizedStatistics = Array.isArray(statistics) ? statistics : [statistics]
  const fields = StatisticsFields.create({
    ...input,
    ...options
  })
 
  return {
    fields,
    features: createStatisticFeatures(normalizedStatistics)
  }
}
 
function createStatisticFeatures (statistics) {
  return statistics.map(statistic => {
    return {
      attributes: convertDatePropertiesToTimestamps(statistic)
    }
  })
}
 
function convertDatePropertiesToTimestamps (obj) {
  return _.mapValues(obj, value => {
    if (isDate(value)) {
      return new Date(value).getTime()
    }
    return value
  })
}
 
function isDate (value) {
  return value instanceof Date || ((typeof value === 'string') && (isValidDate(value) || isValidISODateString(value)))
}
 
module.exports = { renderPrecalculatedStatisticsResponse }