Skip to content

Chore/SOF-6991 #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 25 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,23 @@ 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]);
});
// minimum of two path objects needed to swap
if (defaultIndex <= 0 || pathObjects.length < 2) return pathObjects;

// swap default to first position in array
const tmp = pathObjects[0];
pathObjects[0] = pathObjects[defaultIndex];
pathObjects[defaultIndex] = 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 +67,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 +86,7 @@ export function filterEntityList({
filtered = entitiesOrPaths.filter((e) => isPathSupported(e, filterObjects_));
}

filtered = setDefaultAsFirst(filtered, filterObjects_);

return uniqBy(filtered, "path");
}
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
sortKeysDeepForObject,
stringifyObject,
} from "./object";
import { getSchemaWithDependencies, buildNamedEntitySchema } from "./schemas";
import { buildNamedEntitySchema, getSchemaWithDependencies } from "./schemas";
import { getSearchQuerySelector } from "./selector";
import {
convertArabicToRoman,
Expand Down
4 changes: 1 addition & 3 deletions src/utils/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { JSONSchema } from "@exabyte-io/esse.js/schema";
import { JSONSchema7Definition } from "json-schema";
import forEach from "lodash/forEach";
import hasProperty from "lodash/has";
import isEmpty from "lodash/isEmpty";

import { JSONSchema7Definition } from "json-schema";

import { JSONSchemasInterface } from "../JSONSchemasInterface";

export * from "@exabyte-io/esse.js/lib/js/esse/schemaUtils";
Expand Down Expand Up @@ -245,7 +244,6 @@ const buildNamedEntitiesDependencies = (entities: NamedEntity[]) => {
schemaByNamedEntityName(entity.name) ||
defaultNamedEntitySchema(entity.name);
return {

...filterForGenerativeProperties(schema),
};
}),
Expand Down
10 changes: 10 additions & 0 deletions tests/utils/filter.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ 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