@turf/invariant
Advanced tools
Comparing version 3.10.0 to 3.13.0
@@ -5,2 +5,3 @@ /// <reference types="geojson" /> | ||
type Features = GeoJSON.FeatureCollection<any> | ||
type GetCoord = Feature | any[] | GeoJSON.GeometryObject | ||
@@ -10,3 +11,3 @@ /** | ||
*/ | ||
export function getCoord(obj: any): Array<number>; | ||
export function getCoord(obj: GetCoord): Array<any>; | ||
@@ -16,2 +17,7 @@ /** | ||
*/ | ||
export function getCoords(obj: GetCoord): Array<any>; | ||
/** | ||
* http://turfjs.org/docs/ | ||
*/ | ||
export function geojsonType(value: Features, type: string, name: string): void; | ||
@@ -18,0 +24,0 @@ |
95
index.js
/** | ||
* Unwrap a coordinate from a Feature with a Point geometry, a Point | ||
* geometry, or a single coordinate. | ||
* Unwrap a coordinate from a Point Feature, Geometry or a single coordinate. | ||
* | ||
* @param {*} obj any value | ||
* @returns {Array<number>} a coordinate | ||
* @param {Array<any>|Geometry|Feature<Point>} obj any value | ||
* @returns {Array<number>} coordinates | ||
*/ | ||
function getCoord(obj) { | ||
if (Array.isArray(obj) && | ||
typeof obj[0] === 'number' && | ||
typeof obj[1] === 'number') { | ||
return obj; | ||
} else if (obj) { | ||
if (obj.type === 'Feature' && | ||
obj.geometry && | ||
obj.geometry.type === 'Point' && | ||
Array.isArray(obj.geometry.coordinates)) { | ||
return obj.geometry.coordinates; | ||
} else if (obj.type === 'Point' && | ||
Array.isArray(obj.coordinates)) { | ||
return obj.coordinates; | ||
} | ||
if (!obj) throw new Error('No obj passed'); | ||
var coordinates = getCoords(obj); | ||
// getCoord() must contain at least two numbers (Point) | ||
if (coordinates.length > 1 && | ||
typeof coordinates[0] === 'number' && | ||
typeof coordinates[1] === 'number') { | ||
return coordinates; | ||
} else { | ||
throw new Error('Coordinate is not a valid Point'); | ||
} | ||
throw new Error('A coordinate, feature, or point geometry is required'); | ||
} | ||
/** | ||
* Unwrap coordinates from a Feature, Geometry Object or an Array of numbers | ||
* | ||
* @param {Array<any>|Geometry|Feature<any>} obj any value | ||
* @returns {Array<any>} coordinates | ||
*/ | ||
function getCoords(obj) { | ||
if (!obj) throw new Error('No obj passed'); | ||
var coordinates; | ||
// Array of numbers | ||
if (obj.length) { | ||
coordinates = obj; | ||
// Geometry Object | ||
} else if (obj.coordinates) { | ||
coordinates = obj.coordinates; | ||
// Feature | ||
} else if (obj.geometry && obj.geometry.coordinates) { | ||
coordinates = obj.geometry.coordinates; | ||
} | ||
// Checks if coordinates contains a number | ||
if (coordinates) { | ||
containsNumber(coordinates); | ||
return coordinates; | ||
} | ||
throw new Error('No valid coordinates'); | ||
} | ||
/** | ||
* Checks if coordinates contains a number | ||
* | ||
* @private | ||
* @param {Array<any>} coordinates GeoJSON Coordinates | ||
* @returns {boolean} true if Array contains a number | ||
*/ | ||
function containsNumber(coordinates) { | ||
if (coordinates.length > 1 && | ||
typeof coordinates[0] === 'number' && | ||
typeof coordinates[1] === 'number') { | ||
return true; | ||
} | ||
if (coordinates[0].length) { | ||
return containsNumber(coordinates[0]); | ||
} | ||
throw new Error('coordinates must only contain numbers'); | ||
} | ||
/** | ||
* Enforce expectations about types of GeoJSON objects for Turf. | ||
@@ -55,2 +99,3 @@ * | ||
function featureOf(feature, type, name) { | ||
if (!feature) throw new Error('No feature passed'); | ||
if (!name) throw new Error('.featureOf() requires a name'); | ||
@@ -70,3 +115,3 @@ if (!feature || feature.type !== 'Feature' || !feature.geometry) { | ||
* @alias collectionOf | ||
* @param {FeatureCollection} featurecollection a featurecollection for which features will be judged | ||
* @param {FeatureCollection} featureCollection a FeatureCollection for which features will be judged | ||
* @param {string} type expected GeoJSON type | ||
@@ -76,9 +121,10 @@ * @param {string} name name of calling function | ||
*/ | ||
function collectionOf(featurecollection, type, name) { | ||
function collectionOf(featureCollection, type, name) { | ||
if (!featureCollection) throw new Error('No featureCollection passed'); | ||
if (!name) throw new Error('.collectionOf() requires a name'); | ||
if (!featurecollection || featurecollection.type !== 'FeatureCollection') { | ||
if (!featureCollection || featureCollection.type !== 'FeatureCollection') { | ||
throw new Error('Invalid input to ' + name + ', FeatureCollection required'); | ||
} | ||
for (var i = 0; i < featurecollection.features.length; i++) { | ||
var feature = featurecollection.features[i]; | ||
for (var i = 0; i < featureCollection.features.length; i++) { | ||
var feature = featureCollection.features[i]; | ||
if (!feature || feature.type !== 'Feature' || !feature.geometry) { | ||
@@ -97,1 +143,2 @@ throw new Error('Invalid input to ' + name + ', Feature with geometry required'); | ||
module.exports.getCoord = getCoord; | ||
module.exports.getCoords = getCoords; |
{ | ||
"name": "@turf/invariant", | ||
"version": "3.10.0", | ||
"description": "enforce expectations about inputs to turf", | ||
"version": "3.13.0", | ||
"description": "turf invariant module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "node test.js", | ||
"bench": "node bench.js" | ||
}, | ||
@@ -20,6 +26,2 @@ "keywords": [ | ||
}, | ||
"devDependencies": { | ||
"tape": "^3.5.0" | ||
}, | ||
"dependencies": {}, | ||
"homepage": "https://github.com/Turfjs/turf", | ||
@@ -29,3 +31,8 @@ "bugs": { | ||
}, | ||
"types": "index.d.ts" | ||
"devDependencies": { | ||
"@turf/helpers": "^3.13.0", | ||
"benchmark": "^2.1.3", | ||
"tape": "^3.5.0" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -5,11 +5,20 @@ # @turf/invariant | ||
Unwrap a coordinate from a Feature with a Point geometry, a Point | ||
geometry, or a single coordinate. | ||
Unwrap a coordinate from a Point Feature, Geometry or a single coordinate. | ||
**Parameters** | ||
- `obj` **Any** any value | ||
- `obj` **([Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<any> | [Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<[Point](http://geojson.org/geojson-spec.html#point)>)** any value | ||
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** a coordinate | ||
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** coordinates | ||
# getCoords | ||
Unwrap coordinates from a Feature, Geometry Object or an Array of numbers | ||
**Parameters** | ||
- `obj` **([Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<any> | [Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any value | ||
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<any>** coordinates | ||
# geojsonType | ||
@@ -49,3 +58,3 @@ | ||
- `featurecollection` **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)** a featurecollection for which features will be judged | ||
- `featureCollection` **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)** a FeatureCollection for which features will be judged | ||
- `type` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** expected GeoJSON type | ||
@@ -52,0 +61,0 @@ - `name` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** name of calling function |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
88
11337
3
5
152
1