UNPKG

@tanstack/optimistic

Version:

Core optimistic updates library

153 lines (152 loc) 4.58 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); function upperFunction(arg) { if (typeof arg !== `string`) { throw new Error(`UPPER function expects a string argument`); } return arg.toUpperCase(); } function lowerFunction(arg) { if (typeof arg !== `string`) { throw new Error(`LOWER function expects a string argument`); } return arg.toLowerCase(); } function lengthFunction(arg) { if (typeof arg === `string` || Array.isArray(arg)) { return arg.length; } throw new Error(`LENGTH function expects a string or array argument`); } function concatFunction(arg) { if (!Array.isArray(arg)) { throw new Error(`CONCAT function expects an array of string arguments`); } if (arg.length === 0) { return ``; } for (let i = 0; i < arg.length; i++) { if (arg[i] !== null && arg[i] !== void 0 && typeof arg[i] !== `string`) { throw new Error( `CONCAT function expects all arguments to be strings, but argument at position ${i} is ${typeof arg[i]}` ); } } return arg.map((str) => str === null || str === void 0 ? `` : str).join(``); } function coalesceFunction(arg) { if (!Array.isArray(arg)) { throw new Error(`COALESCE function expects an array of arguments`); } if (arg.length === 0) { return null; } for (const value of arg) { if (value !== null && value !== void 0) { return value; } } return null; } function dateFunction(arg) { if (arg instanceof Date) { return arg; } if (arg === null || arg === void 0) { return null; } if (typeof arg === `string` || typeof arg === `number`) { const date = new Date(arg); if (isNaN(date.getTime())) { throw new Error(`DATE function could not parse "${arg}" as a valid date`); } return date; } throw new Error(`DATE function expects a string, number, or Date argument`); } function jsonExtractFunction(arg) { if (!Array.isArray(arg) || arg.length < 1) { throw new Error( `JSON_EXTRACT function expects an array with at least one element [jsonInput, ...pathElements]` ); } const [jsonInput, ...pathElements] = arg; if (jsonInput === null || jsonInput === void 0) { return null; } let jsonData; if (typeof jsonInput === `string`) { try { jsonData = JSON.parse(jsonInput); } catch (error) { throw new Error( `JSON_EXTRACT function could not parse JSON string: ${error instanceof Error ? error.message : String(error)}` ); } } else if (typeof jsonInput === `object`) { jsonData = jsonInput; } else { throw new Error( `JSON_EXTRACT function expects a JSON string or object as the first argument` ); } if (pathElements.length === 0) { return jsonData; } let current = jsonData; for (let i = 0; i < pathElements.length; i++) { const pathElement = pathElements[i]; if (typeof pathElement !== `string`) { throw new Error( `JSON_EXTRACT function expects path elements to be strings, but element at position ${i + 1} is ${typeof pathElement}` ); } if (current === null || current === void 0 || typeof current !== `object`) { return null; } current = current[pathElement]; } return current === void 0 ? null : current; } function orderIndexFunction(arg) { if (arg !== `numeric` && arg !== `fractional` && arg !== true && arg !== `default`) { throw new Error( `ORDER_INDEX function expects "numeric", "fractional", "default", or true as argument` ); } return null; } const functionImplementations = { // Map function names to their implementation functions DATE: dateFunction, JSON_EXTRACT: jsonExtractFunction, JSON_EXTRACT_PATH: jsonExtractFunction, // Alias for JSON_EXTRACT UPPER: upperFunction, LOWER: lowerFunction, COALESCE: coalesceFunction, CONCAT: concatFunction, LENGTH: lengthFunction, ORDER_INDEX: orderIndexFunction }; function evaluateFunction(functionName, arg) { const implementation = functionImplementations[functionName]; if (!implementation) { throw new Error(`Unknown function: ${functionName}`); } return implementation(arg); } function isFunctionCall(obj) { if (!obj || typeof obj !== `object`) { return false; } const keys = Object.keys(obj); if (keys.length !== 1) { return false; } const functionName = keys[0]; return Object.keys(functionImplementations).includes(functionName); } exports.evaluateFunction = evaluateFunction; exports.isFunctionCall = isFunctionCall; //# sourceMappingURL=functions.cjs.map