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, '')
}
function uniqueBy (arr: any[], uniqueKey: string) {
const seen = new Set<string>()
const res = []
for (const i of arr) {
const key = i[uniqueKey]
if (seen.has(key)) { continue }
res.push(i)
seen.add(key)
function uniqueBy <T, K extends keyof T> (arr: T[], key: K) {
const res: T[] = []
const seen = new Set<T[K]>()
for (const item of arr) {
if (seen.has(item[key])) { continue }
seen.add(item[key])
res.push(item)
}
return res
}

View File

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