Skip to content

Commit

Permalink
Remove clone
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Jul 31, 2017
1 parent 36ca0d1 commit d684814
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/turf-clean-coords/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ glob.sync(path.join(__dirname, 'test', 'in', '*.geojson')).forEach(filepath => {
const {name} = path.parse(filepath);
const geojson = load.sync(filepath);
console.time(name);
cleanCoords(geojson, true);
cleanCoords(geojson);
console.timeEnd(name);
suite.add(name, () => cleanCoords(geojson, true));
suite.add(name, () => cleanCoords(geojson));
});

suite
Expand Down
48 changes: 40 additions & 8 deletions packages/turf-clean-coords/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var clone = require('@turf/clone');
var invariant = require('@turf/invariant');
var getCoords = invariant.getCoords;
var getGeomType = invariant.getGeomType;
Expand All @@ -8,7 +7,6 @@ var getGeomType = invariant.getGeomType;
*
* @name cleanCoords
* @param {Geometry|Feature} geojson Feature or Geometry
* @param {boolean} [mutate=false] allows GeoJSON input to be mutated
* @returns {Geometry|Feature} the cleaned input Feature/Geometry
* @example
* var line = turf.lineString([[0, 0], [0, 2], [0, 5], [0, 8], [0, 8], [0, 10]]);
Expand All @@ -20,7 +18,7 @@ var getGeomType = invariant.getGeomType;
* turf.cleanCoords(multiPoint).geometry.coordinates;
* //= [[0, 0], [2, 2]]
*/
module.exports = function (geojson, mutate) {
module.exports = function (geojson) {
if (!geojson) throw new Error('geojson is required');
var type = getGeomType(geojson);
var coords = getCoords(geojson);
Expand All @@ -30,7 +28,7 @@ module.exports = function (geojson, mutate) {

switch (type) {
case 'LineString':
newCoords = cleanCoords(geojson, mutate);
newCoords = cleanCoords(geojson);
break;
case 'MultiLineString':
case 'Polygon':
Expand Down Expand Up @@ -63,12 +61,46 @@ module.exports = function (geojson, mutate) {
throw new Error(type + ' geometry not supported');
}

if (mutate !== true) geojson = clone(geojson);
if (geojson.coordinates) geojson.coordinates = newCoords;
else geojson.geometry.coordinates = newCoords;
return geojson;
if (geojson.coordinates) return geometry(geojson, type, newCoords);
else return feature(geojson, type, newCoords);
};

/**
* Create Geometry from existing Geometry
*
* @param {Geometry} geojson Existing Geometry
* @param {string} type Geometry Type
* @param {Array<number>} coordinates Coordinates
* @returns {Geometry} Geometry
*/
function geometry(geojson, type, coordinates) {
var geom = {
type: type,
coordinates: coordinates
};
if (geojson.bbox) geom.bbox = geojson.bbox;
return geom;
}

/**
* Create Feature from existing Feature
*
* @param {Feature} geojson Existing Feature
* @param {string} type Feature Type
* @param {Array<number>} coordinates Coordinates
* @returns {Feature} Feature
*/
function feature(geojson, type, coordinates) {
var feat = {
type: 'Feature',
properties: geojson.properties || {},
geometry: geometry(geojson.geometry, type, coordinates)
};
if (geojson.id) feat.id = geojson.id;
if (geojson.bbox) feat.bbox = geojson.bbox;
return feat;
}

/**
* Clean Coords
*
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-clean-coords/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"homepage": "https://github.com/Turfjs/turf",
"devDependencies": {
"@turf/helpers": "4.5.2",
"@turf/helpers": "^4.5.2",
"@turf/truncate": "^4.5.2",
"benchmark": "^2.1.4",
"load-json-file": "^2.0.0",
Expand Down
6 changes: 2 additions & 4 deletions packages/turf-clean-coords/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ test('turf-clean-coords -- throws', t => {

test('turf-clean-coords -- prevent input mutation', t => {
const line = lineString([[0, 0], [1, 1], [2, 2]], {foo: 'bar'});
cleanCoords(line);
const lineBefore = JSON.parse(JSON.stringify(line));

cleanCoords(line);
t.deepEqual(lineBefore, line, 'line should NOT be mutated');

const multiPoly = multiPolygon([
Expand All @@ -61,8 +62,5 @@ test('turf-clean-coords -- prevent input mutation', t => {
const multiPolyBefore = JSON.parse(JSON.stringify(multiPoly));
cleanCoords(multiPoly);
t.deepEqual(multiPolyBefore, multiPoly, 'multiPolygon should NOT be mutated');

const cleanLine = cleanCoords(line, true);
t.deepEqual(cleanLine, line, 'line should be mutated');
t.end();
});
2 changes: 1 addition & 1 deletion packages/turf-clean-coords/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
version "4.5.2"
resolved "https://registry.yarnpkg.com/@turf/clone/-/clone-4.5.2.tgz#397d3b173beb0c67137a6ecd8c232a24c7561316"

"@turf/helpers@4.5.2":
"@turf/helpers@^4.5.2":
version "4.5.2"
resolved "https://registry.yarnpkg.com/@turf/helpers/-/helpers-4.5.2.tgz#6fc6772a7b301f277b49732925c354c7fb85d1df"

Expand Down

0 comments on commit d684814

Please sign in to comment.