Skip to content

[@turf/clone] - object with key "length" is interpreted as array due to value.length in js code #1621

@udos

Description

@udos

I have a GeoJSON feature with a key "length":

myGeoJSON = {
    geometry: [...],
    bbox: [...],
    properties: {
        processed: {
            length: {
                orig: 123.456,
                opti: 100.00,
            },
        }
    }
};

at some point, clone is called, which interprets the processed object as an array due to line

} else if (value.length) {

in

/**
 * Clone Properties
 *
 * @private
 * @param {Object} properties GeoJSON Properties
 * @returns {Object} cloned Properties
 */
function cloneProperties(properties) {
    var cloned = {};
    if (!properties) return cloned;
    Object.keys(properties).forEach(function (key) {
        var value = properties[key];
        if (typeof value === 'object') {
            if (value === null) {
                // handle null
                cloned[key] = null;
            } else if (value.length) {
                // handle Array
                cloned[key] = value.map(function (item) {
                    return item;
                });
            } else {
                // handle generic Object
                cloned[key] = cloneProperties(value);
            }
        } else cloned[key] = value;
    });
    return cloned;
}

and throws an error...

looking at the TypeScript code, it looks as this is handled properly (source: https://github.com/Turfjs/turf/blob/master/packages/turf-clone/index.ts#L78):

} else if (Array.isArray(value)) {

there might be a reason for this, but it would be nice if JS array identification would be done in a more robust way.

p.s.: meanwhile I change my key :|

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions