Browse Source

[frontend] Fix Array.remove/removeBy deleting the last element when the value is not found (#1371)

Co-authored-by: claude[bot] <claude[bot]@users.noreply.github.com>
pull/1382/head
github-actions[bot] 2 weeks ago
committed by GitHub
parent
commit
0b41444337
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 16
      frontend/src/app/framework/utils/array-extensions.spec.ts
  2. 12
      frontend/src/app/framework/utils/array-extensions.ts
  3. 8
      frontend/src/app/shared/state/schemas.state.spec.ts

16
frontend/src/app/framework/utils/array-extensions.spec.ts

@ -62,6 +62,14 @@ describe('ArrayExtensions', () => {
expect(array_1).toBe(array_0);
expect(array_1).toEqual([1, 3]);
});
it('should return same array if value is not found', () => {
const array_0 = [1, 2, 3];
const array_1 = array_0.remove(4);
expect(array_1).toBe(array_0);
expect(array_1).toEqual([1, 2, 3]);
});
});
describe('removedBy', () => {
@ -96,6 +104,14 @@ describe('ArrayExtensions', () => {
expect(array_1).toBe(array_0);
expect(array_1).toEqual([{ id: 2 }]);
});
it('should return same array if value is not found', () => {
const array_0 = [{ id: 1 }, { id: 2 }];
const array_1 = array_0.removeBy('id', { id: 3 });
expect(array_1).toBe(array_0);
expect(array_1).toEqual([{ id: 1 }, { id: 2 }]);
});
});
it('should sort by value', () => {

12
frontend/src/app/framework/utils/array-extensions.ts

@ -108,7 +108,12 @@ Array.prototype.removeBy = function<T>(field: keyof T, value: T) {
return self;
}
self.splice(self.findIndex(x => x[field] === value[field]), 1);
const index = self.findIndex(x => x[field] === value[field]);
// Splice with -1 would remove the last element.
if (index >= 0) {
self.splice(index, 1);
}
return self;
};
@ -132,7 +137,10 @@ Array.prototype.remove = function<T>(value?: T) {
const index = self.indexOf(value);
self.splice(index, 1);
// Splice with -1 would remove the last element.
if (index >= 0) {
self.splice(index, 1);
}
return self;
};

8
frontend/src/app/shared/state/schemas.state.spec.ts

@ -132,6 +132,14 @@ describe('SchemasState', () => {
expect([...schemasState.snapshot.addedCategories]).toEqualIgnoringProps([]);
});
it('should not remove other category if removed category was never added', () => {
schemasState.addCategory('A/B');
schemasState.addCategory('schema-category3');
schemasState.removeCategory('A');
expect([...schemasState.snapshot.addedCategories]).toEqualIgnoringProps(['A/B', 'schema-category3']);
});
it('should return schema on select and reload if already loaded', () => {
schemasService.setup(x => x.getSchema(app, schema1.name))
.returns(() => of(schema1)).verifiable(Times.exactly(2));

Loading…
Cancel
Save