76 lines
2.3 KiB
JavaScript
76 lines
2.3 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.compareRows = exports.compareRow = void 0;
|
||
const tslib_1 = require("tslib");
|
||
const lodash_1 = require("./lodash");
|
||
const relation_1 = require("../store/relation");
|
||
const assert_1 = tslib_1.__importDefault(require("assert"));
|
||
/**
|
||
* 比较两行是否完全相等
|
||
* @param schema
|
||
* @param entity
|
||
* @param row1
|
||
* @param row2
|
||
* @returns 相等返回true,否则返回false
|
||
*/
|
||
function compareRow(schema, entity, row1, row2) {
|
||
const attrs1 = Object.keys(row1 || {}).filter(ele => !ele.startsWith('$$'));
|
||
const attrs2 = Object.keys(row2 || {}).filter(ele => !ele.startsWith('$$'));
|
||
if ((0, lodash_1.difference)(attrs1, attrs2).length > 0) {
|
||
return false;
|
||
}
|
||
for (const attr of attrs1) {
|
||
const rel = (0, relation_1.judgeRelation)(schema, entity, attr);
|
||
if (rel === 0) {
|
||
continue;
|
||
}
|
||
else if (rel === 1) {
|
||
if (row1[attr] !== row2[attr]) {
|
||
return false;
|
||
}
|
||
}
|
||
else if (rel === 2) {
|
||
if (!compareRow(schema, attr, row1[attr], row2[attr])) {
|
||
return false;
|
||
}
|
||
}
|
||
else if (typeof rel === 'string') {
|
||
if (!compareRow(schema, rel, row1[attr], row2[attr])) {
|
||
return false;
|
||
}
|
||
}
|
||
else {
|
||
(0, assert_1.default)(rel instanceof Array);
|
||
// 如果两组对象一样但顺序不一样怎么办?暂时应该不用考虑这种情况,因为只有前台runningTree使用(by Xc)
|
||
if (!compareRows(schema, rel[0], row1[attr], row2[attr])) {
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
exports.compareRow = compareRow;
|
||
/**
|
||
* 比较两行数据是否完全相等
|
||
* @param entity
|
||
* @param schema
|
||
* @param row1
|
||
* @param row2
|
||
*/
|
||
function compareRows(schema, entity, rows1, rows2) {
|
||
if (rows1.length !== rows2.length) {
|
||
return false;
|
||
}
|
||
let idx = 0;
|
||
while (idx < rows1.length) {
|
||
const row1 = rows1[idx];
|
||
const row2 = rows2[idx];
|
||
if (!compareRow(schema, entity, row1, row2)) {
|
||
return false;
|
||
}
|
||
idx++;
|
||
}
|
||
return true;
|
||
}
|
||
exports.compareRows = compareRows;
|