格式化:Prettier统一代码风格(仅格式,无逻辑变化)
仅为 npm run format 的输出;黄金对照、旧库兼容、浏览器流程与单元测试全部通过且零差异。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3743d687e7
commit
28c64814f0
221 files changed
+116943
-26708
No files matched your search
+66
-34
@@ -1,40 +1,72 @@
|
||||
'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;i<Math.min(expected.length,actual.length);i++)compare(expected[i],actual[i],path+'['+i+']',out);
|
||||
for(let i=Math.min(expected.length,actual.length);i<n&&out.diffs.length<100000;i++)out.diffs.push({path:path+'['+i+']',expected:short(expected[i]),actual:short(actual[i])});
|
||||
return out;
|
||||
}
|
||||
if(te==='object'){
|
||||
const ke=Object.keys(expected),ka=Object.keys(actual);
|
||||
for(const k of ke)if(!(k in actual))out.diffs.push({path:path+'.'+k,expected:short(expected[k]),actual:'(缺失)'});
|
||||
for(const k of ka)if(!(k in expected))out.diffs.push({path:path+'.'+k,expected:'(缺失)',actual:short(actual[k])});
|
||||
for(const k of ke)if(k in actual)compare(expected[k],actual[k],path+'.'+k,out);
|
||||
const common=ke.filter(k=>k 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(i<expected.length&&expected[i]===actual[i])i++;const from=Math.max(0,i-60);
|
||||
out.diffs.push({path:path+` @字符${i}`,expected:(from?'…':'')+JSON.stringify(expected.slice(from,i+120)).slice(1,-1)+(i+120<expected.length?'…':''),actual:(from?'…':'')+JSON.stringify(actual.slice(from,i+120)).slice(1,-1)+(i+120<actual.length?'…':'')});return out;
|
||||
}
|
||||
out.diffs.push({path,expected:short(expected),actual:short(actual)});return out;
|
||||
function short(v) {
|
||||
const s = JSON.stringify(v);
|
||||
return s === undefined ? 'undefined' : s.length > 300 ? s.slice(0, 300) + '…' : s;
|
||||
}
|
||||
function report(name,result,limit=30,describe=p=>p){
|
||||
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}`);
|
||||
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(result.orderNotes.length)lines.push(`${name}:${result.orderNotes.length} 处对象键顺序变化(不计为失败),例如 ${result.orderNotes.slice(0,3).join('、')}`);
|
||||
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; i < Math.min(expected.length, actual.length); i++)
|
||||
compare(expected[i], actual[i], path + '[' + i + ']', out);
|
||||
for (let i = Math.min(expected.length, actual.length); i < n && out.diffs.length < 100000; i++)
|
||||
out.diffs.push({ path: path + '[' + i + ']', expected: short(expected[i]), actual: short(actual[i]) });
|
||||
return out;
|
||||
}
|
||||
if (te === 'object') {
|
||||
const ke = Object.keys(expected),
|
||||
ka = Object.keys(actual);
|
||||
for (const k of ke)
|
||||
if (!(k in actual)) out.diffs.push({ path: path + '.' + k, expected: short(expected[k]), actual: '(缺失)' });
|
||||
for (const k of ka)
|
||||
if (!(k in expected)) out.diffs.push({ path: path + '.' + k, expected: '(缺失)', actual: short(actual[k]) });
|
||||
for (const k of ke) if (k in actual) compare(expected[k], actual[k], path + '.' + k, out);
|
||||
const common = ke.filter(k => k 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 (i < expected.length && expected[i] === actual[i]) i++;
|
||||
const from = Math.max(0, i - 60);
|
||||
out.diffs.push({
|
||||
path: path + ` @字符${i}`,
|
||||
expected:
|
||||
(from ? '…' : '') +
|
||||
JSON.stringify(expected.slice(from, i + 120)).slice(1, -1) +
|
||||
(i + 120 < expected.length ? '…' : ''),
|
||||
actual:
|
||||
(from ? '…' : '') +
|
||||
JSON.stringify(actual.slice(from, i + 120)).slice(1, -1) +
|
||||
(i + 120 < actual.length ? '…' : ''),
|
||||
});
|
||||
return out;
|
||||
}
|
||||
out.diffs.push({ path, expected: short(expected), actual: short(actual) });
|
||||
return out;
|
||||
}
|
||||
function report(name, result, limit = 30, describe = p => p) {
|
||||
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};
|
||||
module.exports = { compare, report };
|
||||
Reference in new issue
Block a user