fbb178c35c173046eabe8bea01bbfb2e9bc387d1
[platform/framework/web/crosswalk-tizen.git] /
1 // Inspired by Underscore's groupBy:
2 // http://documentcloud.github.com/underscore/#groupBy
3
4 'use strict';
5
6 var callable = require('../../object/valid-callable')
7   , value    = require('../../object/valid-value')
8
9   , forEach = Array.prototype.forEach, apply = Function.prototype.apply;
10
11 module.exports = function (cb/*, thisArg*/) {
12         var r;
13
14         (value(this) && callable(cb));
15
16         r = {};
17         forEach.call(this, function (v) {
18                 var key = apply.call(cb, this, arguments);
19                 if (!r.hasOwnProperty(key)) r[key] = [];
20                 r[key].push(v);
21         }, arguments[1]);
22         return r;
23 };