(core) Moving client and common tests to core

Summary:
- Moved /test/client and /test/common to core.
- Moved two files (CircularArray and RecentItems) from app/common to core/app/common.
- Moved resetOrg test to gen-server.
- `testrun.sh` is now invoking common and client test from core.
- Added missing packages to core's package.json (and revealed underscore as it is used in the main app).
- Removed Coord.js as it is not used anywhere.

Test Plan: Existing tests

Reviewers: paulfitz

Reviewed By: paulfitz

Subscribers: paulfitz

Differential Revision: https://phab.getgrist.com/D3590
This commit is contained in:
Jarosław Sadziński
2022-08-18 23:08:39 +02:00
parent e06f0bc1d8
commit a52d56f613
78 changed files with 11700 additions and 7 deletions

View File

@@ -0,0 +1,44 @@
/**
* Array-like data structure that lets you push elements to it, but holds only the last N of them.
*/
function CircularArray(maxLength) {
this.maxLength = maxLength;
this._data = [];
this._offset = 0;
}
/**
* @property {Number} - the number of items in the CircularArray.
*/
Object.defineProperty(CircularArray.prototype, "length", {
get: function() { return this._data.length; }
});
/**
* @param {Number} index - An index to fetch, between 0 and length - 1.
* @returns {Object} The item at the given index.
*/
CircularArray.prototype.get = function(index) {
return this._data[(this._offset + index) % this.maxLength];
};
/**
* @param {Object} item - An item to push onto the end of the CircularArray.
*/
CircularArray.prototype.push = function(item) {
if (this._data.length < this.maxLength) {
this._data.push(item);
} else {
this._data[this._offset] = item;
this._offset = (this._offset + 1) % this.maxLength;
}
};
/**
* Returns the entire content of CircularArray as a plain array.
*/
CircularArray.prototype.getArray = function() {
return this._data.slice(this._offset).concat(this._data.slice(0, this._offset));
};
module.exports = CircularArray;

50
app/common/RecentItems.js Normal file
View File

@@ -0,0 +1,50 @@
/**
* RecentItems maintains a list of maxCount most recently added items.
* If an existing item is added, it is moved to the end of the list.
*
* @constructor
* @param {Int} options.maxCount - The maximum number of objects that will be maintained.
* @param {Function} options.keyFunc - Function that returns a key identifying an item;
* If an item is added with an existing key, it replaces the previous item in the list but is
* moved to the end of the list. Defaults to the identity function.
* @param {Array} options.intialItems - A list of items to populate the list on initialization
*/
class RecentItems {
constructor(options) {
this._items = new Map();
this._maxCount = options.maxCount || 0;
this._keyFunc = options.keyFunc || (item => item);
if (options.intialItems) this.addItems(options.intialItems);
}
addItem(item) {
// Map maintains entries in the order of insertion, so by deleting and reinserting an entry,
// we move it to the end of the list.
this._items.delete(this._keyFunc(item));
this._items.set(this._keyFunc(item), item);
// Now that the list is correctly ordered we may need to remove the oldest entry which is
// the first item.
if (this._items.size > this._maxCount && this._maxCount !== 0) {
this._items.delete(this._items.keys().next().value);
}
}
addItems(items) {
items.forEach(item => {
this.addItem(item);
});
}
/**
* Returns a list of the current items in the map. The list is starts with oldest
* added item and ends with the most recently inserted.
*
* @returns {Array} A list of items.
*/
listItems() {
return Array.from(this._items.values());
}
}
module.exports = RecentItems;