mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 23:22:02 +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, '')
|
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
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user