UNPKG

@tanstack/optimistic

Version:

Core optimistic updates library

155 lines (154 loc) 4.32 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); function isAggregateFunctionCall(obj) { if (!obj || typeof obj !== `object`) return false; const aggregateFunctions = [ `SUM`, `COUNT`, `AVG`, `MIN`, `MAX`, `MEDIAN`, `MODE` ]; const keys = Object.keys(obj); return keys.length === 1 && aggregateFunctions.includes(keys[0]); } function isOrderIndexFunctionCall(obj) { if (!obj || typeof obj !== `object`) return false; const keys = Object.keys(obj); return keys.length === 1 && keys[0] === `ORDER_INDEX`; } function isComparable(value) { return typeof value === `number` || typeof value === `string` || typeof value === `boolean` || value instanceof Date; } function compareValues(left, right, operator) { if (!isComparable(left) || !isComparable(right)) { throw new Error( `Cannot compare non-comparable values: ${typeof left} and ${typeof right}` ); } if (typeof left !== typeof right && (typeof left === `string` || typeof left === `number`) && (typeof right === `string` || typeof right === `number`)) { const leftStr = String(left); const rightStr = String(right); switch (operator) { case `<`: return leftStr < rightStr; case `<=`: return leftStr <= rightStr; case `>`: return leftStr > rightStr; case `>=`: return leftStr >= rightStr; } } if (left instanceof Date && right instanceof Date) { const leftTime = left.getTime(); const rightTime = right.getTime(); switch (operator) { case `<`: return leftTime < rightTime; case `<=`: return leftTime <= rightTime; case `>`: return leftTime > rightTime; case `>=`: return leftTime >= rightTime; } } if (typeof left === typeof right) { switch (operator) { case `<`: return left < right; case `<=`: return left <= right; case `>`: return left > right; case `>=`: return left >= right; } } throw new Error( `Cannot compare incompatible types: ${typeof left} and ${typeof right}` ); } function convertLikeToRegex(pattern) { let finalPattern = ``; let i = 0; while (i < pattern.length) { const char = pattern[i]; if (char === `\\` && i + 1 < pattern.length) { finalPattern += pattern[i + 1]; i += 2; continue; } switch (char) { case `%`: finalPattern += `.*`; break; case `_`: finalPattern += `.`; break; // Handle regex special characters case `.`: case `^`: case `$`: case `*`: case `+`: case `?`: case `(`: case `)`: case `[`: case `]`: case `{`: case `}`: case `|`: case `/`: finalPattern += `\\` + char; break; default: finalPattern += char; } i++; } return finalPattern; } function isValueInArray(value, array, caseInsensitive = false) { if (array.includes(value)) { return true; } if (value === null || value === void 0) { return array.some((item) => item === null || item === void 0); } if (typeof value === `number` || typeof value === `string`) { return array.some((item) => { if (typeof item === typeof value) { if (typeof value === `string` && caseInsensitive) { return value.toLowerCase() === item.toLowerCase(); } return item === value; } if ((typeof item === `number` || typeof item === `string`) && (typeof value === `number` || typeof value === `string`)) { return String(item) === String(value); } return false; }); } if (typeof value === `object`) { const valueStr = JSON.stringify(value); return array.some((item) => { if (typeof item === `object` && item !== null) { return JSON.stringify(item) === valueStr; } return false; }); } return false; } exports.compareValues = compareValues; exports.convertLikeToRegex = convertLikeToRegex; exports.isAggregateFunctionCall = isAggregateFunctionCall; exports.isComparable = isComparable; exports.isOrderIndexFunctionCall = isOrderIndexFunctionCall; exports.isValueInArray = isValueInArray; //# sourceMappingURL=utils.cjs.map