Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ _**Note:** Yet to be released breaking changes appear here._
- `get`, `getAll`, `load`, `post`, `submit` via `requestUtils`
- `error`, `popup` via `guiUtils`
- The `utils` namespace has been removed. The remaining properties associated to this namespace have been moved to the `guiUtils` namespace.
- The `cellArrayUtils.filterCells` function has been removed. Use the `Array.filter` function instead.

## 0.16.0

Expand Down
36 changes: 31 additions & 5 deletions packages/core/__tests__/view/GraphDataModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,49 @@ limitations under the License.
import { describe, expect, test } from '@jest/globals';
import { Cell, GraphDataModel } from '../../src';

describe('filterCells', () => {
const model: GraphDataModel = new GraphDataModel();

test('Filter cells with a valid filter function', () => {
const cell1 = new Cell();
const cells = [cell1, new Cell()];
const filter = (cell: Cell) => cell === cell1;
const result = model.filterCells(cells, filter);
expect(result).toEqual([cell1]);
});

test('Filter cells with an empty array', () => {
const cells: Cell[] = [];
const filter = (_cell: Cell) => true;
const result = model.filterCells(cells, filter);
expect(result).toEqual([]);
});

test('Filter cells with a filter function that returns false for all cells', () => {
const cells = [new Cell(), new Cell()];
const filter = (_cell: Cell) => false;
const result = model.filterCells(cells, filter);
expect(result).toEqual([]);
});
});

describe('isLayer', () => {
const dm: GraphDataModel = new GraphDataModel();
const model: GraphDataModel = new GraphDataModel();
const root: Cell = new Cell();
dm.setRoot(root);
model.setRoot(root);

test('Child is null', () => {
expect(dm.isLayer(null)).toBe(false);
expect(model.isLayer(null)).toBe(false);
});

test('Child is not null and is not layer', () => {
expect(dm.isLayer(new Cell())).toBe(false);
expect(model.isLayer(new Cell())).toBe(false);
});

test('Child is not null and is layer', () => {
const child = new Cell();
root.children.push(child);
child.setParent(root);
expect(dm.isLayer(child)).toBeTruthy();
expect(model.isLayer(child)).toBeTruthy();
});
});
16 changes: 0 additions & 16 deletions packages/core/src/util/cellArrayUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,6 @@ import Cell from '../view/cell/Cell';
import Dictionary from './Dictionary';
import ObjectIdentity from './ObjectIdentity';

/**
* Returns the cells from the given array where the given filter function
* returns true.
*/
export const filterCells = (filter: Function) => (cells: Cell[]) => {
const result = [] as Cell[];

for (let i = 0; i < cells.length; i += 1) {
if (filter(cells[i])) {
result.push(cells[i]);
}
}

return result;
};

/**
* Returns all opposite vertices terminal for the given edges, only returning sources and/or targets as specified.
* The result is returned as an array of {@link Cell}.
Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/view/GraphDataModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ import TerminalChange from './undoable_changes/TerminalChange';
import ValueChange from './undoable_changes/ValueChange';
import VisibleChange from './undoable_changes/VisibleChange';
import Geometry from './geometry/Geometry';
import { cloneCells, filterCells } from '../util/cellArrayUtils';

import type { CellStyle, FilterFunction } from '../types';

/**
* Extends {@link EventSource} to implement a graph model. The graph model acts as
* a wrapper around the cells which are in charge of storing the actual graph
* datastructure. The model acts as a transactional wrapper with event
* data structure. The model acts as a transactional wrapper with event
* notification for all changes, whereas the cells contain the atomic
* operations for updating the actual datastructure.
* operations for updating the actual data structure.
*
* ### Layers
*
Expand Down Expand Up @@ -337,7 +335,7 @@ export class GraphDataModel extends EventSource {
}

filterCells(cells: Cell[], filter: FilterFunction) {
return filterCells(filter)(cells);
return cells.filter(filter);
}

getRoot(cell: Cell | null = null) {
Expand Down