Comparing version 0.0.8 to 0.1.0
{ | ||
"name": "geojsonjs", | ||
"version": "0.0.8", | ||
"version": "0.1.0", | ||
"description": "Build and validate GeoJSON", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -12,2 +12,6 @@ # Build and validate GeoJSON with Node.js | ||
- [Usage](#usage) | ||
- [Documentation](#documentation) | ||
- [Global functions](#global-functions) | ||
- [Validation](#validation) | ||
- [Types](#types) | ||
- [Contributing](#contributing) | ||
@@ -45,4 +49,4 @@ - [License](#license) | ||
import { toFeatureCollection, validate } from 'geojsonjs'; | ||
const featureCollection = toFeatureCollection(geom); | ||
import { getFeatureCollection, validate } from 'geojsonjs'; | ||
const featureCollection = getFeatureCollection(geom); | ||
const result = validate(featureCollection); | ||
@@ -52,3 +56,3 @@ | ||
import { | ||
toFeatureCollection, | ||
getFeatureCollection, | ||
validate, | ||
@@ -58,3 +62,3 @@ FeatureCollection, | ||
} from 'geojsonjs'; | ||
const featureCollection: FeatureCollection = toFeatureCollection(geom); | ||
const featureCollection: FeatureCollection = getFeatureCollection(geom); | ||
const result: ValidationResult = validate(featureCollection); | ||
@@ -61,0 +65,0 @@ ``` |
import { AllTypes, CoordinatesTypes, Feature, FeatureCollection, Geometry, ValidationResult } from './types'; | ||
export declare function validate(geom: AllTypes): ValidationResult; | ||
export declare function validateCoordinatesByDepth(coordinates: any[], depth?: number): boolean; | ||
export declare function validateCoordinatesByDepth(coordinates: any[], depth?: number): ValidationResult; | ||
export declare function validateCoordinates(type: string, coordinates: CoordinatesTypes): ValidationResult; | ||
@@ -5,0 +5,0 @@ export declare function validateGeometry(geom: Geometry): ValidationResult; |
@@ -24,7 +24,29 @@ "use strict"; | ||
if (depth === 0) { | ||
return Array.isArray(coordinates) && coordinates.every((c) => (0, lodash_1.isNumber)(c)); | ||
if (!Array.isArray(coordinates) || coordinates.length !== 2) { | ||
return transformResponse(types_1.ValidationError.INVALID_COORDINATES, { | ||
coordinates, | ||
}); | ||
} | ||
const everyItemIsNumber = coordinates.every((c) => (0, lodash_1.isNumber)(c)); | ||
if (!everyItemIsNumber) { | ||
return transformResponse(types_1.ValidationError.INVALID_COORDINATES, { | ||
coordinates, | ||
}); | ||
} | ||
return transformResponse(); | ||
} | ||
if (!Array.isArray(coordinates)) | ||
return false; | ||
return coordinates.every((c) => validateCoordinatesByDepth(c, depth - 1)); | ||
if (!Array.isArray(coordinates)) { | ||
return transformResponse(types_1.ValidationError.INVALID_COORDINATES, { | ||
coordinates, | ||
}); | ||
} | ||
const invalidCoordinates = coordinates | ||
.map((c) => validateCoordinatesByDepth(c, depth - 1)) | ||
.filter((i) => !i.valid); | ||
if (invalidCoordinates === null || invalidCoordinates === void 0 ? void 0 : invalidCoordinates.length) { | ||
return transformResponse(types_1.ValidationError.INVALID_COORDINATES, { | ||
coordinates: invalidCoordinates, | ||
}); | ||
} | ||
return transformResponse(); | ||
} | ||
@@ -44,24 +66,53 @@ exports.validateCoordinatesByDepth = validateCoordinatesByDepth; | ||
} | ||
let valid = false; | ||
if ([types_1.GeometryType.POINT].includes(type)) { | ||
valid = validateCoordinatesByDepth(coordinates, 0); | ||
function handleMulti(type) { | ||
const invalidCoordinates = coordinates | ||
.map((c) => validateCoordinates(type, c)) | ||
.filter((i) => !i.valid); | ||
if (!(invalidCoordinates === null || invalidCoordinates === void 0 ? void 0 : invalidCoordinates.length)) | ||
return transformResponse(); | ||
return transformResponse(types_1.ValidationError.INVALID_COORDINATES, { | ||
coordinates: invalidCoordinates, | ||
}); | ||
} | ||
else if ([types_1.GeometryType.MULTI_POINT, types_1.GeometryType.LINE_STRING].includes(type)) { | ||
valid = validateCoordinatesByDepth(coordinates, 1); | ||
if (type === types_1.GeometryType.POINT) { | ||
return validateCoordinatesByDepth(coordinates, 0); | ||
} | ||
else if ([types_1.GeometryType.MULTI_LINE_STRING, types_1.GeometryType.POLYGON].includes(type)) { | ||
valid = validateCoordinatesByDepth(coordinates, 2); | ||
else if (type === types_1.GeometryType.LINE_STRING) { | ||
if ((coordinates === null || coordinates === void 0 ? void 0 : coordinates.length) < 2) { | ||
return transformResponse(types_1.ValidationError.INVALID_COORDINATES, { | ||
coordinates, | ||
}); | ||
} | ||
return validateCoordinatesByDepth(coordinates, 1); | ||
} | ||
else if ([types_1.GeometryType.MULTI_POLYGON].includes(type)) { | ||
valid = validateCoordinatesByDepth(coordinates, 3); | ||
else if (type === types_1.GeometryType.POLYGON) { | ||
if ((coordinates === null || coordinates === void 0 ? void 0 : coordinates.length) < 3) { | ||
return transformResponse(types_1.ValidationError.INVALID_COORDINATES, { | ||
coordinates, | ||
}); | ||
} | ||
const start = coordinates[0]; | ||
const end = coordinates[coordinates.length - 1]; | ||
if (!(0, lodash_1.isEqual)(start, end)) { | ||
return transformResponse(types_1.ValidationError.INVALID_COORDINATES, { | ||
coordinates, | ||
}); | ||
} | ||
return validateCoordinatesByDepth(coordinates, 2); | ||
} | ||
if (!valid) { | ||
return transformResponse(types_1.ValidationError.INVALID_COORDINATES, { | ||
coordinates, | ||
}); | ||
else if (type === types_1.GeometryType.MULTI_POINT) { | ||
return handleMulti(types_1.GeometryType.POINT); | ||
} | ||
return transformResponse(); | ||
else if (type === types_1.GeometryType.MULTI_LINE_STRING) { | ||
return handleMulti(types_1.GeometryType.LINE_STRING); | ||
} | ||
else if (type === types_1.GeometryType.MULTI_POLYGON) { | ||
return handleMulti(types_1.GeometryType.POLYGON); | ||
} | ||
} | ||
exports.validateCoordinates = validateCoordinates; | ||
function validateGeometry(geom) { | ||
if (!geom) { | ||
return transformResponse(types_1.ValidationError.EMTPY); | ||
} | ||
const coordinates = geom === null || geom === void 0 ? void 0 : geom.coordinates; | ||
@@ -89,2 +140,5 @@ if (!(coordinates === null || coordinates === void 0 ? void 0 : coordinates.length)) { | ||
function validateFeatures(features) { | ||
if (!(features === null || features === void 0 ? void 0 : features.length)) { | ||
return transformResponse(types_1.ValidationError.EMPTY_FEATURES); | ||
} | ||
const invalidFeatures = features === null || features === void 0 ? void 0 : features.map((feature, index) => { | ||
@@ -121,3 +175,3 @@ const valid = validateFeature(feature); | ||
.filter((t) => !types.includes(t)); | ||
if (!(invalidTypes === null || invalidTypes === void 0 ? void 0 : invalidTypes.length)) { | ||
if (invalidTypes === null || invalidTypes === void 0 ? void 0 : invalidTypes.length) { | ||
return transformResponse(types_1.ValidationError.INVALID_TYPE, { | ||
@@ -124,0 +178,0 @@ types: invalidTypes, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46962
385
512