All files / lib/query render-count-and-extent.js

100% Statements 19/19
100% Branches 14/14
100% Functions 2/2
100% Lines 19/19

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 46 47 48 49 50 51 52 53 54 55 56 57 58 592x 2x             9x   9x 1x           8x 1x         7x                       8x   8x 2x           6x 3x     6x 2x 4x 1x     6x     2x  
const _ = require('lodash')
const esriExtent = require('esri-extent')
 
function renderCountAndExtentResponse (data, params) {
  const {
    returnCountOnly,
    returnExtentOnly,
    outSR
  } = params
 
  if (returnCountOnly && returnExtentOnly) {
    return {
      count: _.get(data, 'features.length', 0),
      extent: getExtent(data, outSR)
    }
  }
 
  if (returnCountOnly) {
    return {
      count: _.get(data, 'features.length', 0)
    }
  }
 
  return {
    extent: getExtent(data, outSR)
  }
}
 
/**
 * Get an extent object for passed GeoJSON
 * @param {object} geojson
 * @param {*} outSR Esri spatial reference object, or WKID integer
 */
function getExtent (geojson, outSR) {
  // Calculate extent from features
  const extent = esriExtent(geojson)
 
  if (!outSR) {
    return extent
  }
 
  // esri-extent assumes WGS84, but data passed in may have CRS.
  // Math should be the same different CRS but we need to alter the spatial reference
 
  if (_.isObject(outSR)) {
    extent.spatialReference = outSR
  }
 
  if (Number.isInteger(Number(outSR))) {
    extent.spatialReference = { wkid: Number(outSR) }
  } else if (_.isString(outSR)) {
    extent.spatialReference = { wkt: outSR }
  }
 
  return extent
}
 
module.exports = { renderCountAndExtentResponse }