Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: about 2 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 (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T07:35:12.000Z (about 2 years ago)
- Last Synced: 2024-11-11T12:36:13.742Z (3 months 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
[![CircleCI](https://circleci.com/gh/jcoreio/map-shape.svg?style=svg)](https://circleci.com/gh/jcoreio/map-shape)
[![Coverage Status](https://codecov.io/gh/jcoreio/map-shape/branch/master/graph/badge.svg)](https://codecov.io/gh/jcoreio/map-shape)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
[![npm version](https://badge.fury.io/js/map-shape.svg)](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 }
```