Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
@turf/invariant
Advanced tools
@turf/invariant is a module within the Turf.js library that provides utility functions for validating and extracting GeoJSON objects. It ensures that the input data conforms to the expected GeoJSON format, which is crucial for performing various geospatial operations.
getCoord
The `getCoord` function extracts the coordinates from a GeoJSON Point feature. This is useful when you need to work directly with the coordinates of a point.
const turf = require('@turf/invariant');
const point = { type: 'Feature', geometry: { type: 'Point', coordinates: [102.0, 0.5] } };
const coord = turf.getCoord(point);
console.log(coord); // [102.0, 0.5]
getCoords
The `getCoords` function extracts the coordinates from a GeoJSON LineString or Polygon feature. This is useful for accessing the array of coordinates that make up the geometry.
const turf = require('@turf/invariant');
const line = { type: 'Feature', geometry: { type: 'LineString', coordinates: [[102.0, 0.0], [103.0, 1.0]] } };
const coords = turf.getCoords(line);
console.log(coords); // [[102.0, 0.0], [103.0, 1.0]]
getGeom
The `getGeom` function extracts the geometry object from a GeoJSON feature. This is useful when you need to work with the geometry independently of the feature properties.
const turf = require('@turf/invariant');
const feature = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [102.0, 0.0]]] } };
const geom = turf.getGeom(feature);
console.log(geom); // { type: 'Polygon', coordinates: [[[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [102.0, 0.0]]] }
getType
The `getType` function returns the type of a GeoJSON object. This is useful for validating the type of geometry you are working with.
const turf = require('@turf/invariant');
const feature = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [102.0, 0.0]]] } };
const type = turf.getType(feature);
console.log(type); // 'Polygon'
The `geojson-validation` package provides functions to validate GeoJSON objects against the GeoJSON specification. It is similar to @turf/invariant in that it ensures the input data conforms to the expected GeoJSON format, but it focuses more on validation rather than extraction of specific components.
The `geojson-utils` package offers a set of utility functions for working with GeoJSON data, including validation, distance calculations, and bounding box operations. It provides broader functionality compared to @turf/invariant, which is more focused on validation and extraction.
Unwrap a coordinate from a Point Feature, Geometry or a single coordinate.
var pt = turf.point([10, 10]);
var coord = turf.getCoord(pt);
//= [10, 10]
Returns Array<number> coordinates
Unwrap coordinates from a Feature, Geometry Object or an Array
var poly = turf.polygon([[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]);
var coords = turf.getCoords(poly);
//= [[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]
Returns Array<any> coordinates
Checks if coordinates contains a number
coordinates
Array<any> GeoJSON CoordinatesReturns boolean true if Array contains a number
Enforce expectations about types of GeoJSON objects for Turf.
value
GeoJSON any GeoJSON objecttype
string expected GeoJSON typename
string name of calling functionEnforce expectations about types of Feature inputs for Turf. Internally this uses geojsonType to judge geometry types.
feature
Feature a feature with an expected geometry typetype
string expected GeoJSON typename
string name of calling functionEnforce expectations about types of FeatureCollection inputs for Turf. Internally this uses geojsonType to judge geometry types.
featureCollection
FeatureCollection a FeatureCollection for which features will be judgedtype
string expected GeoJSON typename
string name of calling functionGet Geometry from Feature or Geometry Object
var point = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [110, 40]
}
}
var geom = turf.getGeom(point)
//={"type": "Point", "coordinates": [110, 40]}
Returns (Geometry | null) GeoJSON Geometry Object
Get GeoJSON object's type, Geometry type is prioritize.
geojson
GeoJSON GeoJSON object_name
string? name
string name of the variable to display in error message (unused) (optional, default "geojson"
)var point = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [110, 40]
}
}
var geom = turf.getType(point)
//="Point"
Returns string GeoJSON type
This module is part of the Turfjs project, an open source module collection dedicated to geographic algorithms. It is maintained in the Turfjs/turf repository, where you can create PRs and issues.
Install this single module individually:
$ npm install @turf/invariant
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf
FAQs
turf invariant module
We found that @turf/invariant demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.