Skip to content

Commit

Permalink
Merge branch 'master' into concave-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere authored Aug 17, 2017
2 parents bd2720a + 060f88f commit 97647e0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
17 changes: 7 additions & 10 deletions packages/turf-clone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,14 @@ function cloneProperties(properties) {
if (!properties) return cloned;
Object.keys(properties).forEach(function (key) {
var value = properties[key];
switch (typeof value) {
case 'number':
case 'string':
cloned[key] = value;
break;
case 'object':
// array
if (value.length) cloned[key] = value.map(function (item) { return item; });
// object
if (typeof value === 'object') {
// handle Array
if (value.length) cloned[key] = value.map(function (item) {
return item;
});
// handle Object
cloned[key] = cloneProperties(value);
}
} else cloned[key] = value;
});
return cloned;
}
Expand Down
25 changes: 24 additions & 1 deletion packages/turf-clone/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,48 @@ test('turf-clone -- Preserve Foreign Members -- FeatureCollection', t => {
test('turf-clone -- Preserve all properties -- Feature', t => {
const id = 12345;
const bbox = [0, 20, 0, 20];
const properties = {foo: 'bar', object: {property: 1}, array: [0, 1, 2]};
const properties = {
foo: 'bar',
object: {property: 1},
array: [0, 1, 2],
number: 1,
boolean: true
};
const pt = point([0, 20], properties, bbox, id);
pt.hello = 'world'; // Foreign member

// Clone and mutate
const cloned = clone(pt);

// Clone properly translated all properties
t.equal(cloned.hello, 'world');
t.equal(cloned.properties.foo, 'bar');
t.equal(cloned.id, 12345);
t.deepEqual(cloned.bbox, [0, 20, 0, 20]);
t.equal(cloned.properties.object.property, 1);
t.deepEqual(cloned.properties.array, [0, 1, 2]);
t.equal(cloned.properties.number, 1);
t.equal(cloned.properties.boolean, true);

// Mutate clone properties
cloned['hello'] = 'universe';
cloned.properties['foo'] = 'foo';
cloned['id'] = 54321;
cloned['bbox'] = [30, 40, 30, 40];
cloned.properties.object['property'] = 2;
cloned.properties.array[0] = 500;
cloned.properties.number = -99;
cloned.properties.boolean = false;

// Test if original point hasn't been mutated
t.equal(pt.hello, 'world');
t.equal(pt.properties.foo, 'bar');
t.equal(pt.id, 12345);
t.deepEqual(pt.bbox, [0, 20, 0, 20]);
t.equal(pt.properties.object.property, 1);
t.deepEqual(pt.properties.array, [0, 1, 2]);
t.equal(pt.properties.number, 1);
t.equal(pt.properties.boolean, true);
t.end();
});

Expand Down

0 comments on commit 97647e0

Please sign in to comment.