-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
99 lines (94 loc) · 4.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
;(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.JNDeepDiff = factory());
}(this, (function () {
'use strict';
//找出指定的两个对象之间的差异
function findDiff(args) {
var diffData = {};
//数组比较方式:默认根据Id字段比较
if (args.arrayComparer === undefined || (typeof (args.arrayComparer) !== 'string' && typeof (args.arrayComparer) !== 'function')) {
args.arrayComparer = 'id';
}
for (var eb in args.newData) {
//主键Id是必须的
if (eb === 'id') {
diffData[eb] = args.newData[eb];
}
//如果是对象,则说明是基础资料,辅助资料 等三值对象字段
else if (realTypeOf(args.newData[eb]) === 'object') {
if (args.oldData[eb] === undefined || args.newData[eb].id !== args.oldData[eb].id) {
diffData[eb] = args.newData[eb];
}
}
//如果是数组,则说明是明细
else if (Array.isArray(args.newData[eb])) {
if (Array.isArray(args.oldData[eb])) {
diffData[eb] = [];
for (var i = 0; i < args.newData[eb].length; i++) {
var oldEntitys = findEntity(args.oldData[eb], args.newData[eb][i], args.arrayComparer);
if (oldEntitys) {
//递归
var diffEntity = findDiff({
oldData: oldEntitys,
newData: args.newData[eb][i],
arrayComparer: args.arrayComparer
});
diffData[eb].push(diffEntity);
} else {
diffData[eb].push(args.newData[eb][i]);
}
}
} else {
diffData[eb] = args.newData[eb];
}
}
//其他类型的字段(数字,字符串,布尔)
else if (args.newData[eb] !== undefined) {
if (args.oldData[eb] === undefined || args.newData[eb] !== args.oldData[eb]) {
diffData[eb] = args.newData[eb];
}
}
}
return diffData;
}
//根据指定的主键Id在指定的实体数组中查找,如果存在则返回该实体对象,如果不存在则返回 null
function findEntity(oldEntitys, newEntity, arrayComparer) {
if (typeof (arrayComparer) === 'string') {
if (newEntity[arrayComparer] === undefined) {
throw new Error('arrayComparer 参数 ' + arrayComparer + ' 在数组对象中不存在,请检查!');
}
for (var i = 0; i < oldEntitys.length; i++) {
if (oldEntitys[i][arrayComparer] === undefined) {
throw new Error('arrayComparer 参数 ' + arrayComparer + ' 在数组对象中不存在,请检查!');
}
if (oldEntitys[i][arrayComparer] === newEntity[arrayComparer]) {
return oldEntitys[i];
}
}
} else {
return arrayComparer(oldEntitys, newEntity);
}
return null;
}
function realTypeOf(subject) {
var type = typeof subject;
if (type !== 'object') {
return type;
}
if (subject === Math) {
return 'math';
} else if (subject === null) {
return 'null';
} else if (Array.isArray(subject)) {
return 'array';
} else if (Object.prototype.toString.call(subject) === '[object Date]') {
return 'date';
} else if (typeof subject.toString === 'function' && /^\/.*\//.test(subject.toString())) {
return 'regexp';
}
return 'object';
}
return { findDiff: findDiff };
})));