mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
fix(nuxt3): improve types of uniqueBy
utility (#3897)
This commit is contained in:
parent
773dd59a2c
commit
e7b57fa34c
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user