viewdb 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierrc +1 -1
- package/LICENSE +22 -22
- package/README.md +416 -416
- package/jest.config.js +11 -11
- package/lib/cursor.js +84 -85
- package/lib/index.js +6 -6
- package/lib/inmemory/collection.js +112 -112
- package/lib/inmemory/store.js +19 -19
- package/lib/merger.js +62 -62
- package/lib/observe.js +53 -53
- package/lib/plugins/index.js +2 -2
- package/lib/plugins/viewdb_timestamp_plugin.js +106 -106
- package/lib/plugins/viewdb_versioning_plugin.js +93 -93
- package/package.json +37 -37
- package/test/cursor.js +101 -101
- package/test/merger.js +196 -196
- package/test/observe.js +165 -165
- package/test/plugins/viewdb_timestamp_plugin.js +159 -159
- package/test/plugins/viewdb_versioning_plugin.js +112 -112
package/jest.config.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
/** @type {import('jest').Config} */
|
|
2
|
-
module.exports = {
|
|
3
|
-
roots: ['<rootDir>/lib', '<rootDir>/test'],
|
|
4
|
-
transform: {
|
|
5
|
-
'^.+\\.js$': ['es-jest']
|
|
6
|
-
},
|
|
7
|
-
|
|
8
|
-
testRegex: '(/test/.*|(\\.|/)(test|spec))\\.js$',
|
|
9
|
-
moduleDirectories: ['node_modules', 'lib'],
|
|
10
|
-
moduleFileExtensions: ['js', 'json', 'node']
|
|
11
|
-
};
|
|
1
|
+
/** @type {import('jest').Config} */
|
|
2
|
+
module.exports = {
|
|
3
|
+
roots: ['<rootDir>/lib', '<rootDir>/test'],
|
|
4
|
+
transform: {
|
|
5
|
+
'^.+\\.js$': ['es-jest']
|
|
6
|
+
},
|
|
7
|
+
|
|
8
|
+
testRegex: '(/test/.*|(\\.|/)(test|spec))\\.js$',
|
|
9
|
+
moduleDirectories: ['node_modules', 'lib'],
|
|
10
|
+
moduleFileExtensions: ['js', 'json', 'node']
|
|
11
|
+
};
|
package/lib/cursor.js
CHANGED
|
@@ -1,85 +1,84 @@
|
|
|
1
|
-
var _ = require('lodash');
|
|
2
|
-
var Observe = require('./observe');
|
|
3
|
-
|
|
4
|
-
var Cursor = function (collection, query, options, getDocuments) {
|
|
5
|
-
this._collection = collection;
|
|
6
|
-
this._query = query;
|
|
7
|
-
this._options = options;
|
|
8
|
-
this._getDocuments = getDocuments;
|
|
9
|
-
this._isObserving = false;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
Cursor.prototype.forEach = function (callback) {
|
|
13
|
-
this._getDocuments(this._query, function (err, result) {
|
|
14
|
-
_.forEach(result, function () {
|
|
15
|
-
callback(result);
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
Cursor.prototype.toArray = function (callback) {
|
|
21
|
-
this._getDocuments(this._query, callback);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
Cursor.prototype.observe = function (options) {
|
|
25
|
-
this._isObserving = true;
|
|
26
|
-
return new Observe(this._query, this._options, this._collection, options);
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
Cursor.prototype.updateQuery = function (query) {
|
|
30
|
-
this._query.query = query;
|
|
31
|
-
this._refresh();
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
Cursor.prototype.skip = function (skip) {
|
|
35
|
-
this._query.skip = skip;
|
|
36
|
-
if (this._isObserving) {
|
|
37
|
-
this._refresh();
|
|
38
|
-
}
|
|
39
|
-
return this;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
Cursor.prototype.limit = function (limit) {
|
|
43
|
-
this._query.limit = limit;
|
|
44
|
-
if (this._isObserving) {
|
|
45
|
-
this._refresh();
|
|
46
|
-
}
|
|
47
|
-
return this;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
Cursor.prototype.sort = function (sort) {
|
|
51
|
-
this._query.sort = sort;
|
|
52
|
-
if (this._isObserving) {
|
|
53
|
-
this._refresh();
|
|
54
|
-
}
|
|
55
|
-
return this;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
Cursor.prototype._refresh = function () {
|
|
59
|
-
this._collection.emit('change', {});
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
Cursor.prototype.rewind = function (options) {
|
|
63
|
-
//NOOP
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
Cursor.prototype.count = function (
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
module.exports = Cursor;
|
|
1
|
+
var _ = require('lodash');
|
|
2
|
+
var Observe = require('./observe');
|
|
3
|
+
|
|
4
|
+
var Cursor = function (collection, query, options, getDocuments) {
|
|
5
|
+
this._collection = collection;
|
|
6
|
+
this._query = query;
|
|
7
|
+
this._options = options;
|
|
8
|
+
this._getDocuments = getDocuments;
|
|
9
|
+
this._isObserving = false;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
Cursor.prototype.forEach = function (callback) {
|
|
13
|
+
this._getDocuments(this._query, function (err, result) {
|
|
14
|
+
_.forEach(result, function () {
|
|
15
|
+
callback(result);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
Cursor.prototype.toArray = function (callback) {
|
|
21
|
+
this._getDocuments(this._query, callback);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
Cursor.prototype.observe = function (options) {
|
|
25
|
+
this._isObserving = true;
|
|
26
|
+
return new Observe(this._query, this._options, this._collection, options);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
Cursor.prototype.updateQuery = function (query) {
|
|
30
|
+
this._query.query = query;
|
|
31
|
+
this._refresh();
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
Cursor.prototype.skip = function (skip) {
|
|
35
|
+
this._query.skip = skip;
|
|
36
|
+
if (this._isObserving) {
|
|
37
|
+
this._refresh();
|
|
38
|
+
}
|
|
39
|
+
return this;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
Cursor.prototype.limit = function (limit) {
|
|
43
|
+
this._query.limit = limit;
|
|
44
|
+
if (this._isObserving) {
|
|
45
|
+
this._refresh();
|
|
46
|
+
}
|
|
47
|
+
return this;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
Cursor.prototype.sort = function (sort) {
|
|
51
|
+
this._query.sort = sort;
|
|
52
|
+
if (this._isObserving) {
|
|
53
|
+
this._refresh();
|
|
54
|
+
}
|
|
55
|
+
return this;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
Cursor.prototype._refresh = function () {
|
|
59
|
+
this._collection.emit('change', {});
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
Cursor.prototype.rewind = function (options) {
|
|
63
|
+
//NOOP
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
Cursor.prototype.count = function (callback) {
|
|
67
|
+
var query = { query: this._query.query };
|
|
68
|
+
if (this._query.skip) {
|
|
69
|
+
query.skip = this._query.skip;
|
|
70
|
+
}
|
|
71
|
+
if (this._query.limit) {
|
|
72
|
+
query.limit = this._query.limit;
|
|
73
|
+
}
|
|
74
|
+
this._getDocuments(query, function (err, res) {
|
|
75
|
+
callback(err, res && res.length);
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
Cursor.prototype.close = function (callback) {
|
|
80
|
+
//NOOP
|
|
81
|
+
callback();
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
module.exports = Cursor;
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
module.exports = require('./viewdb');
|
|
2
|
-
|
|
3
|
-
module.exports.Cursor = require('./cursor');
|
|
4
|
-
module.exports.Observer = require('./observe');
|
|
5
|
-
module.exports.merge = require('./merger');
|
|
6
|
-
module.exports.plugins = require('./plugins');
|
|
1
|
+
module.exports = require('./viewdb');
|
|
2
|
+
|
|
3
|
+
module.exports.Cursor = require('./cursor');
|
|
4
|
+
module.exports.Observer = require('./observe');
|
|
5
|
+
module.exports.merge = require('./merger');
|
|
6
|
+
module.exports.plugins = require('./plugins');
|
|
@@ -1,112 +1,112 @@
|
|
|
1
|
-
var _ = require('lodash');
|
|
2
|
-
var { v4: uuid } = require('uuid');
|
|
3
|
-
|
|
4
|
-
var EventEmitter = require('events').EventEmitter;
|
|
5
|
-
var util = require('util');
|
|
6
|
-
|
|
7
|
-
var Kuery = require('kuery');
|
|
8
|
-
|
|
9
|
-
var Cursor = require('../cursor');
|
|
10
|
-
|
|
11
|
-
var Collection = function (collectionName) {
|
|
12
|
-
EventEmitter.call(this);
|
|
13
|
-
|
|
14
|
-
this._documents = [];
|
|
15
|
-
this._name = collectionName;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
util.inherits(Collection, EventEmitter);
|
|
19
|
-
|
|
20
|
-
Collection.prototype.count = function (callback) {
|
|
21
|
-
callback(null, this._documents.length);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
Collection.prototype._write = function (op, documents, options, callback) {
|
|
25
|
-
if (_.isFunction(options)) {
|
|
26
|
-
callback = options;
|
|
27
|
-
}
|
|
28
|
-
if (!_.isArray(documents)) {
|
|
29
|
-
documents = [documents];
|
|
30
|
-
}
|
|
31
|
-
var self = this;
|
|
32
|
-
for (var i = 0; i < documents.length; i++) {
|
|
33
|
-
var document = documents[i];
|
|
34
|
-
if (!_.isObject(document)) {
|
|
35
|
-
return callback(new Error('Document must be object'));
|
|
36
|
-
}
|
|
37
|
-
if (!_.has(document, '_id')) {
|
|
38
|
-
document['_id'] = document['id'] || uuid();
|
|
39
|
-
}
|
|
40
|
-
var idx = _.findIndex(self._documents, { _id: document['_id'] });
|
|
41
|
-
if (op === 'insert' && idx >= 0) {
|
|
42
|
-
return callback(new Error('Unique constraint!'));
|
|
43
|
-
}
|
|
44
|
-
// not stored before
|
|
45
|
-
if (idx === -1) {
|
|
46
|
-
self._documents.push(document);
|
|
47
|
-
} else {
|
|
48
|
-
this._documents[idx] = document;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
this.emit('change', documents);
|
|
52
|
-
if (callback) {
|
|
53
|
-
callback(null, documents);
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
Collection.prototype.insert = function (documents, options, callback) {
|
|
58
|
-
return this._write('insert', documents, options, callback);
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
Collection.prototype.save = function (documents, options, callback) {
|
|
62
|
-
return this._write('save', documents, options, callback);
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
Collection.prototype.drop = function (callback) {
|
|
66
|
-
this._documents = [];
|
|
67
|
-
|
|
68
|
-
if (callback) {
|
|
69
|
-
callback(null);
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
Collection.prototype.find = function (query, options) {
|
|
74
|
-
return new Cursor(this, { query: query }, options, this._getDocuments.bind(this));
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
Collection.prototype.remove = function (query, options, callback) {
|
|
78
|
-
var q = new Kuery(query);
|
|
79
|
-
var documents = q.find(this._documents);
|
|
80
|
-
this._documents = _.pullAll(this._documents, documents);
|
|
81
|
-
|
|
82
|
-
process.nextTick(function () {
|
|
83
|
-
callback(null);
|
|
84
|
-
});
|
|
85
|
-
};
|
|
86
|
-
Collection.prototype.ensureIndex = function () {
|
|
87
|
-
throw new Error('ensureIndex not supported!');
|
|
88
|
-
};
|
|
89
|
-
Collection.prototype.createIndex = function () {
|
|
90
|
-
throw new Error('createIndex not supported!');
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
Collection.prototype._getDocuments = function (queryObject, callback) {
|
|
94
|
-
var self = this;
|
|
95
|
-
var query = queryObject.query || queryObject;
|
|
96
|
-
var q = new Kuery(query);
|
|
97
|
-
if (queryObject.sort) {
|
|
98
|
-
q.sort(queryObject.sort);
|
|
99
|
-
}
|
|
100
|
-
if (queryObject.skip) {
|
|
101
|
-
q.skip(queryObject.skip);
|
|
102
|
-
}
|
|
103
|
-
if (queryObject.limit) {
|
|
104
|
-
q.limit(queryObject.limit);
|
|
105
|
-
}
|
|
106
|
-
var documents = q.find(self._documents);
|
|
107
|
-
process.nextTick(function () {
|
|
108
|
-
callback(null, _.cloneDeep(documents));
|
|
109
|
-
});
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
module.exports = Collection;
|
|
1
|
+
var _ = require('lodash');
|
|
2
|
+
var { v4: uuid } = require('uuid');
|
|
3
|
+
|
|
4
|
+
var EventEmitter = require('events').EventEmitter;
|
|
5
|
+
var util = require('util');
|
|
6
|
+
|
|
7
|
+
var Kuery = require('kuery');
|
|
8
|
+
|
|
9
|
+
var Cursor = require('../cursor');
|
|
10
|
+
|
|
11
|
+
var Collection = function (collectionName) {
|
|
12
|
+
EventEmitter.call(this);
|
|
13
|
+
|
|
14
|
+
this._documents = [];
|
|
15
|
+
this._name = collectionName;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
util.inherits(Collection, EventEmitter);
|
|
19
|
+
|
|
20
|
+
Collection.prototype.count = function (callback) {
|
|
21
|
+
callback(null, this._documents.length);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
Collection.prototype._write = function (op, documents, options, callback) {
|
|
25
|
+
if (_.isFunction(options)) {
|
|
26
|
+
callback = options;
|
|
27
|
+
}
|
|
28
|
+
if (!_.isArray(documents)) {
|
|
29
|
+
documents = [documents];
|
|
30
|
+
}
|
|
31
|
+
var self = this;
|
|
32
|
+
for (var i = 0; i < documents.length; i++) {
|
|
33
|
+
var document = documents[i];
|
|
34
|
+
if (!_.isObject(document)) {
|
|
35
|
+
return callback(new Error('Document must be object'));
|
|
36
|
+
}
|
|
37
|
+
if (!_.has(document, '_id')) {
|
|
38
|
+
document['_id'] = document['id'] || uuid();
|
|
39
|
+
}
|
|
40
|
+
var idx = _.findIndex(self._documents, { _id: document['_id'] });
|
|
41
|
+
if (op === 'insert' && idx >= 0) {
|
|
42
|
+
return callback(new Error('Unique constraint!'));
|
|
43
|
+
}
|
|
44
|
+
// not stored before
|
|
45
|
+
if (idx === -1) {
|
|
46
|
+
self._documents.push(document);
|
|
47
|
+
} else {
|
|
48
|
+
this._documents[idx] = document;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
this.emit('change', documents);
|
|
52
|
+
if (callback) {
|
|
53
|
+
callback(null, documents);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
Collection.prototype.insert = function (documents, options, callback) {
|
|
58
|
+
return this._write('insert', documents, options, callback);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
Collection.prototype.save = function (documents, options, callback) {
|
|
62
|
+
return this._write('save', documents, options, callback);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
Collection.prototype.drop = function (callback) {
|
|
66
|
+
this._documents = [];
|
|
67
|
+
|
|
68
|
+
if (callback) {
|
|
69
|
+
callback(null);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
Collection.prototype.find = function (query, options) {
|
|
74
|
+
return new Cursor(this, { query: query }, options, this._getDocuments.bind(this));
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
Collection.prototype.remove = function (query, options, callback) {
|
|
78
|
+
var q = new Kuery(query);
|
|
79
|
+
var documents = q.find(this._documents);
|
|
80
|
+
this._documents = _.pullAll(this._documents, documents);
|
|
81
|
+
|
|
82
|
+
process.nextTick(function () {
|
|
83
|
+
callback(null);
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
Collection.prototype.ensureIndex = function () {
|
|
87
|
+
throw new Error('ensureIndex not supported!');
|
|
88
|
+
};
|
|
89
|
+
Collection.prototype.createIndex = function () {
|
|
90
|
+
throw new Error('createIndex not supported!');
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
Collection.prototype._getDocuments = function (queryObject, callback) {
|
|
94
|
+
var self = this;
|
|
95
|
+
var query = queryObject.query || queryObject;
|
|
96
|
+
var q = new Kuery(query);
|
|
97
|
+
if (queryObject.sort) {
|
|
98
|
+
q.sort(queryObject.sort);
|
|
99
|
+
}
|
|
100
|
+
if (queryObject.skip) {
|
|
101
|
+
q.skip(queryObject.skip);
|
|
102
|
+
}
|
|
103
|
+
if (queryObject.limit) {
|
|
104
|
+
q.limit(queryObject.limit);
|
|
105
|
+
}
|
|
106
|
+
var documents = q.find(self._documents);
|
|
107
|
+
process.nextTick(function () {
|
|
108
|
+
callback(null, _.cloneDeep(documents));
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
module.exports = Collection;
|
package/lib/inmemory/store.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
var Collection = require('./collection');
|
|
2
|
-
|
|
3
|
-
var Store = function () {
|
|
4
|
-
this._collections = {};
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
Store.prototype.collection = function (collectionName, callback) {
|
|
8
|
-
var coll = this._collections[collectionName];
|
|
9
|
-
if (coll === undefined) {
|
|
10
|
-
coll = new Collection(collectionName);
|
|
11
|
-
this._collections[collectionName] = coll;
|
|
12
|
-
}
|
|
13
|
-
if (callback) {
|
|
14
|
-
callback(coll);
|
|
15
|
-
}
|
|
16
|
-
return coll;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
module.exports = Store;
|
|
1
|
+
var Collection = require('./collection');
|
|
2
|
+
|
|
3
|
+
var Store = function () {
|
|
4
|
+
this._collections = {};
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
Store.prototype.collection = function (collectionName, callback) {
|
|
8
|
+
var coll = this._collections[collectionName];
|
|
9
|
+
if (coll === undefined) {
|
|
10
|
+
coll = new Collection(collectionName);
|
|
11
|
+
this._collections[collectionName] = coll;
|
|
12
|
+
}
|
|
13
|
+
if (callback) {
|
|
14
|
+
callback(coll);
|
|
15
|
+
}
|
|
16
|
+
return coll;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
module.exports = Store;
|
package/lib/merger.js
CHANGED
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
var _ = require('lodash');
|
|
2
|
-
|
|
3
|
-
function contains(list, element, comparator) {
|
|
4
|
-
for (var i in list) {
|
|
5
|
-
var n = list[i];
|
|
6
|
-
if (comparator(element, n)) {
|
|
7
|
-
return n;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
return undefined;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function merge(asis, tobe, options) {
|
|
14
|
-
options = options || {};
|
|
15
|
-
var comparator = options.comparator || _.isEqual;
|
|
16
|
-
var comparatorId = options.comparatorId || comparator;
|
|
17
|
-
var list = _.slice(asis);
|
|
18
|
-
//check removed
|
|
19
|
-
_.forEach(asis, function (e) {
|
|
20
|
-
var found = contains(tobe, e, comparatorId);
|
|
21
|
-
if (found === undefined) {
|
|
22
|
-
var index = list.indexOf(e);
|
|
23
|
-
list.splice(index, 1);
|
|
24
|
-
if (options.removed) {
|
|
25
|
-
options.removed(e, index);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
var indexInNew = -1;
|
|
30
|
-
_.forEach(tobe, function (e) {
|
|
31
|
-
indexInNew++;
|
|
32
|
-
var found = contains(list, e, comparatorId);
|
|
33
|
-
//added
|
|
34
|
-
if (found === undefined) {
|
|
35
|
-
list.splice(indexInNew, 0, e);
|
|
36
|
-
if (options.added) {
|
|
37
|
-
options.added(e, indexInNew);
|
|
38
|
-
}
|
|
39
|
-
} else {
|
|
40
|
-
//existed before
|
|
41
|
-
var indexInOld = list.indexOf(found);
|
|
42
|
-
if (indexInOld !== indexInNew) {
|
|
43
|
-
//remove
|
|
44
|
-
list.splice(indexInOld, 1);
|
|
45
|
-
//add
|
|
46
|
-
list.splice(indexInNew, 0, e);
|
|
47
|
-
if (options.moved) {
|
|
48
|
-
options.moved(e, indexInOld, indexInNew);
|
|
49
|
-
}
|
|
50
|
-
} //else not moved
|
|
51
|
-
if (!comparator(found, e)) {
|
|
52
|
-
list[indexInNew] = e;
|
|
53
|
-
if (options.changed) {
|
|
54
|
-
options.changed(found, e, indexInNew);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
return list;
|
|
60
|
-
//list = _.exclude()v
|
|
61
|
-
}
|
|
62
|
-
module.exports = merge;
|
|
1
|
+
var _ = require('lodash');
|
|
2
|
+
|
|
3
|
+
function contains(list, element, comparator) {
|
|
4
|
+
for (var i in list) {
|
|
5
|
+
var n = list[i];
|
|
6
|
+
if (comparator(element, n)) {
|
|
7
|
+
return n;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function merge(asis, tobe, options) {
|
|
14
|
+
options = options || {};
|
|
15
|
+
var comparator = options.comparator || _.isEqual;
|
|
16
|
+
var comparatorId = options.comparatorId || comparator;
|
|
17
|
+
var list = _.slice(asis);
|
|
18
|
+
//check removed
|
|
19
|
+
_.forEach(asis, function (e) {
|
|
20
|
+
var found = contains(tobe, e, comparatorId);
|
|
21
|
+
if (found === undefined) {
|
|
22
|
+
var index = list.indexOf(e);
|
|
23
|
+
list.splice(index, 1);
|
|
24
|
+
if (options.removed) {
|
|
25
|
+
options.removed(e, index);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
var indexInNew = -1;
|
|
30
|
+
_.forEach(tobe, function (e) {
|
|
31
|
+
indexInNew++;
|
|
32
|
+
var found = contains(list, e, comparatorId);
|
|
33
|
+
//added
|
|
34
|
+
if (found === undefined) {
|
|
35
|
+
list.splice(indexInNew, 0, e);
|
|
36
|
+
if (options.added) {
|
|
37
|
+
options.added(e, indexInNew);
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
//existed before
|
|
41
|
+
var indexInOld = list.indexOf(found);
|
|
42
|
+
if (indexInOld !== indexInNew) {
|
|
43
|
+
//remove
|
|
44
|
+
list.splice(indexInOld, 1);
|
|
45
|
+
//add
|
|
46
|
+
list.splice(indexInNew, 0, e);
|
|
47
|
+
if (options.moved) {
|
|
48
|
+
options.moved(e, indexInOld, indexInNew);
|
|
49
|
+
}
|
|
50
|
+
} //else not moved
|
|
51
|
+
if (!comparator(found, e)) {
|
|
52
|
+
list[indexInNew] = e;
|
|
53
|
+
if (options.changed) {
|
|
54
|
+
options.changed(found, e, indexInNew);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return list;
|
|
60
|
+
//list = _.exclude()v
|
|
61
|
+
}
|
|
62
|
+
module.exports = merge;
|