@turf/helpers 4.7.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/README.md +76 -55
  2. package/index.d.ts +52 -45
  3. package/index.js +45 -46
  4. package/main.js +567 -0
  5. package/package.json +20 -7
package/index.js CHANGED
@@ -17,7 +17,7 @@
17
17
  *
18
18
  * //=feature
19
19
  */
20
- function feature(geometry, properties, bbox, id) {
20
+ export function feature(geometry, properties, bbox, id) {
21
21
  if (geometry === undefined) throw new Error('geometry is required');
22
22
  if (properties && properties.constructor !== Object) throw new Error('properties must be an Object');
23
23
  if (bbox && bbox.length !== 4) throw new Error('bbox must be an Array of 4 numbers');
@@ -48,7 +48,7 @@ function feature(geometry, properties, bbox, id) {
48
48
  *
49
49
  * //=geometry
50
50
  */
51
- function geometry(type, coordinates, bbox) {
51
+ export function geometry(type, coordinates, bbox) {
52
52
  // Validation
53
53
  if (!type) throw new Error('type is required');
54
54
  if (!coordinates) throw new Error('coordinates is required');
@@ -83,7 +83,7 @@ function geometry(type, coordinates, bbox) {
83
83
  *
84
84
  * //=point
85
85
  */
86
- function point(coordinates, properties, bbox, id) {
86
+ export function point(coordinates, properties, bbox, id) {
87
87
  if (!coordinates) throw new Error('No coordinates passed');
88
88
  if (coordinates.length === undefined) throw new Error('Coordinates must be an array');
89
89
  if (coordinates.length < 2) throw new Error('Coordinates must be at least 2 numbers long');
@@ -117,7 +117,7 @@ function point(coordinates, properties, bbox, id) {
117
117
  *
118
118
  * //=polygon
119
119
  */
120
- function polygon(coordinates, properties, bbox, id) {
120
+ export function polygon(coordinates, properties, bbox, id) {
121
121
  if (!coordinates) throw new Error('No coordinates passed');
122
122
 
123
123
  for (var i = 0; i < coordinates.length; i++) {
@@ -169,7 +169,7 @@ function polygon(coordinates, properties, bbox, id) {
169
169
  *
170
170
  * //=linestring2
171
171
  */
172
- function lineString(coordinates, properties, bbox, id) {
172
+ export function lineString(coordinates, properties, bbox, id) {
173
173
  if (!coordinates) throw new Error('No coordinates passed');
174
174
  if (coordinates.length < 2) throw new Error('Coordinates must be an array of two or more positions');
175
175
  // Check if first point of LineString contains two numbers
@@ -200,7 +200,7 @@ function lineString(coordinates, properties, bbox, id) {
200
200
  *
201
201
  * //=collection
202
202
  */
203
- function featureCollection(features, bbox, id) {
203
+ export function featureCollection(features, bbox, id) {
204
204
  if (!features) throw new Error('No features passed');
205
205
  if (!Array.isArray(features)) throw new Error('features must be an Array');
206
206
  if (bbox && bbox.length !== 4) throw new Error('bbox must be an Array of 4 numbers');
@@ -229,7 +229,7 @@ function featureCollection(features, bbox, id) {
229
229
  *
230
230
  * //=multiLine
231
231
  */
232
- function multiLineString(coordinates, properties, bbox, id) {
232
+ export function multiLineString(coordinates, properties, bbox, id) {
233
233
  if (!coordinates) throw new Error('No coordinates passed');
234
234
 
235
235
  return feature({
@@ -254,7 +254,7 @@ function multiLineString(coordinates, properties, bbox, id) {
254
254
  *
255
255
  * //=multiPt
256
256
  */
257
- function multiPoint(coordinates, properties, bbox, id) {
257
+ export function multiPoint(coordinates, properties, bbox, id) {
258
258
  if (!coordinates) throw new Error('No coordinates passed');
259
259
 
260
260
  return feature({
@@ -280,7 +280,7 @@ function multiPoint(coordinates, properties, bbox, id) {
280
280
  * //=multiPoly
281
281
  *
282
282
  */
283
- function multiPolygon(coordinates, properties, bbox, id) {
283
+ export function multiPolygon(coordinates, properties, bbox, id) {
284
284
  if (!coordinates) throw new Error('No coordinates passed');
285
285
 
286
286
  return feature({
@@ -312,7 +312,7 @@ function multiPolygon(coordinates, properties, bbox, id) {
312
312
  *
313
313
  * //=collection
314
314
  */
315
- function geometryCollection(geometries, properties, bbox, id) {
315
+ export function geometryCollection(geometries, properties, bbox, id) {
316
316
  if (!geometries) throw new Error('geometries is required');
317
317
  if (!Array.isArray(geometries)) throw new Error('geometries must be an Array');
318
318
 
@@ -352,6 +352,7 @@ var areaFactors = {
352
352
  feet: 10.763910417,
353
353
  inches: 1550.003100006
354
354
  };
355
+
355
356
  /**
356
357
  * Round number to precision
357
358
  *
@@ -365,7 +366,7 @@ var areaFactors = {
365
366
  * turf.round(120.4321, 2)
366
367
  * //=120.43
367
368
  */
368
- function round(num, precision) {
369
+ export function round(num, precision) {
369
370
  if (num === undefined || num === null || isNaN(num)) throw new Error('num is required');
370
371
  if (precision && !(precision >= 0)) throw new Error('precision must be a positive number');
371
372
  var multiplier = Math.pow(10, precision || 0);
@@ -378,14 +379,15 @@ function round(num, precision) {
378
379
  *
379
380
  * @name radiansToDistance
380
381
  * @param {number} radians in radians across the sphere
381
- * @param {string} [units=kilometers] can be degrees, radians, miles, or kilometers inches, yards, metres, meters, kilometres, kilometers.
382
+ * @param {string} [units="kilometers"] can be degrees, radians, miles, or kilometers inches, yards, metres, meters, kilometres, kilometers.
382
383
  * @returns {number} distance
383
384
  */
384
- function radiansToDistance(radians, units) {
385
+ export function radiansToDistance(radians, units) {
385
386
  if (radians === undefined || radians === null) throw new Error('radians is required');
386
387
 
388
+ if (units && typeof units !== 'string') throw new Error('units must be a string');
387
389
  var factor = factors[units || 'kilometers'];
388
- if (!factor) throw new Error('units is invalid');
390
+ if (!factor) throw new Error(units + ' units is invalid');
389
391
  return radians * factor;
390
392
  }
391
393
 
@@ -398,11 +400,12 @@ function radiansToDistance(radians, units) {
398
400
  * @param {string} [units=kilometers] can be degrees, radians, miles, or kilometers inches, yards, metres, meters, kilometres, kilometers.
399
401
  * @returns {number} radians
400
402
  */
401
- function distanceToRadians(distance, units) {
403
+ export function distanceToRadians(distance, units) {
402
404
  if (distance === undefined || distance === null) throw new Error('distance is required');
403
405
 
406
+ if (units && typeof units !== 'string') throw new Error('units must be a string');
404
407
  var factor = factors[units || 'kilometers'];
405
- if (!factor) throw new Error('units is invalid');
408
+ if (!factor) throw new Error(units + ' units is invalid');
406
409
  return distance / factor;
407
410
  }
408
411
 
@@ -415,7 +418,7 @@ function distanceToRadians(distance, units) {
415
418
  * @param {string} [units=kilometers] can be degrees, radians, miles, or kilometers inches, yards, metres, meters, kilometres, kilometers.
416
419
  * @returns {number} degrees
417
420
  */
418
- function distanceToDegrees(distance, units) {
421
+ export function distanceToDegrees(distance, units) {
419
422
  return radians2degrees(distanceToRadians(distance, units));
420
423
  }
421
424
 
@@ -427,7 +430,7 @@ function distanceToDegrees(distance, units) {
427
430
  * @param {number} bearing angle, between -180 and +180 degrees
428
431
  * @returns {number} angle between 0 and 360 degrees
429
432
  */
430
- function bearingToAngle(bearing) {
433
+ export function bearingToAngle(bearing) {
431
434
  if (bearing === null || bearing === undefined) throw new Error('bearing is required');
432
435
 
433
436
  var angle = bearing % 360;
@@ -442,7 +445,7 @@ function bearingToAngle(bearing) {
442
445
  * @param {number} radians angle in radians
443
446
  * @returns {number} degrees between 0 and 360 degrees
444
447
  */
445
- function radians2degrees(radians) {
448
+ export function radians2degrees(radians) {
446
449
  if (radians === null || radians === undefined) throw new Error('radians is required');
447
450
 
448
451
  var degrees = radians % (2 * Math.PI);
@@ -456,14 +459,13 @@ function radians2degrees(radians) {
456
459
  * @param {number} degrees angle between 0 and 360 degrees
457
460
  * @returns {number} angle in radians
458
461
  */
459
- function degrees2radians(degrees) {
462
+ export function degrees2radians(degrees) {
460
463
  if (degrees === null || degrees === undefined) throw new Error('degrees is required');
461
464
 
462
465
  var radians = degrees % 360;
463
466
  return radians * Math.PI / 180;
464
467
  }
465
468
 
466
-
467
469
  /**
468
470
  * Converts a distance to the requested unit.
469
471
  * Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
@@ -473,7 +475,7 @@ function degrees2radians(degrees) {
473
475
  * @param {string} [finalUnit=kilometers] returned unit
474
476
  * @returns {number} the converted distance
475
477
  */
476
- function convertDistance(distance, originalUnit, finalUnit) {
478
+ export function convertDistance(distance, originalUnit, finalUnit) {
477
479
  if (distance === null || distance === undefined) throw new Error('distance is required');
478
480
  if (!(distance >= 0)) throw new Error('distance must be a positive number');
479
481
 
@@ -489,7 +491,7 @@ function convertDistance(distance, originalUnit, finalUnit) {
489
491
  * @param {string} [finalUnit=kilometers] returned unit
490
492
  * @returns {number} the converted distance
491
493
  */
492
- function convertArea(area, originalUnit, finalUnit) {
494
+ export function convertArea(area, originalUnit, finalUnit) {
493
495
  if (area === null || area === undefined) throw new Error('area is required');
494
496
  if (!(area >= 0)) throw new Error('area must be a positive number');
495
497
 
@@ -513,29 +515,26 @@ function convertArea(area, originalUnit, finalUnit) {
513
515
  * turf.isNumber('foo')
514
516
  * //=false
515
517
  */
516
- function isNumber(num) {
518
+ export function isNumber(num) {
517
519
  return !isNaN(num) && num !== null && !Array.isArray(num);
518
520
  }
519
521
 
520
- module.exports = {
521
- feature: feature,
522
- geometry: geometry,
523
- featureCollection: featureCollection,
524
- geometryCollection: geometryCollection,
525
- point: point,
526
- multiPoint: multiPoint,
527
- lineString: lineString,
528
- multiLineString: multiLineString,
529
- polygon: polygon,
530
- multiPolygon: multiPolygon,
531
- radiansToDistance: radiansToDistance,
532
- distanceToRadians: distanceToRadians,
533
- distanceToDegrees: distanceToDegrees,
534
- radians2degrees: radians2degrees,
535
- degrees2radians: degrees2radians,
536
- bearingToAngle: bearingToAngle,
537
- convertDistance: convertDistance,
538
- convertArea: convertArea,
539
- round: round,
540
- isNumber: isNumber
541
- };
522
+ /**
523
+ * isObject
524
+ *
525
+ * @param {*} input variable to validate
526
+ * @returns {boolean} true/false
527
+ * @example
528
+ * turf.isObject({elevation: 10})
529
+ * //=true
530
+ * turf.isObject('foo')
531
+ * //=false
532
+ */
533
+ export function isObject(input) {
534
+ return (!!input) && (input.constructor === Object);
535
+ }
536
+
537
+ /**
538
+ * Earth Radius used with the Harvesine formula and approximates using a spherical (non-ellipsoid) Earth.
539
+ */
540
+ export var earthRadius = 6371008.8;