fix(nuxt3): improve types of uniqueBy utility (#3897)

This commit is contained in:
Kevin Marrec 2022-03-25 12:54:40 +01:00 committed by GitHub
parent 773dd59a2c
commit e7b57fa34c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 15 deletions

View File

@ -106,14 +106,13 @@ function getNameFromPath (path: string) {
return kebabCase(basename(path).replace(extname(path), '')).replace(/["']/g, '') return kebabCase(basename(path).replace(extname(path), '')).replace(/["']/g, '')
} }
function uniqueBy (arr: any[], uniqueKey: string) { function uniqueBy <T, K extends keyof T> (arr: T[], key: K) {
const seen = new Set<string>() const res: T[] = []
const res = [] const seen = new Set<T[K]>()
for (const i of arr) { for (const item of arr) {
const key = i[uniqueKey] if (seen.has(item[key])) { continue }
if (seen.has(key)) { continue } seen.add(item[key])
res.push(i) res.push(item)
seen.add(key)
} }
return res return res
} }

View File

@ -272,14 +272,12 @@ export function getImportName (name: string) {
return pascalCase(name).replace(/[^\w]/g, '') return pascalCase(name).replace(/[^\w]/g, '')
} }
function uniqueBy (arr: any[], key: string) { function uniqueBy <T, K extends keyof T> (arr: T[], key: K) {
const res = [] const res: T[] = []
const keys = new Set<string>() const seen = new Set<T[K]>()
for (const item of arr) { for (const item of arr) {
if (keys.has(item[key])) { if (seen.has(item[key])) { continue }
continue seen.add(item[key])
}
keys.add(item[key])
res.push(item) res.push(item)
} }
return res return res