Skip to content

Commit

Permalink
Golf code size of collectTypesFromResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Dec 2, 2020
1 parent b944db4 commit 403a263
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions packages/core/src/utils/typenames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ interface EntityLike {
__typename: string | null | void;
}

const collectTypes = (obj: EntityLike | EntityLike[], types: string[] = []) => {
const collectTypes = (
obj: EntityLike | EntityLike[],
types: { [typename: string]: unknown }
) => {
if (Array.isArray(obj)) {
obj.forEach(inner => {
collectTypes(inner, types);
});
for (let i = 0; i < obj.length; i++) collectTypes(obj[i], types);
} else if (typeof obj === 'object' && obj !== null) {
for (const key in obj) {
if (key === '__typename' && typeof obj[key] === 'string') {
types.push(obj[key] as string);
types[obj[key] as string] = 0;
} else {
collectTypes(obj[key], types);
}
Expand All @@ -31,7 +32,7 @@ const collectTypes = (obj: EntityLike | EntityLike[], types: string[] = []) => {
};

export const collectTypesFromResponse = (response: object) =>
collectTypes(response as EntityLike).filter((v, i, a) => a.indexOf(v) === i);
Object.keys(collectTypes(response as EntityLike, {}));

const formatNode = (node: FieldNode | InlineFragmentNode) => {
if (
Expand Down

0 comments on commit 403a263

Please sign in to comment.