Turfjs / turf

A modular geospatial engine written in JavaScript and TypeScript
https://turfjs.org/
MIT License
9.3k stars 941 forks source link

Functions for type coercion #1746

Open noerw opened 5 years ago

noerw commented 5 years ago

Maybe I overlooked something, but I'm missing functions for ensuring a specific GeoJSON type. This would make writing processing logic for generic input data much simpler.

// typedef proposal
import { AllGeoJSON, Types, GeoJSONObject } from '@turf/helpers'
declare function ensureType<T: Types> (data: AllGeoJSON, type: T): GeoJSONObject<T>

// usage
ensureType({ type: 'Point', coordinates: [0,0] }, 'FeatureCollection')
// result is { type: 'FeatureCollection', features: [{ type: 'Feature', geometry: { type: 'Point', coordinates: [0,0] }] }

Considering that coercing between geometry types is ambiguous or impossible, it might be easier to only implement T: 'Feature' | 'FeatureCollection'.

noerw commented 5 years ago

I have an implementation ready, und would provide a pull to turf. Would this be best added to helpers or invariant?

morganherlocker commented 5 years ago

Thanks for the proposal @noerw. I do not think this should be added. Most Turf modules target a specific, substantial geospatial operation. This seems like a useful utility, but would probably be better as a helper function in a downstream application's codebase. I generally avoid features that the language already supports, such as array manipulation. In this case, and if/else would be sufficient for normalizing to a feature collection type.