winnow
Version:
Apply sql-like filters to GeoJSON
34 lines (26 loc) • 926 B
JavaScript
const _ = require('lodash')
const { detectEsriFieldType } = require('./helpers')
function normalizeCollection (collection, features) {
if (!collection) return
const clonedCollection = _.cloneDeep(collection)
// Ensure metadata property exists
clonedCollection.metadata = clonedCollection.metadata || {}
// metadata.fields not set and at least one feature with properties
if (!clonedCollection.metadata.fields && _.get(features, '[0].properties')) {
// fields generated by inspection of feature
const fields = getFieldsFromFeature(features[0])
clonedCollection.metadata.fields = fields
}
return clonedCollection
}
function getFieldsFromFeature (feature) {
if (!feature) return
const { properties = {} } = feature
return Object.keys(properties).map(key => {
return {
name: key,
type: detectEsriFieldType(properties[key])
}
})
}
module.exports = normalizeCollection