shannonmoeller-archive / mtil

Supporting vanilla JavaScript functionality since 2014. **Unmaintained**
1 stars 0 forks source link

array/plucks #1

Open nickstark opened 10 years ago

nickstark commented 10 years ago

Pluck functionality

var customers = [ ... ];
var names = customers.map(pluck('name'));
nickstark commented 10 years ago

The naming is a little confusing since it actually returns a function. Usually pluck methods perform the map as well.

shannonmoeller commented 10 years ago

This is a tough call because it reads so nicely. Agreed though. We should probably add a suffix such as s or er to indicate that the util is a maker/factory.

var names = customers.map(plucks('name'));
nickstark commented 10 years ago

I like the pluralizing "plucks". It still reads well.

Another option to be a "to" prefix so it reads like "map to".

var names = customers.map(toProp('name'));
var salary = customer.filter(toProp('fullTime'));
shannonmoeller commented 10 years ago

Let's see how well plural and "to" play out for other utils.

nickstark commented 10 years ago

Multiple args for deep plucks?

// gets customer.addresses.home.state
var states = customers.map(plucks('addresses', 'home', 'state')).filter(unique());
shannonmoeller commented 10 years ago

:+1:

I was thinking that plucks might return an object with multiple keys based on multiple args, but that should probably be a different util.

nickstark commented 10 years ago

I was thinking dot notation like customer.addresses but that's a valid key for an object. I've used json data that has dots in the keys before. For multiple plucks we could use additional args and use array syntax for deep plucks, since arrays are not valid.

var publicInfo = customers.map(plucks('name', 'email', ['address', 'state']));

Might get a little nasty though. A separate method might be justified.

shannonmoeller commented 10 years ago

Agreed about dot-notation. I thought about arrays too, but yeah, getting nasty.

shannonmoeller commented 10 years ago

Added picks in #6.