Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
32 changes: 26 additions & 6 deletions src/utils/filter.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import uniqBy from "lodash/uniqBy";

// Entity or object with path property
interface PathObject {
path: string;
}
import { PathSchema } from "../types";

type PathObject = Required<PathSchema>;

// Filter conditions
export interface FilterObject {
path?: string;
regex?: RegExp;
isDefault?: boolean;
}

/**
Expand Down Expand Up @@ -42,6 +42,24 @@ function isMultiPathSupported(
return expandedPaths.every((expandedPath) => isPathSupported(expandedPath, filterObjects));
}

function setDefaultAsFirst(
pathObjects: PathObject[],
filterObjects: FilterObject[],
): PathObject[] {
const defaultFilter = filterObjects.find((f) => f.isDefault);
if (!defaultFilter) return pathObjects;

const defaultIndex = pathObjects.findIndex((pathObj) => {
return isPathSupported(pathObj, [defaultFilter]);
});
if (defaultIndex < 0 || pathObjects.length < 2) return pathObjects;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the magic number 2? Let's explain the logic


const tmp = pathObjects[1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above for the indices

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

corrected the error and added explanatory comment

pathObjects[0] = pathObjects[defaultIndex];
pathObjects[1] = tmp;
return pathObjects;
}

interface FilterEntityListProps {
entitiesOrPaths: PathObject[]; // Array of objects defining entity path
filterObjects?: FilterObject[]; // Array of path or regular expression objects
Expand All @@ -50,13 +68,13 @@ interface FilterEntityListProps {

/**
* Filter list of entity paths or entities by paths and regular expressions.
* @return {Object[]} - filtered entity path objects or entities
* @return - filtered entity path objects or entities
*/
export function filterEntityList({
entitiesOrPaths,
filterObjects = [],
multiPathSeparator = "",
}: FilterEntityListProps) {
}: FilterEntityListProps): PathObject[] {
if (!filterObjects || !filterObjects.length) return [];
const filterObjects_ = filterObjects.map((o) => (o.regex ? { regex: new RegExp(o.regex) } : o));

Expand All @@ -69,5 +87,7 @@ export function filterEntityList({
filtered = entitiesOrPaths.filter((e) => isPathSupported(e, filterObjects_));
}

filtered = setDefaultAsFirst(filtered, filterObjects_);

return uniqBy(filtered, "path");
}
7 changes: 7 additions & 0 deletions tests/utils/filter.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ describe("entity filter", () => {
expect(filtered).to.have.deep.members(expected);
});

it("should place the default entity as first element in list", () => {
const filterObjects = [{ path: "/root/entity/b" }, { path: "/root/entity/c", isDefault: true }];
const filtered = filterEntityList({ filterObjects, entitiesOrPaths: entities });
const expectedPath ="/root/entity/c";
expect(filtered[0].path).to.be.equal(expectedPath);
});

it("should filter an entity list containing concatenated paths", () => {
const filterObjects = [{ path: "/root/entity/b" }, { path: "/root/entity/c" }];
const multiPathEntities = [
Expand Down