Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

apply mapping functions to specific properties of an object

License

Notifications You must be signed in to change notification settings

jcoreio/map-shape

Repository files navigation

map-shape

CircleCI Coverage Status semantic-release Commitizen friendly npm version

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 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.

import mapShape from 'map-shape/fp'
mapShape({
 foo: String,
 bar: parseInt,
})({
 foo: 1,
 bar: '2',
 baz: 'hello',
})
// outputs { foo: '1', bar: 2 }

About

apply mapping functions to specific properties of an object

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

AltStyle によって変換されたページ (->オリジナル) /