From ca4130b22134a291f4b6a44682fb2ded512e308c Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Sun, 30 Jul 2023 12:52:35 +0200 Subject: [PATCH] fix(ui/datatables): simplify getRowId --- .../components/datatables/defaultGetRowId.ts | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/app/react/components/datatables/defaultGetRowId.ts b/app/react/components/datatables/defaultGetRowId.ts index 722edb2c8..1c2f63735 100644 --- a/app/react/components/datatables/defaultGetRowId.ts +++ b/app/react/components/datatables/defaultGetRowId.ts @@ -1,25 +1,17 @@ import { DefaultType } from './types'; +/** + * gets row id by looking for one of id, Id, or ID keys on the object + */ export function defaultGetRowId(row: D): string { - if ( - 'id' in row && - (typeof row.id === 'string' || typeof row.id === 'number') - ) { - return row.id.toString(); - } + const key = ['id', 'Id', 'ID'].find((key) => + Object.hasOwn(row, key) + ) as keyof D; - if ( - 'Id' in row && - (typeof row.Id === 'string' || typeof row.Id === 'number') - ) { - return row.Id.toString(); - } + const value = row[key]; - if ( - 'ID' in row && - (typeof row.ID === 'string' || typeof row.ID === 'number') - ) { - return row.ID.toString(); + if (typeof value === 'string' || typeof value === 'number') { + return value.toString(); } return '';