import { GeoJSON , Feature , FeatureCollection , GeometryCollection , Point , LineString , MultiPoint , Polygon , MultiLineString , MultiPolygon , } from './geojson' ; export function assert ( condition , msg ? ) { if ( ! condition ) throw new Error ( msg ) ; } param { * } input returns { boolean } example export function isObject ( input ) { return ! ! input && input . constructor === Object ; } param { * } input returns { boolean } export function isArray ( input ) { return ! ! input && Object . prototype . toString . call ( input ) === '[object Array]' ; } param { * } num returns { boolean } example export function isNumber ( input ) { return ! isNaN ( Number ( input ) ) && input !== null && ! isArray ( input ) ; } param { * } input returns { boolean } export function isString ( input ) { return typeof input === 'string' ; } param { function [ ] } functions returns { function } export function compose ( ... funcs ) { const start = funcs . length - 1 ; return function ( ... args ) { let i = start ; let result = funcs [ start ] . apply ( null , args ) ; while ( i -- ) result = funcs [ i ] . call ( null , result ) ; return result ; } ; } name param { FeatureCollection | Feature | Geometry } geojson param { Function } callback param { boolean } excludeWrapCoord returns { void } example export function coordEach ( geojson , callback , excludeWrapCoord = false ) { if ( geojson === null ) return ; let j , k , l , geometry , stopG , coords , geometryMaybeCollection , wrapShrink = 0 , coordIndex = 0 , isGeometryCollection ; const { type } = geojson ; const isFeatureCollection = type === 'FeatureCollection' ; const isFeature = type === 'Feature' ; const stop = isFeatureCollection ? ( < FeatureCollection > geojson ) . features . length : 1 ; for ( let featureIndex = 0 ; featureIndex < stop ; featureIndex ++ ) { geometryMaybeCollection = isFeatureCollection ? ( < FeatureCollection > geojson ) . features [ featureIndex ] . geometry : isFeature ? ( < Feature > geojson ) . geometry : geojson ; isGeometryCollection = geometryMaybeCollection ? geometryMaybeCollection . type === 'GeometryCollection' : false ; stopG = isGeometryCollection ? ( < GeometryCollection > geometryMaybeCollection ) . geometries . length : 1 ; for ( let geomIndex = 0 ; geomIndex < stopG ; geomIndex ++ ) { let multiFeatureIndex = 0 ; let geometryIndex = 0 ; geometry = isGeometryCollection ? ( < GeometryCollection > geometryMaybeCollection ) . geometries [ geomIndex ] : geometryMaybeCollection ; if ( geometry === null ) continue ; const geomType = geometry . type ; wrapShrink = excludeWrapCoord && ( geomType === 'Polygon' || geomType === 'MultiPolygon' ) ? 1 : 0 ; switch ( geomType ) { case null : break ; case 'Point' : coords = ( < Point > geometry ) . coordinates ; if ( callback ( coords , coordIndex , featureIndex , multiFeatureIndex , geometryIndex ) === false ) return false ; coordIndex ++ ; multiFeatureIndex ++ ; break ; case 'LineString' : case 'MultiPoint' : coords = ( < LineString | MultiPoint > geometry ) . coordinates ; for ( j = 0 ; j < coords . length ; j ++ ) { if ( callback ( coords [ j ] , coordIndex , featureIndex , multiFeatureIndex , geometryIndex ) === false ) return false ; coordIndex ++ ; if ( geomType === 'MultiPoint' ) multiFeatureIndex ++ ; } if ( geomType === 'LineString' ) multiFeatureIndex ++ ; break ; case 'Polygon' : case 'MultiLineString' : coords = ( < Polygon | MultiLineString > geometry ) . coordinates ; for ( j = 0 ; j < coords . length ; j ++ ) { for ( k = 0 ; k < coords [ j ] . length - wrapShrink ; k ++ ) { if ( callback ( coords [ j ] [ k ] , coordIndex , featureIndex , multiFeatureIndex , geometryIndex ) === false ) return false ; coordIndex ++ ; } if ( geomType === 'MultiLineString' ) multiFeatureIndex ++ ; if ( geomType === 'Polygon' ) geometryIndex ++ ; } if ( geomType === 'Polygon' ) multiFeatureIndex ++ ; break ; case 'MultiPolygon' : coords = ( < MultiPolygon > geometry ) . coordinates ; for ( j = 0 ; j < coords . length ; j ++ ) { geometryIndex = 0 ; for ( k = 0 ; k < coords [ j ] . length ; k ++ ) { for ( l = 0 ; l < coords [ j ] [ k ] . length - wrapShrink ; l ++ ) { if ( callback ( coords [ j ] [ k ] [ l ] , coordIndex , featureIndex , multiFeatureIndex , geometryIndex ) === false ) return false ; coordIndex ++ ; } geometryIndex ++ ; } multiFeatureIndex ++ ; } break ; case 'GeometryCollection' : for ( j = 0 ; j < ( < GeometryCollection > geometry ) . geometries . length ; j ++ ) { if ( coordEach ( ( < GeometryCollection > geometry ) . geometries [ j ] , callback , excludeWrapCoord ) === false ) return false ; } break ; default : throw new Error ( 'Unknown Geometry Type' ) ; } } } }