From f3fd52ead4f2e4ed4a459558025258cfed881c01 Mon Sep 17 00:00:00 2001 From: Michael Haufe Date: Sun, 23 Feb 2025 10:17:10 -0600 Subject: [PATCH] Replace custom groupBy implementation with Object.groupBy and remove related tests (#498) --- application/OrganizationInteractor.ts | 2 +- .../NaturalLanguageToRequirementService.ts | 4 +- shared/utils/groupBy.test.ts | 46 ------------------- shared/utils/groupBy.ts | 12 ----- shared/utils/index.ts | 1 - 5 files changed, 3 insertions(+), 62 deletions(-) delete mode 100644 shared/utils/groupBy.test.ts delete mode 100644 shared/utils/groupBy.ts diff --git a/application/OrganizationInteractor.ts b/application/OrganizationInteractor.ts index d2bd9ec3..1a9a016a 100644 --- a/application/OrganizationInteractor.ts +++ b/application/OrganizationInteractor.ts @@ -516,7 +516,7 @@ export class OrganizationInteractor extends Interactor { }) // Group the results by the requirement type - return groupBy(requirements, ({ req_type }) => req_type) + return Object.groupBy(requirements, ({ req_type }) => req_type) } */ diff --git a/server/data/services/NaturalLanguageToRequirementService.ts b/server/data/services/NaturalLanguageToRequirementService.ts index af0c7098..1ce981a7 100644 --- a/server/data/services/NaturalLanguageToRequirementService.ts +++ b/server/data/services/NaturalLanguageToRequirementService.ts @@ -3,7 +3,7 @@ import { v7 as uuidv7 } from 'uuid'; import zodToJsonSchema from "zod-to-json-schema"; import zodSchema from '../llm-zod-schemas/index.js' import { zodResponseFormat } from "openai/helpers/zod"; -import { dedent, groupBy } from "#shared/utils"; +import { dedent } from "#shared/utils"; type LLMResponseType = typeof zodSchema['_type']['requirements'] type ArrayToUnion = T extends (infer U)[] ? U : never @@ -53,6 +53,6 @@ export default class NaturalLanguageToRequirementService { const result = (completion.choices[0].message.parsed?.requirements ?? []) .map((req) => ({ ...req, id: uuidv7() })); - return groupBy(result, ({ type }) => type) as ParsedRequirementGroup + return Object.groupBy(result, ({ type }) => type) as ParsedRequirementGroup } } \ No newline at end of file diff --git a/shared/utils/groupBy.test.ts b/shared/utils/groupBy.test.ts deleted file mode 100644 index d12d4a1c..00000000 --- a/shared/utils/groupBy.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { describe, it, expect } from 'vitest'; -import { groupBy } from './groupBy'; - -describe('groupBy', () => { - it('should group items by a string key', () => { - const items = [ - { category: 'fruit', name: 'apple' }, - { category: 'fruit', name: 'banana' }, - { category: 'vegetable', name: 'carrot' } - ]; - const result = groupBy(items, item => item.category); - expect(result).toEqual({ - fruit: [ - { category: 'fruit', name: 'apple' }, - { category: 'fruit', name: 'banana' } - ], - vegetable: [ - { category: 'vegetable', name: 'carrot' } - ] - }); - }); - - it('should group items by a numeric key', () => { - const items = [1.1, 2.2, 1.3, 2.4]; - const result = groupBy(items, item => Math.floor(item)); - expect(result).toEqual({ - 1: [1.1, 1.3], - 2: [2.2, 2.4] - }); - }); - - it('should handle an empty array', () => { - const items: any[] = []; - const result = groupBy(items, item => item); - expect(result).toEqual({}); - }); - - it('should handle grouping by index', () => { - const items = ['a', 'b', 'c', 'd']; - const result = groupBy(items, (item, index) => index % 2); - expect(result).toEqual({ - 0: ['a', 'c'], - 1: ['b', 'd'] - }); - }); -}); \ No newline at end of file diff --git a/shared/utils/groupBy.ts b/shared/utils/groupBy.ts deleted file mode 100644 index 9386038e..00000000 --- a/shared/utils/groupBy.ts +++ /dev/null @@ -1,12 +0,0 @@ -// The server is currently Node 20 which does not support Object.groupBy. -// See: https://github.com/final-hill/cathedral/issues/371 -function groupBy(items: Iterable, keySelector: (item: T, index: number) => K): Partial> { - return [...items].reduce((acc, item, index) => { - const key = keySelector(item, index), - group = (acc as any)[key as any] ?? ((acc as any)[key as any] = []) - group.push(item) - return acc - }, {} as Partial>) -} - -export { groupBy } \ No newline at end of file diff --git a/shared/utils/index.ts b/shared/utils/index.ts index 4f654c22..8f03fda0 100644 --- a/shared/utils/index.ts +++ b/shared/utils/index.ts @@ -4,7 +4,6 @@ export * from './camelCaseToTitleCase'; export * from './debounce'; export * from './dedent'; export * from './deSlugify'; -export * from './groupBy'; export * from './pascalCaseToSnakeCase' export * from './slugify'; export * from './snakeCaseToCamelCase';