(itemIdOrIndex: string, primaryKeyName: Extract<keyof L, string>, sortedList: L[])
| 49 | } |
| 50 | |
| 51 | export function convertToId<L>(itemIdOrIndex: string, primaryKeyName: Extract<keyof L, string>, sortedList: L[]): string | false { |
| 52 | if (itemIdOrIndex.length === 0) { |
| 53 | return false |
| 54 | } |
| 55 | const matchingItem = sortedList.find(item => { |
| 56 | const pk = item[primaryKeyName] |
| 57 | return typeof pk === 'string' && itemIdOrIndex === pk |
| 58 | }) |
| 59 | if (matchingItem) { |
| 60 | return itemIdOrIndex |
| 61 | } |
| 62 | |
| 63 | if (!isIndexArgument(itemIdOrIndex)) { |
| 64 | return false |
| 65 | } |
| 66 | |
| 67 | const index = Number.parseInt(itemIdOrIndex) |
| 68 | |
| 69 | if (!Number.isNaN(index) && index > 0 && index <= sortedList.length) { |
| 70 | const pk = sortedList[index - 1][primaryKeyName] |
| 71 | if (typeof pk === 'string') { |
| 72 | return pk |
| 73 | } |
| 74 | throw Error(`invalid type ${typeof pk} for primary key` + |
| 75 | ` ${primaryKeyName} in ${JSON.stringify(sortedList[index - 1])}`) |
| 76 | } |
| 77 | return false |
| 78 | } |
| 79 | |
| 80 | export async function stringGetIdFromUser<L extends object>(fieldInfo: Sorting<L>, list: L[], prompt?: string): Promise<string> { |
| 81 | const primaryKeyName = fieldInfo.primaryKeyName |
no test coverage detected