mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
23 lines
455 B
TypeScript
23 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
|
||
|
}
|