One of those missing lodash functions. Includes TypeScript definitions and Flow definitions (they aren't prefect, there are some edge cases...)
import mapShape from 'map-shape'
mapShape(
{
foo: 1,
bar: '2',
baz: 'hello',
},
{
foo: (value, key, obj) => `${value} ${key} ${obj.baz}`,
bar: value => parseInt(value),
}
)
// outputs { foo: '1 foo hello', bar: 2 }
Each mapper function is called with three arguments:
value
- the value of the input propertykey
- the key of the input propertyobj
- the input object
If the input object is null
, returns null
. If the input object is undefined
, returns undefined
.
Works better with lodash/fp
. Only passes the value
to each mapper function.
import mapShape from 'map-shape/fp'
mapShape({
foo: String,
bar: parseInt,
})({
foo: 1,
bar: '2',
baz: 'hello',
})
// outputs { foo: '1', bar: 2 }