@polgubau/utils
Version:
A collection of utility functions for TypeScript
1 lines • 2.07 kB
Source Map (JSON)
{"version":3,"sources":["../../../../src/objects/get-main-field/get-main-field.ts"],"sourcesContent":[" export const DEFAULT_KEYS = [\n \"displayName\",\n \"name\",\n \"label\",\n \"title\",\n \"main\",\n \"code\",\n \"value\",\n \"userName\",\n \"locale\",\n \"id\",\n \"fullName\",\n \"shortName\",\n];\n\n/**\n * Retrieves the main field from an item object based on a predefined list of labels to highlight.\n *\n * @param item - The item object from which to extract the main field.\n * @returns An object containing the key and value of the main field.\n *\n * The function searches for the first label in the `labelsToHighlight` array that exists in the item object.\n * If a label is found, it returns an object with the label as the key and the corresponding value from the item.\n * If no label is found, it returns the first key-value pair from the item object.\n */\nexport const getMainField = (\n // biome-ignore lint/suspicious/noExplicitAny: <explanation>\n item: Record<string, any>,\n): {\n key: string;\n value: string;\n} => {\n // search for the first time a label is found in labelsToHighlight (1st position has preference over 2nd position)\n\n const label = DEFAULT_KEYS.find((label) => {\n const value = item?.[label];\n if (value) {\n return true;\n }\n });\n\n if (label) {\n const value = item[label];\n return { key: label, value };\n }\n\n const object = {\n key: Object.keys(item)[0],\n value: item[Object.keys(item)[0] as string],\n };\n\n return object;\n};\n"],"mappings":";AAAQ,IAAM,eAAe;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAYO,IAAM,eAAe,CAE1B,SAIG;AAGH,QAAM,QAAQ,aAAa,KAAK,CAACA,WAAU;AACzC,UAAM,QAAQ,OAAOA,MAAK;AAC1B,QAAI,OAAO;AACT,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AAED,MAAI,OAAO;AACT,UAAM,QAAQ,KAAK,KAAK;AACxB,WAAO,EAAE,KAAK,OAAO,MAAM;AAAA,EAC7B;AAEA,QAAM,SAAS;AAAA,IACb,KAAK,OAAO,KAAK,IAAI,EAAE,CAAC;AAAA,IACxB,OAAO,KAAK,OAAO,KAAK,IAAI,EAAE,CAAC,CAAW;AAAA,EAC5C;AAEA,SAAO;AACT;","names":["label"]}