A small utility for mapping properties in JavaScript
import { prop as p, fn } from "@spenserblack/that";
// Mapping to properties
[foo, bar, baz].map(p("foo")); // Same as .map((item) => item.prop) or .map(({ prop }) => prop)
[1, 2, 3].map(fn("toString", 16)); // Same as .map((n) => n.toString(16))
- When doing these types of simple mapping, thinking of the item's parameter name is unnecessary mental overhead
- I wanted to play around with TypeScript
- Laziness (
=
and>
are so annoying to type!)
This Ruby syntax:
[1, 2, 3].map(&:to_s) # equivalent to [1, 2, 3].map((n) => n.toString()) in JavaScript