Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Foreign Members to @turf/clone #904

Merged
merged 3 commits into from
Aug 17, 2017
Merged

Conversation

DenisCarriere
Copy link
Member

@DenisCarriere DenisCarriere commented Aug 15, 2017

Slightly outside the scope of GeoJSON, Foreign members are members which is not described by the GeoJSON specification (https://tools.ietf.org/html/rfc7946#section-6.1).

Tasks

  • Dropped cloneAll since the default does the same thing as JSON.parse/stringify
  • Preserve Foreign members
  • Preserve id & bbox
  • Preserve Properties (string/number/object/array)

Before

Using @turf/clone would remove all foreign members by default, unless you performed the JSON.parse + JSON.stringify combo which is significantly slower.

const clone = require('@turf/clone');
const point = {
    type: 'Feature',
    title: 'Custom Title',
    properties: {},
    geometry: {
        type: 'Point',
        coordinates: [10, 20]
    }
};
clone(point).title;
//= undefined

Now

By default all members are preserved including id & bbox.

clone(point).title;
//= 'Custom Title'

if (geojson.bbox) cloned.bbox = geojson.bbox;

// Custom Properties
Object.keys(geojson).forEach(function (key) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DenisCarriere doesn't this in practice replace the cloneAll feature?

Copy link
Member Author

@DenisCarriere DenisCarriere Aug 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe.. but I don't think so?... pretty sure cloneAll feature clones all property values (JSON.parse + JSON.stringify), so if you need to mutate some of the properties then it might be best to use that option.

However.. we could test it out and maybe it can replace the cloneAll feature entirely.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cloneAll is primarily used to clone properties, so we could test for that.

Copy link
Member Author

@DenisCarriere DenisCarriere Aug 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to preserve the properties using the default setting, so I've dropped that the cloneAll param. Doesn't really have any backwards compatibility effects since it behaves the same way if true or false.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DenisCarriere what if the user wants still to clone only the GeoJSON object (no foreign members) taking advantage of a higher speed? I'd keep an optional parameter here, like geojsonOnly or something, to allow that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 ... it's really not a big speed difference speed.

The bigger difference is mutate=true to not even do this clone operation at all!

The new "foreign member" support acts also to support id & bbox which is apart of the GeoJSON spec, so we shouldn't make that optional, we should clone those.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then now this is just a faster version of JSON.parse(JSON.stringify(thing))! 😜
And it makes sense to expect that the whole thing is going to be cloned.
Good job! 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! It's a faster JSON.parse(JSON.stringify(geojson)) tool, also it looks "cleaner" by just wrapping it with clone(geojson).

It's about 2-3x faster... depends on the type of Geometry.

// Custom Properties
Object.keys(geojson).forEach(function (key) {
if (['id', 'type', 'bbox', 'features'].indexOf(key) !== -1) return;
cloned[key] = geojson[key];
Copy link
Collaborator

@stebogit stebogit Aug 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DenisCarriere does this guarantee you can modify the properties of the cloned object without changing the original ones? Just want to make sure this issue doesn't happen again 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't have any impact, before these properties were being dropped (so it's better than before).

We can add some tests for this behavior.

It shouldn't have any impact on the issue you mentioned above.

Copy link
Collaborator

@stebogit stebogit Aug 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, no impact on that specific issue; I just used that as an example of creating a reference to the item instead of cloning it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, we just need to test if those properties get mutated or not.

@DenisCarriere
Copy link
Member Author

DenisCarriere commented Aug 17, 2017

@stebogit Did some significant changes to prevent properties from being mutated, added test cases for every scenario I can think of. @stebogit Let me know what you think about this now.

@DenisCarriere
Copy link
Member Author

What's been done so far:

  • Dropped cloneAll since the default does the same thing as JSON.parse/stringify
  • Preserve Foreign members
  • Preserve id & bbox
  • Preserve Properties (string/number/object/array)

@DenisCarriere
Copy link
Member Author

Going to merge, if we find any other issues, I'll open up a new PR with tests/solution.

@DenisCarriere DenisCarriere merged commit 188ad47 into master Aug 17, 2017
@DenisCarriere DenisCarriere deleted the clone-foreign-members branch August 17, 2017 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants