Skip to content

Commit

Permalink
Merge branch 'feature/PB-30023_Reduce-the-number-of-resources-collect…
Browse files Browse the repository at this point in the history
…ions-instantiations-while-displaying-the-suggested-resources-in-the-inform-menu' into 'develop'

PB-30023 - Reduce the number of resources collections instantiations while...

See merge request passbolt/passbolt-styleguide!1521
  • Loading branch information
cedricalfonsi committed Mar 1, 2024
2 parents 5cdde45 + 1fc3a0c commit 756df70
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "passbolt-styleguide",
"version": "4.6.0-alpha.0",
"version": "4.6.0-alpha.1",
"license": "AGPL-3.0",
"copyright": "Copyright 2023 Passbolt SA",
"description": "Passbolt styleguide contains common styling assets used by the different sites, plugin, etc.",
Expand Down
24 changes: 20 additions & 4 deletions src/shared/models/entity/abstract/entityCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,27 @@ class EntityCollection {
throw new TypeError('EntityCollection filterByPropertyValueIn expects needles to be an array.');
}

for (let currentIndex = this._items.length - 1; currentIndex >= 0; currentIndex--) {
const item = this._items[currentIndex];
this.filterByCallback(item => {
const isPropertyDefined = Object.prototype.hasOwnProperty.call(item._props, propName);
if ((excludeUndefined && !isPropertyDefined) // exclude undefined property.
|| (isPropertyDefined && !needles.includes(item._props[propName]))) { // or exclude defined property not matching the search.
return !((excludeUndefined && !isPropertyDefined) // exclude undefined property.
|| (isPropertyDefined && !needles.includes(item._props[propName]))); // or exclude defined property not matching the search.
});
}

/**
* Filter all items with a callback method.
*
* @param {function} callback The callback execute on each collection item used to filter the collection.
* @return {void} The function alters the collection itself.
* @throws TypeError if parameters are invalid
*/
filterByCallback(callback) {
if (typeof callback !== "function") {
throw new TypeError('EntityCollection filterByCallback expects callback to be a function.');
}

for (let currentIndex = this._items.length - 1; currentIndex >= 0; currentIndex--) {
if (!callback(this._items[currentIndex])) {
this._items.splice(currentIndex, 1);
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/shared/models/entity/abstract/entityCollection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,38 @@ describe("EntityCollection", () => {
expect(() => collection.filterByPropertyValueIn(42)).toThrow(TypeError);
});
});

describe("EntityCollection::filterByCallback", () => {
it("should filter out all items the callback function will return false for.", () => {
const collection = new EntityCollection();
collection.push(new TestEntity({name: 'first'}));
collection.push(new TestEntity({name: 'second'}));
collection.push(new TestEntity({name: 'first'}));
collection.push(new TestEntity({}));
collection.push(new TestEntity({name: null}));

expect.assertions(1);
collection.filterByCallback(() => false);
expect(collection).toHaveLength(0);
});

it("should filter in all items the callback function will return false for.", () => {
const collection = new EntityCollection();
collection.push(new TestEntity({name: 'first'}));
collection.push(new TestEntity({name: 'second'}));
collection.push(new TestEntity({name: 'first'}));
collection.push(new TestEntity({}));
collection.push(new TestEntity({name: null}));

expect.assertions(1);
collection.filterByCallback(() => true);
expect(collection).toHaveLength(5);
});

it("should throw an exception if the callback parameter is not a function.", () => {
const collection = new EntityCollection();
expect.assertions(1);
expect(() => collection.filterByCallback(42)).toThrow(TypeError);
});
});
});
4 changes: 2 additions & 2 deletions src/shared/models/entity/resource/resourceEntity.test.data.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ export const defaultResourceDto = (data = {}) => {
personal: false,
resource_type_id: TEST_RESOURCE_TYPE_PASSWORD_AND_DESCRIPTION,
permission: ownerPermissionDto({aco_foreign_key: id}),
// permissions: [],
// permissions: [], // Permission are not retrieved by the process storing the information in the local storage.
favorite: null,
secrets: [],
// secrets: [], // Secrets are not retrieved by the process storing the information in the local storage.
...data
};
};
Expand Down

0 comments on commit 756df70

Please sign in to comment.