documentcloud / documentcloud/underscore-contrib

Extend _.groupBy to support multi-key objects

Open
#216 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

after modules enhancement
Dominant language
JavaScript
Stars
622
Forks
114
PR merge metrics
No merged PRs in 30d

Description

The current groupBy function only adds each Collection object to the returned hash only once.
In some case one might want to add the same object in multiple groups.

A concrete example is an object having an array property.

Basically this means replacing:

.groupBy = group(function(result, value, key) {
if (
.has(result, key)) result[key].push(value); else result[key] = [value];
});

by

function addToGroup(result, value, key) {
if _.isArray(key) { .each(key, function(k) { addToGroup(result, value, k); }); return; }
if (
.has(result, key)) result[key].push(value); else result[key] = [value];
});

_.groupBy = group(addToGroup);

I implemented this for my backbone collection as groupByMulti but that is rather cumbersome as I don't have access to the underscore internals, in particular the group function.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with _.groupBy and the internal group helper described in the issue, then review the existing collection utility behavior. Verify that a collection object returned with multiple keys is included in every corresponding group while single-key behavior remains unchanged; the payload does not name a test file or command.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
data
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.