From c2e76279c5d81feca0dea00320b9ad3bb0fa745b Mon Sep 17 00:00:00 2001 From: Hal Frigaard <4559349+HalFrgrd@users.noreply.github.com> Date: Mon, 16 Dec 2024 18:07:00 +0000 Subject: [PATCH] simplify lookups --- src/geosearch.ts | 2 +- src/utils.ts | 33 ++++----------------------------- 2 files changed, 5 insertions(+), 30 deletions(-) diff --git a/src/geosearch.ts b/src/geosearch.ts index 9e7b679..04178d0 100644 --- a/src/geosearch.ts +++ b/src/geosearch.ts @@ -149,7 +149,7 @@ export async function googlePlacesSearch( name: `${result?.name} (${result?.formatted_address})`, location: geolocation, resultType: 'searchResult', - extraLocationData: { google_maps_place: result }, + extraLocationData: { google_maps_place_data: result }, } as GeoSearchResult); } } diff --git a/src/utils.ts b/src/utils.ts index d342b5b..f296b2b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -42,40 +42,15 @@ export function getLastUsedValidMarkdownLeaf() { } function resolveJsonPath(json: object, path: string): string { + // convert a string path like "some.path.to.data.0" to the value at that path in json // Remove leading/trailing curly braces and split the path into parts const pathParts = path.replace(/[{}]/g, '').split('.'); - - // Iterate through each part of the path - let current = json; - for (let part of pathParts) { - // Handle array index - const arrayMatch = part.match(/^(\w+)\[(\d+)\]$/); - if (arrayMatch) { - const key = arrayMatch[1]; - const index = parseInt(arrayMatch[2], 10); - if ( - current[key] && - Array.isArray(current[key]) && - current[key][index] !== undefined - ) { - current = current[key][index]; - } else { - return ''; // Path doesn't exist - } - } else { - // Access object property - if (current && current.hasOwnProperty(part)) { - current = current[part]; - } else { - return ''; // Path doesn't exist - } - } - } - return JSON.stringify(current); + // Use reduce with optional chaining to traverse the path + return pathParts.reduce((current: object, part: string) => {return current?.[part];}, json); } function replaceJsonPaths(inputString: string, json: object) { - // Use regex to find all patterns like {{some.path.to[0].data}} + // Use regex to find all patterns like {{some.path.to.data.0}} return inputString.replace(/{{(.*?)}}/g, (_, path: string) => { const value = resolveJsonPath(json, path); // return value !== undefined ? value : null;