mapEntries

Transforms own properties of an object using a mapping function.

Signature

mapEntries(object, transform, define)
(
  object    : Object 'a,
  transform : ((String, 'a)) => (String, 'b),
  define    : (('x : Object 'b), String, 'b) => Object 'b :: mutates 'x
) => Object 'b

Documentation

Transforms own properties of an object using a mapping function.

The transformation takes a [key, value] pair, and is expected to return a new [key, value] pair. The resulting object has not only its values transformed, but also its keys.

Example:

const mapEntries = require('folktale/core/object/map-entries');

const pair = { x: 10, y: 20 };
mapEntries(
  pair,
  ([key, value]) => [key.toUpperCase(), value * 2],
  (result, key, value) => {
    result[key] = value;
    return result;
  }
);
// ==> { X: 20, Y: 40 }

Handling collisions

Since the mapping function returns a [key, value] pair, it's possible that some of the returned keys collide with another. Since there's no single answer that is correct for all cases when handling these collisions, mapEntries expects an additional function that's used to define the properties in the resulting object, and this function is expected to deal with the collisions.

A definition function takes the result object, a property name, and a value, and is expected to return a new object containing the provided key/value pair, if it can be attached to the result object. This function may mutate the object, but pure functions are also supported.

Specialised forms of this function exist to cover common cases. mapEntries.overwrite will have later key/value pairs overwrite earlier ones with the same key, while mapEntries.unique will throw whenever a collision happens.

Caveats

mapEntries will not preserve the shape of the original object. It treats objects as plain maps from String to some value. It ignores things like prototypical delegation, symbols, and non-enumerable properties.

Properties

Convenience

overwrite(object, transform)

Transforms own properties of an object using a mapping function.

unique(object, transform)

Transforms own properties of an object using a mapping function.

Source Code

Defined in source/core/object/map-entries.js at line 25, column 0
(object, transform, define) =>
        Object.keys(object).reduce((result, key) => {
          const [newKey, newValue] = transform([key, object[key]]);
          return define(result, newKey, newValue);
        }, {})
Stability
stable
Licence
MIT
Module
folktale/core/object/map-entries
Authors
Copyright
(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS
Authors
  • Quildreen Motta
Maintainers
  • Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)