Skip to content

Commit

Permalink
Try to type Item and getValue (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bettelstab committed Jan 31, 2025
1 parent 69c735b commit a9ed390
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
7 changes: 1 addition & 6 deletions src/Components/Map/Layer.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
/* eslint-disable @typescript-eslint/restrict-plus-operands */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/prefer-optional-chain */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { Children, isValidElement, useEffect, useState } from 'react'
import { Marker, Tooltip } from 'react-leaflet'

Expand Down Expand Up @@ -319,7 +314,7 @@ export const Layer = ({
</>
)}
<Tooltip offset={[0, -38]} direction='top'>
{item.name ? item.name : getValue(item, itemNameField)}
{item.name ? item.name : `${getValue(item, itemNameField)}`}
</Tooltip>
</Marker>
)
Expand Down
13 changes: 8 additions & 5 deletions src/Utils/GetValue.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
export function getValue(obj, path) {
import type { Item } from '#types/Item'

function getNestedValue(obj: Object, path: string) {

Check warning on line 3 in src/Utils/GetValue.ts

View workflow job for this annotation

GitHub Actions / Lint

'getNestedValue' is defined but never used

Check warning on line 3 in src/Utils/GetValue.ts

View workflow job for this annotation

GitHub Actions / Lint

'obj' is defined but never used
re
}

export function getValue(obj: Item | undefined, path: string): Item | string | undefined {
if (!obj || typeof path !== 'string') return undefined

const pathArray = path.split('.') // Use a different variable for the split path
for (let i = 0, len = pathArray.length; i < len; i++) {
if (!obj) return undefined // Check if obj is falsy at each step
// eslint-disable-next-line security/detect-object-injection
obj = obj[pathArray[i]] // Dive one level deeper
obj = obj[pathArray[i]] as Item // Dive one level deeper
}
return obj // Return the final value
}
3 changes: 1 addition & 2 deletions types/Item.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export interface Item {
slug?: string
user_created?: UserItem
image?: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any
group_type: string
/* constructor(
id: string,
name: string,
Expand Down

0 comments on commit a9ed390

Please sign in to comment.