Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify lookups
Browse files Browse the repository at this point in the history
HalFrgrd committed Dec 16, 2024
1 parent 6cdbe4c commit c2e7627
Showing 2 changed files with 5 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/geosearch.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
33 changes: 4 additions & 29 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit c2e7627

Please sign in to comment.