@turf/helpers
Advanced tools
Comparing version 4.4.0 to 4.5.1
@@ -118,1 +118,6 @@ /// <reference types="geojson" /> | ||
export function convertDistance(distance: number, originalUnit: Units, finalUnit?: Units): number | ||
/** | ||
* http://turfjs.org/docs/#convertarea | ||
*/ | ||
export function convertArea(area: number, originalUnit?: Units, finalUnit?: Units): number |
36
index.js
@@ -45,3 +45,3 @@ /** | ||
if (coordinates.length < 2) throw new Error('Coordinates must be at least 2 numbers long'); | ||
if (typeof coordinates[0] !== 'number' || typeof coordinates[1] !== 'number') throw new Error('Coordinates must numbers'); | ||
if (typeof coordinates[0] !== 'number' || typeof coordinates[1] !== 'number') throw new Error('Coordinates must contain numbers'); | ||
@@ -276,2 +276,15 @@ return feature({ | ||
var areaFactors = { | ||
kilometers: 0.000001, | ||
kilometres: 0.000001, | ||
meters: 1, | ||
metres: 1, | ||
centimetres: 10000, | ||
millimeter: 1000000, | ||
acres: 0.000247105, | ||
miles: 3.86e-7, | ||
yards: 1.195990046, | ||
feet: 10.763910417, | ||
inches: 1550.003100006 | ||
}; | ||
/** | ||
@@ -406,3 +419,23 @@ * Round number to precision | ||
/** | ||
* Converts a area to the requested unit. | ||
* Valid units: kilometers, kilometres, meters, metres, centimetres, millimeter, acre, mile, yard, foot, inch | ||
* @param {number} area to be converted | ||
* @param {string} [originalUnit=meters] of the distance | ||
* @param {string} [finalUnit=kilometers] returned unit | ||
* @returns {number} the converted distance | ||
*/ | ||
function convertArea(area, originalUnit, finalUnit) { | ||
if (area === null || area === undefined) throw new Error('area is required'); | ||
if (!(area >= 0)) throw new Error('area must be a positive number'); | ||
var startFactor = areaFactors[originalUnit || 'meters']; | ||
if (!startFactor) throw new Error('invalid original units'); | ||
var finalFactor = areaFactors[finalUnit || 'kilometers']; | ||
if (!finalFactor) throw new Error('invalid final units'); | ||
return (area / startFactor) * finalFactor; | ||
} | ||
module.exports = { | ||
@@ -425,3 +458,4 @@ feature: feature, | ||
convertDistance: convertDistance, | ||
convertArea: convertArea, | ||
round: round | ||
}; |
{ | ||
"name": "@turf/helpers", | ||
"version": "4.4.0", | ||
"version": "4.5.1", | ||
"description": "turf helpers module", | ||
@@ -29,3 +29,4 @@ "main": "index.js", | ||
"Stefano Borghi <@stebogit>", | ||
"Denis Carriere <@DenisCarriere>" | ||
"Denis Carriere <@DenisCarriere>", | ||
"William Nordmann <@wnordmann>" | ||
], | ||
@@ -32,0 +33,0 @@ "license": "MIT", |
@@ -329,2 +329,15 @@ # @turf/helpers | ||
# convertArea | ||
Converts a area to the requested unit. | ||
Valid units: kilometers, kilometres, meters, metres, centimetres, millimeter, acre, mile, yard, foot, inch | ||
**Parameters** | ||
- `area` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** to be converted | ||
- `originalUnit` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** of the distance (optional, default `meters`) | ||
- `finalUnit` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** returned unit (optional, default `kilometers`) | ||
Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the converted distance | ||
<!-- This file is automatically generated. Please don't edit it directly: | ||
@@ -331,0 +344,0 @@ if you find an error, edit the source file (likely index.js), and re-run |
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
36939
520
366