All files / FeatureServer/lib/helpers/fields fields.js

97.14% Statements 34/35
91.66% Branches 22/24
100% Functions 13/13
96.96% Lines 32/33

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 1031x 1x           1x                       88x   88x                         85x   85x           85x   85x             44x       85x 79x     37x   6x       15x 52x   45x     15x 15x 15x       70x 407x   70x 389x     70x   70x       15x 22x     15x 7x     8x     1x  
const _ = require('lodash')
const chalk = require('chalk')
const {
  ObjectIdField,
  FieldFromKeyValue,
  FieldFromFieldDefinition,
  ObjectIdFieldFromDefinition
} = require('./field-classes')
 
class Fields {
  static normalizeOptions (inputOptions) {
    const {
      features,
      metadata: {
        fields,
        idField
      } = {},
      attributeSample,
      ...options
    } = inputOptions
 
    return {
      idField: options.idField || idField,
      fieldDefinitions: options.fieldDefinitions || options.fields || fields,
      attributeSample: attributeSample || getAttributeSample(features, attributeSample),
      ...options
    }
  }
 
  constructor (options = {}) {
    const {
      fieldDefinitions,
      idField,
      attributeSample = {}
    } = options
 
    Iif (shouldWarnAboutMissingIdFieldDefinition(idField, fieldDefinitions)) {
      console.warn(
        chalk.yellow(`WARNING: provider's "idField" is set to ${idField}, but this field is not found in field-definitions`)
      )
    }
 
    const normalizedIdField = idField || 'OBJECTID'
 
    this.fields = fieldDefinitions
      ? setFieldsFromDefinitions(fieldDefinitions, normalizedIdField)
      : setFieldsFromProperties(attributeSample, normalizedIdField)
  }
}
 
function getAttributeSample (features) {
  return _.get(features, '[0].properties') || _.get(features, '[0].attributes', {})
}
 
function shouldWarnAboutMissingIdFieldDefinition (idField, fieldDefinitions) {
  if (!idField || !fieldDefinitions) {
    return
  }
 
  const fieldNames = fieldDefinitions.map(field => field.name)
 
  return !fieldNames.includes(idField)
}
 
function setFieldsFromDefinitions (fieldDefinitions, idField) {
  const fields = fieldDefinitions
    .filter(fieldDefinition => fieldDefinition.name !== idField)
    .map(fieldDefinition => {
      return new FieldFromFieldDefinition(fieldDefinition)
    })
 
  const idFieldDefinition = getIdFieldDefinition(fieldDefinitions, idField)
  fields.unshift(new ObjectIdFieldFromDefinition(idFieldDefinition))
  return fields
}
 
function setFieldsFromProperties (propertiesSample, idField) {
  const fieldNames = Object.keys(propertiesSample)
  const simpleFieldNames = fieldNames.filter(name => name !== idField)
 
  const fields = simpleFieldNames.map((key) => {
    return new FieldFromKeyValue(key, propertiesSample[key])
  })
 
  fields.unshift(new ObjectIdField(idField))
 
  return fields
}
 
function getIdFieldDefinition (fieldDefinitions, idField) {
  const idFieldDefinition = fieldDefinitions.find(definition => {
    return definition.name === idField
  })
 
  if (idFieldDefinition) {
    return idFieldDefinition
  }
 
  return { name: 'OBJECTID' }
}
 
module.exports = Fields