Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

1.0 KB
Raw Blame
/**
 * Helper function to parse numbers from query params. Can map varying input keys to the same output key.
 * @param mappings - Object mapping output keys to input keys
 * @returns A function that takes a set of query params and returns an object with the parsed values
 */
export function createNumericParser<T extends Record<string, number>>(
  mappings: Record<keyof T, string[]>
): (params: Record<string, unknown>) => T | null {
  return (params: Record<string, unknown>) => {
    const result = {} as T;
    let hasValidProperty = false;
    for (const [outputKey, inputKeys] of Object.entries(mappings)) {
      for (const inputKey of inputKeys) {
        if (inputKey in params) {
          const value = params[inputKey];
          const parsedValue = Number(value);

          if (!isNaN(parsedValue)) {
            (result as Record<string, number>)[outputKey] = parsedValue;
            hasValidProperty = true;
            break;
          }
        }
      }
    }
    return hasValidProperty ? (result as T) : null;
  };
}