From f4590f1faece3ea903d2639b817a1ca36809f4fa Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Mon, 23 Oct 2023 10:59:23 -0700 Subject: [PATCH 1/6] update: place default entity in first position --- src/utils/filter.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/utils/filter.ts b/src/utils/filter.ts index b09b4118..73c6d7c0 100644 --- a/src/utils/filter.ts +++ b/src/utils/filter.ts @@ -1,4 +1,6 @@ import uniqBy from "lodash/uniqBy"; +import { filter } from "underscore"; +import { pathToFileURL } from "url"; // Entity or object with path property interface PathObject { @@ -9,6 +11,7 @@ interface PathObject { export interface FilterObject { path?: string; regex?: RegExp; + isDefault?: boolean; } /** @@ -42,6 +45,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; + + const tmp = pathObjects[1]; + 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 @@ -50,13 +71,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 +90,7 @@ export function filterEntityList({ filtered = entitiesOrPaths.filter((e) => isPathSupported(e, filterObjects_)); } + filtered = setDefaultAsFirst(filtered, filterObjects_); + return uniqBy(filtered, "path"); } From 9df4b770dfb4660e34b3e3fcc57d78da6d18fa2a Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Mon, 23 Oct 2023 11:00:01 -0700 Subject: [PATCH 2/6] test: filter entity with default --- tests/utils/filter.tests.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/utils/filter.tests.ts b/tests/utils/filter.tests.ts index d4b5d221..16093808 100644 --- a/tests/utils/filter.tests.ts +++ b/tests/utils/filter.tests.ts @@ -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 = [ From 3cb3bba18f318581b2668f61972d8f3ef0caa5b0 Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Thu, 26 Oct 2023 12:15:33 -0700 Subject: [PATCH 3/6] chore: remove unused imports --- src/utils/filter.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/utils/filter.ts b/src/utils/filter.ts index 73c6d7c0..6b43d9f8 100644 --- a/src/utils/filter.ts +++ b/src/utils/filter.ts @@ -1,6 +1,4 @@ import uniqBy from "lodash/uniqBy"; -import { filter } from "underscore"; -import { pathToFileURL } from "url"; // Entity or object with path property interface PathObject { From b94c987f98d620790466cefc04ba354395a1c62d Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Thu, 26 Oct 2023 17:03:10 -0700 Subject: [PATCH 4/6] chore: derive path object from esse --- src/utils/filter.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/utils/filter.ts b/src/utils/filter.ts index 6b43d9f8..4245e822 100644 --- a/src/utils/filter.ts +++ b/src/utils/filter.ts @@ -1,9 +1,8 @@ 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 { From 1d4f2e17721ce573c05f5f3e13990fd810180589 Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Fri, 27 Oct 2023 17:34:32 -0700 Subject: [PATCH 5/6] fix: swap default in array --- src/utils/filter.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/utils/filter.ts b/src/utils/filter.ts index 4245e822..bdbb8545 100644 --- a/src/utils/filter.ts +++ b/src/utils/filter.ts @@ -42,21 +42,20 @@ function isMultiPathSupported( return expandedPaths.every((expandedPath) => isPathSupported(expandedPath, filterObjects)); } -function setDefaultAsFirst( - pathObjects: PathObject[], - filterObjects: FilterObject[], -): PathObject[] { +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; + // minimum of two path objects needed to swap + if (defaultIndex <= 0 || pathObjects.length < 2) return pathObjects; - const tmp = pathObjects[1]; + // swap default to first position in array + const tmp = pathObjects[0]; pathObjects[0] = pathObjects[defaultIndex]; - pathObjects[1] = tmp; + pathObjects[defaultIndex] = tmp; return pathObjects; } From 9e19f7687e57e8b1469d11a7c28818dbb0bc1a94 Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Fri, 27 Oct 2023 17:35:36 -0700 Subject: [PATCH 6/6] style: fix linter errors --- src/utils/index.ts | 2 +- src/utils/schemas.ts | 4 +--- tests/utils/filter.tests.ts | 7 +++++-- 3 files changed, 7 insertions(+), 6 deletions(-) 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 16093808..119a188a 100644 --- a/tests/utils/filter.tests.ts +++ b/tests/utils/filter.tests.ts @@ -41,9 +41,12 @@ describe("entity filter", () => { }); it("should place the default entity as first element in list", () => { - const filterObjects = [{ path: "/root/entity/b" }, { path: "/root/entity/c", isDefault: true }]; + const filterObjects = [ + { path: "/root/entity/b" }, + { path: "/root/entity/c", isDefault: true }, + ]; const filtered = filterEntityList({ filterObjects, entitiesOrPaths: entities }); - const expectedPath ="/root/entity/c"; + const expectedPath = "/root/entity/c"; expect(filtered[0].path).to.be.equal(expectedPath); });