From 5b916be7c08240b13045e21876e18d282fcd3547 Mon Sep 17 00:00:00 2001 From: Iku-turso Date: Mon, 24 Oct 2022 15:06:23 +0300 Subject: [PATCH] Start using named export for composite Co-authored-by: Janne Savolainen Signed-off-by: Iku-turso --- .../composite-has-descendant.test.ts | 2 +- .../find-composite/find-composite.test.ts | 2 +- .../get-composite-normalization.test.ts | 3 +- .../get-composite-paths.test.ts | 2 +- .../get-composite/get-composite.test.ts | 2 +- .../composite/get-composite/get-composite.ts | 144 +++++++++--------- ...lication-menu-item-composite.injectable.ts | 2 +- .../preferences-composite.injectable.ts | 2 +- 8 files changed, 80 insertions(+), 79 deletions(-) diff --git a/src/common/utils/composite/composite-has-descendant/composite-has-descendant.test.ts b/src/common/utils/composite/composite-has-descendant/composite-has-descendant.test.ts index 2dae7326d1..0b7f07bf2d 100644 --- a/src/common/utils/composite/composite-has-descendant/composite-has-descendant.test.ts +++ b/src/common/utils/composite/composite-has-descendant/composite-has-descendant.test.ts @@ -5,7 +5,7 @@ import type { Composite } from "../get-composite/get-composite"; import { compositeHasDescendant } from "./composite-has-descendant"; -import getCompositeFor from "../get-composite/get-composite"; +import { getCompositeFor } from "../get-composite/get-composite"; describe("composite-has-descendant, given composite with children and grand children", () => { let composite: Composite<{ id: string; parentId?: string }>; diff --git a/src/common/utils/composite/find-composite/find-composite.test.ts b/src/common/utils/composite/find-composite/find-composite.test.ts index a04d79325c..5b4147070e 100644 --- a/src/common/utils/composite/find-composite/find-composite.test.ts +++ b/src/common/utils/composite/find-composite/find-composite.test.ts @@ -4,7 +4,7 @@ */ import type { Composite } from "../get-composite/get-composite"; import { findComposite } from "./find-composite"; -import getCompositeFor from "../get-composite/get-composite"; +import { getCompositeFor } from "../get-composite/get-composite"; describe("find-composite", () => { let composite: Composite<{ id: string; parentId?: string }>; diff --git a/src/common/utils/composite/get-composite-normalization/get-composite-normalization.test.ts b/src/common/utils/composite/get-composite-normalization/get-composite-normalization.test.ts index 2a3074c4f1..ac88973251 100644 --- a/src/common/utils/composite/get-composite-normalization/get-composite-normalization.test.ts +++ b/src/common/utils/composite/get-composite-normalization/get-composite-normalization.test.ts @@ -3,7 +3,8 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getCompositeNormalization } from "./get-composite-normalization"; -import getCompositeFor from "../get-composite/get-composite"; +import { getCompositeFor } from "../get-composite/get-composite"; + describe("get-composite-normalization", () => { it("given a composite, flattens it to paths and composites", () => { diff --git a/src/common/utils/composite/get-composite-paths/get-composite-paths.test.ts b/src/common/utils/composite/get-composite-paths/get-composite-paths.test.ts index 5d0c28e771..079f7e1b83 100644 --- a/src/common/utils/composite/get-composite-paths/get-composite-paths.test.ts +++ b/src/common/utils/composite/get-composite-paths/get-composite-paths.test.ts @@ -3,8 +3,8 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getCompositePaths } from "./get-composite-paths"; -import getCompositeFor from "../get-composite/get-composite"; import { sortBy } from "lodash/fp"; +import { getCompositeFor } from "../get-composite/get-composite"; describe("get-composite-paths", () => { it("given composite with transformed children, returns paths of transformed children in hierarchical order", () => { diff --git a/src/common/utils/composite/get-composite/get-composite.test.ts b/src/common/utils/composite/get-composite/get-composite.test.ts index 398282e449..cc038d46bb 100644 --- a/src/common/utils/composite/get-composite/get-composite.test.ts +++ b/src/common/utils/composite/get-composite/get-composite.test.ts @@ -4,9 +4,9 @@ */ import type { Composite } from "./get-composite"; -import getCompositeFor from "./get-composite"; import { getCompositePaths } from "../get-composite-paths/get-composite-paths"; import { sortBy } from "lodash/fp"; +import { getCompositeFor } from "./get-composite"; interface SomeItem { id: string; diff --git a/src/common/utils/composite/get-composite/get-composite.ts b/src/common/utils/composite/get-composite/get-composite.ts index c55f56e335..7e499ad40a 100644 --- a/src/common/utils/composite/get-composite/get-composite.ts +++ b/src/common/utils/composite/get-composite/get-composite.ts @@ -31,105 +31,105 @@ interface Configuration { handleMissingParentIds?: (parentIdsForHandling: ParentIdsForHandling) => void; } -export default ({ +export const getCompositeFor = ({ rootId = undefined, getId, getParentId, transformChildren = identity, handleMissingParentIds = throwMissingParentIds, }: Configuration) => (source: T[]) => { - const undefinedIds = pipeline( - source, - filter((x) => getId(x) === undefined), - ); - - if (undefinedIds.length) { - throw new Error( - `Tried to get a composite but encountered ${undefinedIds.length} undefined ids`, + const undefinedIds = pipeline( + source, + filter((x) => getId(x) === undefined), ); - } - const selfReferencingIds = pipeline( - source, - filter((x) => getId(x) === getParentId(x)), - map(getId), - ); + if (undefinedIds.length) { + throw new Error( + `Tried to get a composite but encountered ${undefinedIds.length} undefined ids`, + ); + } - if (selfReferencingIds.length) { - throw new Error( - `Tried to get a composite, but found items with self as parent: "${selfReferencingIds.join( - '", ', - )}"`, + const selfReferencingIds = pipeline( + source, + filter((x) => getId(x) === getParentId(x)), + map(getId), ); - } - const duplicateIds = pipeline( - source, - countBy(getId), - toPairs, - filter(([, count]) => count > 1), - map(nth(0)), - ); + if (selfReferencingIds.length) { + throw new Error( + `Tried to get a composite, but found items with self as parent: "${selfReferencingIds.join( + '", ', + )}"`, + ); + } - if (duplicateIds.length) { - throw new Error( - `Tried to get a composite but encountered non-unique ids: "${duplicateIds - .map((x) => String(x)) - .join('", "')}"`, + const duplicateIds = pipeline( + source, + countBy(getId), + toPairs, + filter(([, count]) => count > 1), + map(nth(0)), ); - } - const allIds = pipeline(source, map(getId)); + if (duplicateIds.length) { + throw new Error( + `Tried to get a composite but encountered non-unique ids: "${duplicateIds + .map((x) => String(x)) + .join('", "')}"`, + ); + } - const allParentIds = pipeline(source, map(getParentId), uniq, compact); + const allIds = pipeline(source, map(getId)); - const missingParentIds = without(allIds, allParentIds); + const allParentIds = pipeline(source, map(getParentId), uniq, compact); - if (missingParentIds.length) { - handleMissingParentIds({ missingParentIds, availableParentIds: allIds }); - } + const missingParentIds = without(allIds, allParentIds); - const toComposite = (thing: T): Composite => { - const thingId = getId(thing); + if (missingParentIds.length) { + handleMissingParentIds({ missingParentIds, availableParentIds: allIds }); + } - return { - id: thingId, - parentId: getParentId(thing), - value: thing, + const toComposite = (thing: T): Composite => { + const thingId = getId(thing); - children: pipeline( - source, + return { + id: thingId, + parentId: getParentId(thing), + value: thing, - filter((childThing) => { - const parentId = getParentId(childThing); + children: pipeline( + source, - return parentId === thingId; - }), + filter((childThing) => { + const parentId = getParentId(childThing); - transformChildren, + return parentId === thingId; + }), - map(toComposite), - ), + transformChildren, + + map(toComposite), + ), + }; }; + + const isRootId = rootId + ? (thing: T) => getId(thing) === rootId + : (thing: T) => getParentId(thing) === undefined; + + const roots = source.filter(isRootId); + + if (roots.length > 1) { + throw new Error( + `Tried to get a composite, but multiple roots where encountered: "${roots + .map(getId) + .join('", "')}"`, + ); + } + + return toComposite(roots[0]); }; - const isRootId = rootId - ? (thing: T) => getId(thing) === rootId - : (thing: T) => getParentId(thing) === undefined; - - const roots = source.filter(isRootId); - - if (roots.length > 1) { - throw new Error( - `Tried to get a composite, but multiple roots where encountered: "${roots - .map(getId) - .join('", "')}"`, - ); - } - - return toComposite(roots[0]); -}; - interface ParentIdsForHandling { missingParentIds: string[]; availableParentIds: string[]; diff --git a/src/features/application-menu/main/application-menu-item-composite.injectable.ts b/src/features/application-menu/main/application-menu-item-composite.injectable.ts index b0e2a2eb79..8941500dbe 100644 --- a/src/features/application-menu/main/application-menu-item-composite.injectable.ts +++ b/src/features/application-menu/main/application-menu-item-composite.injectable.ts @@ -5,7 +5,6 @@ import { getInjectable } from "@ogre-tools/injectable"; import applicationMenuItemsInjectable from "./application-menu-items.injectable"; import type { Composite } from "../../../common/utils/composite/get-composite/get-composite"; -import getCompositeFor from "../../../common/utils/composite/get-composite/get-composite"; import { computed } from "mobx"; import { pipeline } from "@ogre-tools/fp"; import type { ApplicationMenuItemTypes } from "./menu-items/application-menu-item-injection-token"; @@ -15,6 +14,7 @@ import type { Orderable } from "../../../common/utils/composable-responsibilitie import { orderByOrderNumber } from "../../../common/utils/composable-responsibilities/orderable/orderable"; import logErrorInjectable from "../../../common/log-error.injectable"; import { isShown } from "../../../common/utils/composable-responsibilities/showable/showable"; +import { getCompositeFor } from "../../../common/utils/composite/get-composite/get-composite"; export type MenuItemRoot = Discriminable<"root"> & RootComposite<"root"> & diff --git a/src/features/preferences/renderer/preference-items/preferences-composite.injectable.ts b/src/features/preferences/renderer/preference-items/preferences-composite.injectable.ts index e62915f4d3..058fd7b980 100644 --- a/src/features/preferences/renderer/preference-items/preferences-composite.injectable.ts +++ b/src/features/preferences/renderer/preference-items/preferences-composite.injectable.ts @@ -12,8 +12,8 @@ import type { PreferenceTabsRoot } from "./preference-tab-root"; import { preferenceTabsRoot } from "./preference-tab-root"; import logErrorInjectable from "../../../../common/log-error.injectable"; import { isShown } from "../../../../common/utils/composable-responsibilities/showable/showable"; -import getCompositeFor from "../../../../common/utils/composite/get-composite/get-composite"; import { orderByOrderNumber } from "../../../../common/utils/composable-responsibilities/orderable/orderable"; +import { getCompositeFor } from "../../../../common/utils/composite/get-composite/get-composite"; const preferencesCompositeInjectable = getInjectable({ id: "preferences-composite",