-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
259 lines (226 loc) · 6.04 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
'use strict';
const md5 = require('md5');
////////////////////////////////////////////////////////////////////////////////
// Private (service) functions
function _jsonStringifySync(item) {
return JSON.stringify(item)
}
async function _jsonStringifyAsync(item) {
return JSON.stringify(item)
}
function _jsonParseSync(str) {
return JSON.parse(str)
}
async function _jsonParseAsync(str) {
return JSON.parse(str)
}
function _deepCopySync(item) {
let copy = null;
try {
let str = _jsonStringifySync(item);
copy = _jsonParseSync(str);
} catch (e) {
copy = item; // item has circ refs, do not use deep copy
}
return copy;
}
async function _deepCopyAsync(item) {
let copy = null;
try {
let str = await _jsonStringifyAsync(item);
copy = await _jsonParseAsync(str);
} catch (e) {
copy = item; // item has circ refs, do not use deep copy
}
return copy;
}
function _hashString(str) {
return md5(str)
}
function _hashObjectSync(obj) {
let str;
try {
str = _jsonStringifySync(obj);
} catch (error) {
str = '0'; // obj has cyclical refs. bad tmp solution
}
return _hashString(str);
}
async function _hashObjectAsync(obj) {
let str;
try {
str = await _jsonStringifyAsync(obj);
} catch (error) {
str = '0'; // obj has cyclical refs. bad tmp solution
}
return _hashString(str);
}
////////////////////////////////////////////////////////////////////////////////
// Public (export) functions
/**
* Async merge two array of objects
*
* @param arrOriginal - original array
* @param arrUpdate - array for update
* @param identifier - field or function for object matching (optional)
* @returns {Promise<*[]>}
*/
async function arrMergeAsync(arrOriginal, arrUpdate, identifier) {
const PROD = process.env.NODE_ENV !== 'develop';
// check input
if (!arrOriginal) {
arrOriginal = [];
}
if (!arrUpdate) {
arrUpdate = [];
}
if (!Array.isArray(arrOriginal)) {
arrOriginal = [arrOriginal];
}
if (!Array.isArray(arrUpdate)) {
arrUpdate = [arrUpdate];
}
// init
let arrResult = [].concat(await _deepCopyAsync(arrOriginal));
let mapOriginal = {};
let identifierAbsent = !identifier;
let identifierIsFunc = typeof identifier === 'function';
let identifierIsStr = false;
if (!identifierAbsent && !identifierIsFunc) {
identifier = identifier.toString();
identifierIsStr = true;
}
// scan Original
for (let index = 0; index < arrOriginal.length; index++) {
let item = arrOriginal[index];
let hash = await _hashObjectAsync(item);
let key = hash; // default case identifierAbsent
if (identifierIsStr) {
key = item[identifier];
}
if (identifierIsFunc) {
try {
key = identifier(item);
} catch (error) {
if (!PROD) {
console.error(error.message);
}
}
}
mapOriginal[key] = {key, hash, index};
}
// scan arrUpdate
let indexNext = arrResult.length;
for (let item of arrUpdate) {
let hash = await _hashObjectAsync(item);
let key = hash; // default case identifierAbsent
if (identifierIsStr) {
key = item[identifier];
}
if (identifierIsFunc) {
try {
key = identifier(item);
} catch (error) {
if (!PROD) {
console.error(error.message);
}
}
}
if (mapOriginal[key]) { // item exists in arrOriginal
if (mapOriginal[key].hash !== hash) { // need update
let index = mapOriginal[key].index;
arrResult[index] = await _deepCopyAsync(item);
mapOriginal[key] = {key, hash, index};
}
} else { // need append new item
let index = indexNext;
indexNext = arrResult.push(await _deepCopyAsync(item));
mapOriginal[key] = {key, hash, index};
}
}
return arrResult;
}
/**
* Sync merge two array of objects
*
* @param arrOriginal - original array
* @param arrUpdate - array for update
* @param identifier - field or function for object matching (optional)
* @returns {*[]}
*/
function arrMergeSync(arrOriginal, arrUpdate, identifier) {
const PROD = process.env.NODE_ENV !== 'develop';
// check input
if (!arrOriginal) {
arrOriginal = [];
}
if (!arrUpdate) {
arrUpdate = [];
}
if (!Array.isArray(arrOriginal)) {
arrOriginal = [arrOriginal];
}
if (!Array.isArray(arrUpdate)) {
arrUpdate = [arrUpdate];
}
// init
let arrResult = [].concat(_deepCopySync(arrOriginal));
let mapOriginal = {};
let identifierAbsent = !identifier;
let identifierIsFunc = typeof identifier === 'function';
let identifierIsStr = false;
if (!identifierAbsent && !identifierIsFunc) {
identifier = identifier.toString();
identifierIsStr = true;
}
// scan Original
for (let index = 0; index < arrOriginal.length; index++) {
let item = arrOriginal[index];
let hash = _hashObjectSync(item);
let key = hash; // default case identifierAbsent
if (identifierIsStr) {
key = item[identifier];
}
if (identifierIsFunc) {
try {
key = identifier(item);
} catch (error) {
if (!PROD) {
console.error(error.message);
}
}
}
mapOriginal[key] = {key, hash, index};
}
// scan arrUpdate
let indexNext = arrResult.length;
for (let item of arrUpdate) {
let hash = _hashObjectSync(item);
let key = hash; // default case identifierAbsent
if (identifierIsStr) {
key = item[identifier];
}
if (identifierIsFunc) {
try {
key = identifier(item);
} catch (error) {
if (!PROD) {
console.error(error.message);
}
}
}
if (mapOriginal[key]) { // item exists in arrOriginal
if (mapOriginal[key].hash !== hash) { // need update
let index = mapOriginal[key].index;
arrResult[index] = _deepCopySync(item);
mapOriginal[key] = {key, hash, index};
}
} else { // need append new item
let index = indexNext;
indexNext = arrResult.push(_deepCopySync(item));
mapOriginal[key] = {key, hash, index};
}
}
return arrResult;
}
module.exports = {arrMergeAsync, arrMergeSync};