Skip to content

Refactor out AccumulatorMap from groupBy #3568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/execution/collectFields.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AccumulatorMap } from '../jsutils/AccumulatorMap';
import type { ObjMap } from '../jsutils/ObjMap';

import type {
Expand Down Expand Up @@ -37,7 +38,7 @@ export function collectFields(
runtimeType: GraphQLObjectType,
selectionSet: SelectionSetNode,
): Map<string, ReadonlyArray<FieldNode>> {
const fields = new Map();
const fields = new AccumulatorMap<string, FieldNode>();
collectFieldsImpl(
schema,
fragments,
Expand Down Expand Up @@ -67,7 +68,7 @@ export function collectSubfields(
returnType: GraphQLObjectType,
fieldNodes: ReadonlyArray<FieldNode>,
): Map<string, ReadonlyArray<FieldNode>> {
const subFieldNodes = new Map();
const subFieldNodes = new AccumulatorMap<string, FieldNode>();
const visitedFragmentNames = new Set<string>();
for (const node of fieldNodes) {
if (node.selectionSet) {
Expand All @@ -91,7 +92,7 @@ function collectFieldsImpl(
variableValues: { [variable: string]: unknown },
runtimeType: GraphQLObjectType,
selectionSet: SelectionSetNode,
fields: Map<string, Array<FieldNode>>,
fields: AccumulatorMap<string, FieldNode>,
visitedFragmentNames: Set<string>,
): void {
for (const selection of selectionSet.selections) {
Expand All @@ -100,13 +101,7 @@ function collectFieldsImpl(
if (!shouldIncludeNode(variableValues, selection)) {
continue;
}
const name = getFieldEntryKey(selection);
const fieldList = fields.get(name);
if (fieldList !== undefined) {
fieldList.push(selection);
} else {
fields.set(name, [selection]);
}
fields.add(getFieldEntryKey(selection), selection);
break;
}
case Kind.INLINE_FRAGMENT: {
Expand Down
17 changes: 17 additions & 0 deletions src/jsutils/AccumulatorMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* ES6 Map with additional `add` method to accumulate items.
*/
export class AccumulatorMap<K, T> extends Map<K, Array<T>> {
get [Symbol.toStringTag]() {
return 'AccumulatorMap';
}

add(key: K, item: T): void {
const group = this.get(key);
if (group === undefined) {
this.set(key, [item]);
} else {
group.push(item);
}
}
}
36 changes: 36 additions & 0 deletions src/jsutils/__tests__/AccumulatorMap-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { AccumulatorMap } from '../AccumulatorMap';

function expectMap<K, V>(map: Map<K, V>) {
return expect(Object.fromEntries(map.entries()));
}

describe('AccumulatorMap', () => {
it('can be Object.toStringified', () => {
const accumulatorMap = new AccumulatorMap();

expect(Object.prototype.toString.call(accumulatorMap)).to.equal(
'[object AccumulatorMap]',
);
});

it('accumulate items', () => {
const accumulatorMap = new AccumulatorMap<string, number>();

expectMap(accumulatorMap).to.deep.equal({});

accumulatorMap.add('a', 1);
accumulatorMap.add('b', 2);
accumulatorMap.add('c', 3);
accumulatorMap.add('b', 4);
accumulatorMap.add('c', 5);
accumulatorMap.add('c', 6);
expectMap(accumulatorMap).to.deep.equal({
a: [1],
b: [2, 4],
c: [3, 5, 6],
});
});
});
12 changes: 4 additions & 8 deletions src/jsutils/groupBy.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { AccumulatorMap } from './AccumulatorMap';

/**
* Groups array items into a Map, given a function to produce grouping key.
*/
export function groupBy<K, T>(
list: ReadonlyArray<T>,
keyFn: (item: T) => K,
): Map<K, ReadonlyArray<T>> {
const result = new Map<K, Array<T>>();
const result = new AccumulatorMap<K, T>();
for (const item of list) {
const key = keyFn(item);
const group = result.get(key);
if (group === undefined) {
result.set(key, [item]);
} else {
group.push(item);
}
result.add(keyFn(item), item);
}
return result;
}