'use strict'; // 语义比较:对象键顺序不计入差异(另行提示),数组顺序与所有值严格比较。 function short(v){const s=JSON.stringify(v);return s===undefined?'undefined':s.length>300?s.slice(0,300)+'…':s;} function compare(expected,actual,path='$',out={diffs:[],orderNotes:[]}){ if(Object.is(expected,actual))return out; const te=Array.isArray(expected)?'array':expected===null?'null':typeof expected,ta=Array.isArray(actual)?'array':actual===null?'null':typeof actual; if(te!==ta){out.diffs.push({path,expected:short(expected),actual:short(actual)});return out;} if(te==='array'){ const n=Math.max(expected.length,actual.length); if(expected.length!==actual.length)out.diffs.push({path:path+'.length',expected:String(expected.length),actual:String(actual.length)}); for(let i=0;ik in actual),commonActual=ka.filter(k=>k in expected); if(common.join('\u0000')!==commonActual.join('\u0000'))out.orderNotes.push(path); return out; } if(te==='string'&&expected.length>120){ // 长字符串只显示首个不同字符附近的片段 let i=0;while(ip){ const lines=[]; if(result.diffs.length){ lines.push(`${name}:发现 ${result.diffs.length} 处差异,前 ${Math.min(limit,result.diffs.length)} 处:`); for(const d of result.diffs.slice(0,limit))lines.push(` ${describe(d.path)}\n 基线:${d.expected}\n 实际:${d.actual}`); } if(result.orderNotes.length)lines.push(`${name}:${result.orderNotes.length} 处对象键顺序变化(不计为失败),例如 ${result.orderNotes.slice(0,3).join('、')}`); return lines.join('\n'); } module.exports={compare,report};