overleaf/services/web/frontend/js/utils/normalize.ts
ilkin-overleaf 5b0c122f5d Merge pull request #7290 from overleaf/ii-7154-list-user-emails
List of user emails

GitOrigin-RevId: 28a8e405812932ba7ebd8043a4dc9d3c573a68b2
2022-04-11 08:03:38 +00:00

22 lines
455 B
TypeScript

import mapKeys from 'lodash/mapKeys'
export interface NormalizedObject<T> {
[p: string]: T
}
type Data<T> = T[]
type Config = Partial<{
idAttribute: string
}>
export function normalize<T>(
data: Data<T>,
config: Config = {}
): NormalizedObject<T> | undefined {
const { idAttribute = 'id' } = config
const mapped = mapKeys(data, idAttribute)
return Object.prototype.hasOwnProperty.call(mapped, 'undefined')
? undefined
: mapped
}