'use strict'; /** * Sequelize where 条件匹配器(测试专用)。 * 供 service 层测试的 mock 模型复用:在内存中按真实 where 语义过滤 fixture 行, * 重点支持 Op.like(含 % _ 与反斜杠转义语义),避免只断言到「假行为」。 */ const Sequelize = require('sequelize'); const { Op } = Sequelize; /** * 宽松等值比较:先严格比较,再对数字做数值化比较(兼容字符串/数字混用)。 * @param {*} left 左值。 * @param {*} right 右值。 * @return {boolean} 是否等值。 */ function looseEqual(left, right) { if (left === right) return true; if (left === null || right === null || left === undefined || right === undefined) return false; const leftNum = Number(left); const rightNum = Number(right); if (String(left).trim() !== '' && String(right).trim() !== '' && Number.isFinite(leftNum) && Number.isFinite(rightNum)) { return leftNum === rightNum; } return String(left) === String(right); } /** * 转义正则元字符。 * @param {string} ch 单个字符。 * @return {string} 转义后的字符。 */ function escapeRegExpChar(ch) { return ch.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } /** * 将 SQL LIKE 模式转换为等价正则(支持 \ 转义 % _ \)。 * @param {string} pattern LIKE 模式。 * @return {RegExp} 等价正则。 */ function likeToRegExp(pattern) { let body = '^'; for (let index = 0; index < pattern.length; index++) { const ch = pattern[index]; if (ch === '\\') { const next = pattern[index + 1]; if (next !== undefined) { body += escapeRegExpChar(next); index++; } else { body += '\\\\'; } } else if (ch === '%') { body += '.*'; } else if (ch === '_') { body += '.'; } else { body += escapeRegExpChar(ch); } } body += '$'; return new RegExp(body); } /** * 判断字段值是否命中 LIKE 模式。 * @param {*} value 字段值。 * @param {string} pattern LIKE 模式。 * @return {boolean} 是否命中。 */ function matchLike(value, pattern) { return likeToRegExp(String(pattern)).test(String(value === null || value === undefined ? '' : value)); } /** * 判断单条条件对象是否命中。 * @param {Object} row 待匹配行。 * @param {string} field 字段名。 * @param {Object} condition 条件对象。 * @return {boolean} 是否命中。 */ function matchConditionObject(row, field, condition) { const symbols = Object.getOwnPropertySymbols(condition); if (symbols.length > 0) { return symbols.every(symbol => { const value = condition[symbol]; if (symbol === Op.eq) return looseEqual(row[field], value); if (symbol === Op.ne) return !looseEqual(row[field], value); if (symbol === Op.in) return Array.isArray(value) && value.some(item => looseEqual(row[field], item)); if (symbol === Op.notIn) return Array.isArray(value) && !value.some(item => looseEqual(row[field], item)); if (symbol === Op.like || symbol === Op.iLike) return matchLike(row[field], value); if (symbol === Op.notLike) return !matchLike(row[field], value); if (symbol === Op.gt) return Number(row[field]) > Number(value); if (symbol === Op.gte) return Number(row[field]) >= Number(value); if (symbol === Op.lt) return Number(row[field]) < Number(value); if (symbol === Op.lte) return Number(row[field]) <= Number(value); if (symbol === Op.or) return Array.isArray(value) && value.some(item => matchConditionObject(row, field, item)); if (symbol === Op.and) return Array.isArray(value) && value.every(item => matchConditionObject(row, field, item)); if (symbol === Op.not) return !matchConditionObject(row, field, value); return true; }); } if (Object.keys(condition).length > 0) { return matchesWhere(row[field], condition); } return true; } /** * 判断 mock 行数据是否匹配 where 条件(支持 Op.and / Op.or / Op.not / Op.like 等)。 * @param {Object|*} row 待匹配行。 * @param {Object|Array|*} where Sequelize where 条件。 * @return {boolean} 是否匹配。 */ function matchesWhere(row, where) { if (where === null || where === undefined) return true; if (typeof where !== 'object') return looseEqual(row, where); if (Array.isArray(where)) return where.some(item => matchesWhere(row, item)); if (Array.isArray(where[Op.and]) && !where[Op.and].every(item => matchesWhere(row, item))) { return false; } if (Array.isArray(where[Op.or]) && !where[Op.or].some(item => matchesWhere(row, item))) { return false; } if (where[Op.not] !== undefined && matchesWhere(row, where[Op.not])) { return false; } return Object.entries(where).every(([ field, condition ]) => { if (condition && typeof condition === 'object' && !Array.isArray(condition)) { return matchConditionObject(row, field, condition); } if (Array.isArray(condition)) { return condition.some(item => looseEqual(row[field], item)); } return looseEqual(row[field], condition); }); } module.exports = { looseEqual, matchLike, matchesWhere, };