index.cjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
  2. function clone(geojson) {
  3. if (!geojson) {
  4. throw new Error("geojson is required");
  5. }
  6. switch (geojson.type) {
  7. case "Feature":
  8. return cloneFeature(geojson);
  9. case "FeatureCollection":
  10. return cloneFeatureCollection(geojson);
  11. case "Point":
  12. case "LineString":
  13. case "Polygon":
  14. case "MultiPoint":
  15. case "MultiLineString":
  16. case "MultiPolygon":
  17. case "GeometryCollection":
  18. return cloneGeometry(geojson);
  19. default:
  20. throw new Error("unknown GeoJSON type");
  21. }
  22. }
  23. function cloneFeature(geojson) {
  24. const cloned = { type: "Feature" };
  25. Object.keys(geojson).forEach((key) => {
  26. switch (key) {
  27. case "type":
  28. case "properties":
  29. case "geometry":
  30. return;
  31. default:
  32. cloned[key] = geojson[key];
  33. }
  34. });
  35. cloned.properties = cloneProperties(geojson.properties);
  36. if (geojson.geometry == null) {
  37. cloned.geometry = null;
  38. } else {
  39. cloned.geometry = cloneGeometry(geojson.geometry);
  40. }
  41. return cloned;
  42. }
  43. function cloneProperties(properties) {
  44. const cloned = {};
  45. if (!properties) {
  46. return cloned;
  47. }
  48. Object.keys(properties).forEach((key) => {
  49. const value = properties[key];
  50. if (typeof value === "object") {
  51. if (value === null) {
  52. cloned[key] = null;
  53. } else if (Array.isArray(value)) {
  54. cloned[key] = value.map((item) => {
  55. return item;
  56. });
  57. } else {
  58. cloned[key] = cloneProperties(value);
  59. }
  60. } else {
  61. cloned[key] = value;
  62. }
  63. });
  64. return cloned;
  65. }
  66. function cloneFeatureCollection(geojson) {
  67. const cloned = { type: "FeatureCollection" };
  68. Object.keys(geojson).forEach((key) => {
  69. switch (key) {
  70. case "type":
  71. case "features":
  72. return;
  73. default:
  74. cloned[key] = geojson[key];
  75. }
  76. });
  77. cloned.features = geojson.features.map((feature) => {
  78. return cloneFeature(feature);
  79. });
  80. return cloned;
  81. }
  82. function cloneGeometry(geometry) {
  83. const geom = { type: geometry.type };
  84. if (geometry.bbox) {
  85. geom.bbox = geometry.bbox;
  86. }
  87. if (geometry.type === "GeometryCollection") {
  88. geom.geometries = geometry.geometries.map((g) => {
  89. return cloneGeometry(g);
  90. });
  91. return geom;
  92. }
  93. geom.coordinates = deepSlice(geometry.coordinates);
  94. return geom;
  95. }
  96. function deepSlice(coords) {
  97. const cloned = coords;
  98. if (typeof cloned[0] !== "object") {
  99. return cloned.slice();
  100. }
  101. return cloned.map((coord) => {
  102. return deepSlice(coord);
  103. });
  104. }
  105. var turf_clone_default = clone;
  106. exports.clone = clone; exports.cloneProperties = cloneProperties; exports.default = turf_clone_default;
  107. //# sourceMappingURL=index.cjs.map