Anahkiasen / underscore-php

A redacted PHP port of Underscore.js with additional functions and goodies – Available for Composer and Laravel
http://anahkiasen.github.com/underscore-php/
1.12k stars 88 forks source link

Implement indexBy() function on Types\Arrays #81

Closed tamakiii closed 8 years ago

tamakiii commented 8 years ago

rel: https://github.com/Anahkiasen/underscore-php/issues/33

I would like to implement Types\Arrays::indexBy() that is implemented in underscore.js. It's _.indexBy() work like this:

> var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
undefined
> u.indexBy(stooges, 'age');
{ '40': { name: 'moe', age: 40 },
  '50': { name: 'larry', age: 50 },
  '60': { name: 'curly', age: 60 } }
>
> u.indexBy(stooges, 'hoge');
{ undefined: { name: 'curly', age: 60 } }

In case of non-existing key is specified, underscore.js's _.indexBy() return last element with key undefined (this might not be intentionally).

But it's better not to include in result in PHP. So,

$ php -r 'print_r(Arrays::indexOf(..., "hoge");'
Array
(
)
tamakiii commented 8 years ago

hmmm...

tamakiii commented 8 years ago

@Anahkiasen Please merge this.

tamakiii commented 8 years ago

@Anahkiasen Thank you! :)