extendAll function

Map extendAll(
  1. Map target,
  2. Iterable<Map> sources
)

A Dart port of classic extend() method from jQuery with a little twist. It extends an existing Mutable target with multiple additional sources in a sequence.

Beware: the original target will be modified and will be returned

Example:

// Don't use `const` or `final`. It'll make the Map `Immutable`
var baseObj = {
  'dummy': 'x',
  'complex': {
    'subKey': 'subValue',
    'subComplex': {
      'deepKey': 'deepValue',
    },
    'subUndefined': null
  },
  'baseUndefined': null
};

final result = extendAll(baseObj,
[
  {
    'complex': {
      'subKey': 'subValueOther',
      'subComplex': {'deepNewKey': 'deepNewValue'}
    },
    'newKey': 'newValue'
  }
]);
print(result);
// beware, the original object is also changed
print(baseObj);
// in case, if you have one Immutable Map or don't want to change the
// original Map, try to extend an empty Map
final result = extendAll({},
[
  baseObj,
  {
    'complex': {
      'subKey': 'subValueOther',
      'subComplex': {'deepNewKey': 'deepNewValue'}
    },
    'newKey': 'newValue'
  }
]);

print(result);

Output:

{
  "dummy": "x",
  "complex": {
    "subKey": "subValueOther", // 👈 is extended
    "subComplex": {
      "deepKey": "deepValue", // 👈 remains unchanged
      "deepNewKey": "deepNewValue"  // 👈 is added
    },
    "subUndefined": null
  },
  "baseUndefined": null,
  "newKey": "newValue"  // 👈 is added
}

Implementation

Map extendAll(Map target, Iterable<Map> sources) =>
    target.extend(sources.first, sources.skip(1));