fix(ui/datatables): simplify getRowId

refactor/EE-4337/service-task-datatable
Chaim Lev-Ari 2023-07-30 12:52:35 +02:00
parent bb7c6077d5
commit ca4130b221
1 changed files with 9 additions and 17 deletions

View File

@ -1,25 +1,17 @@
import { DefaultType } from './types'; import { DefaultType } from './types';
/**
* gets row id by looking for one of id, Id, or ID keys on the object
*/
export function defaultGetRowId<D extends DefaultType>(row: D): string { export function defaultGetRowId<D extends DefaultType>(row: D): string {
if ( const key = ['id', 'Id', 'ID'].find((key) =>
'id' in row && Object.hasOwn(row, key)
(typeof row.id === 'string' || typeof row.id === 'number') ) as keyof D;
) {
return row.id.toString();
}
if ( const value = row[key];
'Id' in row &&
(typeof row.Id === 'string' || typeof row.Id === 'number')
) {
return row.Id.toString();
}
if ( if (typeof value === 'string' || typeof value === 'number') {
'ID' in row && return value.toString();
(typeof row.ID === 'string' || typeof row.ID === 'number')
) {
return row.ID.toString();
} }
return ''; return '';