diff --git a/src/utils/filter.ts b/src/utils/filter.ts index b09b4118..bdbb8545 100644 --- a/src/utils/filter.ts +++ b/src/utils/filter.ts @@ -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; // Filter conditions export interface FilterObject { path?: string; regex?: RegExp; + isDefault?: boolean; } /** @@ -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 @@ -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)); @@ -69,5 +86,7 @@ export function filterEntityList({ filtered = entitiesOrPaths.filter((e) => isPathSupported(e, filterObjects_)); } + filtered = setDefaultAsFirst(filtered, filterObjects_); + return uniqBy(filtered, "path"); } diff --git a/src/utils/index.ts b/src/utils/index.ts index c464eecd..6b7319df 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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, diff --git a/src/utils/schemas.ts b/src/utils/schemas.ts index 0b0f096b..68d30b07 100644 --- a/src/utils/schemas.ts +++ b/src/utils/schemas.ts @@ -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"; @@ -245,7 +244,6 @@ const buildNamedEntitiesDependencies = (entities: NamedEntity[]) => { schemaByNamedEntityName(entity.name) || defaultNamedEntitySchema(entity.name); return { - ...filterForGenerativeProperties(schema), }; }), diff --git a/tests/utils/filter.tests.ts b/tests/utils/filter.tests.ts index d4b5d221..119a188a 100644 --- a/tests/utils/filter.tests.ts +++ b/tests/utils/filter.tests.ts @@ -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 = [