UNPKG

@zubridge/electron

Version:

A streamlined state management library for Electron applications using Zustand.

56 lines (55 loc) 1.9 kB
/** * Helper function to find a case-insensitive match in an object */ export function findCaseInsensitiveMatch(obj, key) { // Try exact match first if (key in obj) { return [key, obj[key]]; } // Try case-insensitive match const keyLower = key.toLowerCase(); const matchingKey = Object.keys(obj).find((k) => k.toLowerCase() === keyLower); if (matchingKey) { return [matchingKey, obj[matchingKey]]; } return undefined; } /** * Helper function to find a handler by nested path * Example: "counter.increment" -> obj.counter.increment */ export function findNestedHandler(obj, path) { try { const parts = path.split('.'); let current = obj; // Navigate through each part of the path for (let i = 0; i < parts.length; i++) { const part = parts[i]; // Case-insensitive comparison for each level const keys = Object.keys(current); const matchingKey = keys.find((k) => k.toLowerCase() === part.toLowerCase()); if (matchingKey === undefined) { return undefined; } current = current[matchingKey]; } return typeof current === 'function' ? current : undefined; } catch (error) { console.error('Error resolving nested handler:', error); return undefined; } } /** * Resolves a handler function from provided handlers using action type * This handles both direct matches and nested path resolution */ export function resolveHandler(handlers, actionType) { // Try direct match with handlers const handlerMatch = findCaseInsensitiveMatch(handlers, actionType); if (handlerMatch && typeof handlerMatch[1] === 'function') { return handlerMatch[1]; } // Try nested path resolution in handlers return findNestedHandler(handlers, actionType); }