https://github.com/jcoreio/map-shape
apply mapping functions to specific properties of an object
https://github.com/jcoreio/map-shape
convert functional-programming map restructuring
Last synced: 7 months ago
JSON representation
apply mapping functions to specific properties of an object
- Host: GitHub
- URL: https://github.com/jcoreio/map-shape
- Owner: jcoreio
- License: mit
- Created: 2020-02-18T07:08:43.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T07:35:12.000Z (about 3 years ago)
- Last Synced: 2025-03-01T17:49:07.286Z (about 1 year ago)
- Topics: convert, functional-programming, map, restructuring
- Language: TypeScript
- Size: 3.3 MB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# map-shape
[](https://circleci.com/gh/jcoreio/map-shape)
[](https://codecov.io/gh/jcoreio/map-shape)
[](https://github.com/semantic-release/semantic-release)
[](http://commitizen.github.io/cz-cli/)
[](https://badge.fury.io/js/map-shape)
One of those missing lodash functions. Includes TypeScript definitions and Flow definitions
(they aren't prefect, there are some edge cases...)
```js
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 property
- `key` - the key of the input property
- `obj` - the input object
### Notes
If the input object is `null`, returns `null`. If the input object is `undefined`, returns `undefined`.
## FP Version
Works better with `lodash/fp`. Only passes the `value` to each mapper function.
```js
import mapShape from 'map-shape/fp'
mapShape({
foo: String,
bar: parseInt,
})({
foo: 1,
bar: '2',
baz: 'hello',
})
// outputs { foo: '1', bar: 2 }
```