Skip to content

Commit 18baa89

Browse files
feat(util): introduce uniq() utility and standardize array dedup across codebase
Centralizes array dedup using a new uniq() function to replace scattered Set-based patterns and prepare groundwork for removing lodash replacements. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent d6e1fd5 commit 18baa89

File tree

6 files changed

+20
-14
lines changed

6 files changed

+20
-14
lines changed

common/src/util/lodash-replacements.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ export function union<T>(arr1: T[], arr2: T[]): T[] {
5454
export function partition<T, S extends T>(
5555
array: T[],
5656
predicate: (value: T) => value is S,
57-
): [S[], Exclude<T, S>[]];
57+
): [S[], Exclude<T, S>[]]
5858
export function partition<T>(
5959
array: T[],
6060
predicate: (value: T) => boolean,
61-
): [T[], T[]];
61+
): [T[], T[]]
6262
export function partition<T>(
6363
array: T[],
6464
predicate: (value: T) => boolean,
@@ -74,3 +74,8 @@ export function partition<T>(
7474
}
7575
return [truthy, falsy]
7676
}
77+
78+
// Remove duplicates from an array
79+
export function uniq<T>(arr: T[]): T[] {
80+
return Array.from(new Set(arr))
81+
}

packages/agent-runtime/src/find-files/request-files-prompt.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
type FinetunedVertexModel,
77
} from '@codebuff/common/old-constants'
88
import { getAllFilePaths } from '@codebuff/common/project-file-tree'
9-
import { range, shuffle } from '@codebuff/common/util/lodash-replacements'
9+
import { range, shuffle, uniq } from '@codebuff/common/util/lodash-replacements'
1010

1111
import { promptFlashWithFallbacks } from '../llm-api/gemini-with-fallbacks'
1212
import {
@@ -82,7 +82,7 @@ export async function requestRelevantFiles(
8282

8383
const candidateFiles = (await keyPromise).files
8484

85-
validateFilePaths(Array.from(new Set(candidateFiles)))
85+
validateFilePaths(uniq(candidateFiles))
8686

8787
// logger.info(
8888
// {
@@ -149,7 +149,7 @@ export async function requestRelevantFilesForTraining(
149149
})
150150

151151
const candidateFiles = [...keyFiles.files, ...nonObviousFiles.files]
152-
const validatedFiles = validateFilePaths(Array.from(new Set(candidateFiles)))
152+
const validatedFiles = validateFilePaths(uniq(candidateFiles))
153153
logger.debug(
154154
{ keyFiles, nonObviousFiles, validatedFiles },
155155
'requestRelevantFilesForTraining: results',
@@ -322,10 +322,7 @@ function getExampleFileList(params: {
322322
selectedDirectories.add(dirname(filePath))
323323
}
324324

325-
return Array.from(new Set([...selectedFiles, ...randomFilePaths])).slice(
326-
0,
327-
count,
328-
)
325+
return uniq([...selectedFiles, ...randomFilePaths]).slice(0, count)
329326
}
330327

331328
function generateNonObviousRequestFilesPrompt(

packages/agent-runtime/src/get-file-reading-updates.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { RequestFilesFn } from '@codebuff/common/types/contracts/client'
2+
import { uniq } from '@codebuff/common/util/lodash-replacements'
23

34
export async function getFileReadingUpdates(params: {
45
requestFiles: RequestFilesFn
@@ -11,7 +12,7 @@ export async function getFileReadingUpdates(params: {
1112
> {
1213
const { requestFiles, requestedFiles } = params
1314

14-
const allFilePaths = Array.from(new Set(requestedFiles))
15+
const allFilePaths = uniq(requestedFiles)
1516
const loadedFiles = await requestFiles({ filePaths: allFilePaths })
1617

1718
const addedFiles = allFilePaths

packages/code-map/src/parse.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as fs from 'fs'
22
import * as path from 'path'
33

4+
import { uniq } from '@codebuff/common/util/lodash-replacements'
5+
46
import { getLanguageConfig, LanguageConfig } from './languages'
57
import type { Parser, Query } from 'web-tree-sitter'
68

@@ -172,8 +174,8 @@ export function parseTokens(
172174
throw new Error('Parser or query not found')
173175
}
174176
const parseResults = parseFile(parser, query, sourceCode)
175-
const identifiers = Array.from(new Set(parseResults.identifier))
176-
const calls = Array.from(new Set(parseResults['call.identifier']))
177+
const identifiers = uniq(parseResults.identifier)
178+
const calls = uniq(parseResults['call.identifier'])
177179

178180
if (DEBUG_PARSING) {
179181
console.log(`\nParsing ${filePath}:`)

scripts/generate-ci-env.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import fs from 'fs'
88
import path from 'path'
99
import { fileURLToPath } from 'url'
10+
import { uniq } from '../common/src/util/lodash-replacements.ts'
1011

1112
const __filename = fileURLToPath(import.meta.url)
1213
const __dirname = path.dirname(__filename)
@@ -88,7 +89,7 @@ function generateGitHubEnv() {
8889
} else if (scope === 'client') {
8990
selected = varsByScope.client
9091
} else {
91-
selected = Array.from(new Set([...varsByScope.server, ...varsByScope.client]))
92+
selected = uniq([...varsByScope.server, ...varsByScope.client])
9293
}
9394

9495
if (prefix) {

web/src/lib/docs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ export async function getNewsArticles(): Promise<NewsArticle[]> {
3434

3535
// export function getAllCategories() {
3636
// if (!allDocs) return []
37-
// return Array.from(new Set((allDocs as Doc[]).map((doc: Doc) => doc.category)))
37+
// return uniq((allDocs as Doc[]).map((doc: Doc) => doc.category))
3838
// }

0 commit comments

Comments
 (0)