index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // index.ts
  2. import { coordEach } from "@turf/meta";
  3. import { isObject } from "@turf/helpers";
  4. function truncate(geojson, options) {
  5. options = options != null ? options : {};
  6. if (!isObject(options)) throw new Error("options is invalid");
  7. var precision = options.precision;
  8. var coordinates = options.coordinates;
  9. var mutate = options.mutate;
  10. precision = precision === void 0 || precision === null || isNaN(precision) ? 6 : precision;
  11. coordinates = coordinates === void 0 || coordinates === null || isNaN(coordinates) ? 3 : coordinates;
  12. if (!geojson) throw new Error("<geojson> is required");
  13. if (typeof precision !== "number")
  14. throw new Error("<precision> must be a number");
  15. if (typeof coordinates !== "number")
  16. throw new Error("<coordinates> must be a number");
  17. if (mutate === false || mutate === void 0)
  18. geojson = JSON.parse(JSON.stringify(geojson));
  19. var factor = Math.pow(10, precision);
  20. coordEach(geojson, function(coords) {
  21. truncateCoords(coords, factor, coordinates);
  22. });
  23. return geojson;
  24. }
  25. function truncateCoords(coords, factor, coordinates) {
  26. if (coords.length > coordinates) coords.splice(coordinates, coords.length);
  27. for (var i = 0; i < coords.length; i++) {
  28. coords[i] = Math.round(coords[i] * factor) / factor;
  29. }
  30. return coords;
  31. }
  32. var turf_truncate_default = truncate;
  33. export {
  34. turf_truncate_default as default,
  35. truncate
  36. };
  37. //# sourceMappingURL=index.js.map