MAP::EXTEND 1g 2023-11-19 laplante@plcb.ca GOWEB/MAP


title: “MAP::EXTEND Function” author: “Pierre Laplante laplante@plcb.ca” date: “2023-11-19” version: “1.0.0”

category: “GOWEB/MAP”

Name

map::extend merges two or more maps and returns a new map.

Synopsis

map::extend or extend(map[, map..., deep: bool])

Description

The map::extend function merges two or more maps and returns a new map.
If the parameter deep is set to true, the merge is recursive, meaning nested maps will also be merged instead of replaced.
If deep is not specified, only the top-level keys will be merged.

Examples

Example 1: Deep merge with multiple maps

res={{
    option := {"selection": "#main_page"};
    option2 := {"data": {"uuid": "1234-2345-rert"} };
    option3 := {"data": {"uids": ["123", "234", "345"]} };
    option4 := {"data": {"targets": [{"primary": "test"}, {"primary": "test2"}]} };
    option = extend(option, option2, option3, option4, deep: true);
    option;
}}.

return

res={"data":{"targets":[{"primary":"test"},{"primary":"test2"}],"uids":["123","234","345"],"uuid":"1234-2345-rert"},"selection":"#main_page"}.

Example 2: Shallow merge with additional properties

res={{
    option := {"selection": "#main_page"};
    option2 := {"data": {"uuid": "1234-2345-rert"}, "target": "blank"};
    option3 := {"data": {"page": 1} };
    option = extend(option, option2, option3);
    option;
}}.

return

res={"data":{"page":1},"selection":"#main_page","target":"blank"}.

Example 3: Deep vs shallow merge

res={{
    option := {"data": {"uuid": "1234-2345-rert"} };
    option2 := {"data": {"page": "1"} };
    option = extend(option, option2, deep: true);
    option;
}}.

return

res={"data":{"page":"1","uuid":"1234-2345-rert"}}.
res={{
    option := {"data": {"uuid": "1234-2345-rert"} };
    option2 := {"data": {"page": "1"} };
    option = extend(option, option2);
    option;
}}.

return

res={"data":{"page":"1"}}.

Example 4: Overwriting keys

res={{
    option := {"selection": "#main_page"};
    option2 := {"selection": "#sub_page", "data": {"uuid": "1234-2345-rert"} };
    option = extend(option, option2);
    option;
}}.

return

res={"data":{"uuid":"1234-2345-rert"},"selection":"#sub_page"}.

Example 5: Merge preserving original data

res={{
    option := {"selection": "#main_page"};
    option2 := {
        "data": {
            "uuid": "1234-2345-rert"
        }
    };
    option = extend(option, option2);
    option;
}}.

return

res={"data":{"uuid":"1234-2345-rert"},"selection":"#main_page"}.

Example 6: Basic overwriting behavior

res={{
    option := {"selection": "#main_page"};
    option2 := {"selection": "#sub_page"};
    option = extend(option, option2);
    option;
}}.

return

res={"selection":"#sub_page"}.

Example 7: Minimal merge cases

res={{
    extend({}, { "x" : 1 });
    extend({"x" : true}, { "x" : 1 });
    a := extend({"x" : true}, { "y" : 1 });
    a;
}}.

return

res={"x":1}{"x":1}{"x":true,"y":1}.

Author

Pierre Laplante laplante@plcb.ca

See also

Version