1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Start using named export for composite

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
Iku-turso 2022-10-24 15:06:23 +03:00
parent e7b652c627
commit 5b916be7c0
8 changed files with 80 additions and 79 deletions

View File

@ -5,7 +5,7 @@
import type { Composite } from "../get-composite/get-composite"; import type { Composite } from "../get-composite/get-composite";
import { compositeHasDescendant } from "./composite-has-descendant"; 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", () => { describe("composite-has-descendant, given composite with children and grand children", () => {
let composite: Composite<{ id: string; parentId?: string }>; let composite: Composite<{ id: string; parentId?: string }>;

View File

@ -4,7 +4,7 @@
*/ */
import type { Composite } from "../get-composite/get-composite"; import type { Composite } from "../get-composite/get-composite";
import { findComposite } from "./find-composite"; import { findComposite } from "./find-composite";
import getCompositeFor from "../get-composite/get-composite"; import { getCompositeFor } from "../get-composite/get-composite";
describe("find-composite", () => { describe("find-composite", () => {
let composite: Composite<{ id: string; parentId?: string }>; let composite: Composite<{ id: string; parentId?: string }>;

View File

@ -3,7 +3,8 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { getCompositeNormalization } from "./get-composite-normalization"; import { getCompositeNormalization } from "./get-composite-normalization";
import getCompositeFor from "../get-composite/get-composite"; import { getCompositeFor } from "../get-composite/get-composite";
describe("get-composite-normalization", () => { describe("get-composite-normalization", () => {
it("given a composite, flattens it to paths and composites", () => { it("given a composite, flattens it to paths and composites", () => {

View File

@ -3,8 +3,8 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { getCompositePaths } from "./get-composite-paths"; import { getCompositePaths } from "./get-composite-paths";
import getCompositeFor from "../get-composite/get-composite";
import { sortBy } from "lodash/fp"; import { sortBy } from "lodash/fp";
import { getCompositeFor } from "../get-composite/get-composite";
describe("get-composite-paths", () => { describe("get-composite-paths", () => {
it("given composite with transformed children, returns paths of transformed children in hierarchical order", () => { it("given composite with transformed children, returns paths of transformed children in hierarchical order", () => {

View File

@ -4,9 +4,9 @@
*/ */
import type { Composite } from "./get-composite"; import type { Composite } from "./get-composite";
import getCompositeFor from "./get-composite";
import { getCompositePaths } from "../get-composite-paths/get-composite-paths"; import { getCompositePaths } from "../get-composite-paths/get-composite-paths";
import { sortBy } from "lodash/fp"; import { sortBy } from "lodash/fp";
import { getCompositeFor } from "./get-composite";
interface SomeItem { interface SomeItem {
id: string; id: string;

View File

@ -31,105 +31,105 @@ interface Configuration<T> {
handleMissingParentIds?: (parentIdsForHandling: ParentIdsForHandling) => void; handleMissingParentIds?: (parentIdsForHandling: ParentIdsForHandling) => void;
} }
export default <T>({ export const getCompositeFor = <T>({
rootId = undefined, rootId = undefined,
getId, getId,
getParentId, getParentId,
transformChildren = identity, transformChildren = identity,
handleMissingParentIds = throwMissingParentIds, handleMissingParentIds = throwMissingParentIds,
}: Configuration<T>) => (source: T[]) => { }: Configuration<T>) => (source: T[]) => {
const undefinedIds = pipeline( const undefinedIds = pipeline(
source, source,
filter((x) => getId(x) === undefined), filter((x) => getId(x) === undefined),
);
if (undefinedIds.length) {
throw new Error(
`Tried to get a composite but encountered ${undefinedIds.length} undefined ids`,
); );
}
const selfReferencingIds = pipeline( if (undefinedIds.length) {
source, throw new Error(
filter((x) => getId(x) === getParentId(x)), `Tried to get a composite but encountered ${undefinedIds.length} undefined ids`,
map(getId), );
); }
if (selfReferencingIds.length) { const selfReferencingIds = pipeline(
throw new Error( source,
`Tried to get a composite, but found items with self as parent: "${selfReferencingIds.join( filter((x) => getId(x) === getParentId(x)),
'", ', map(getId),
)}"`,
); );
}
const duplicateIds = pipeline( if (selfReferencingIds.length) {
source, throw new Error(
countBy(getId), `Tried to get a composite, but found items with self as parent: "${selfReferencingIds.join(
toPairs, '", ',
filter(([, count]) => count > 1), )}"`,
map(nth(0)), );
); }
if (duplicateIds.length) { const duplicateIds = pipeline(
throw new Error( source,
`Tried to get a composite but encountered non-unique ids: "${duplicateIds countBy(getId),
.map((x) => String(x)) toPairs,
.join('", "')}"`, 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) { const missingParentIds = without(allIds, allParentIds);
handleMissingParentIds({ missingParentIds, availableParentIds: allIds });
}
const toComposite = (thing: T): Composite<T> => { if (missingParentIds.length) {
const thingId = getId(thing); handleMissingParentIds({ missingParentIds, availableParentIds: allIds });
}
return { const toComposite = (thing: T): Composite<T> => {
id: thingId, const thingId = getId(thing);
parentId: getParentId(thing),
value: thing,
children: pipeline( return {
source, id: thingId,
parentId: getParentId(thing),
value: thing,
filter((childThing) => { children: pipeline(
const parentId = getParentId(childThing); 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 { interface ParentIdsForHandling {
missingParentIds: string[]; missingParentIds: string[];
availableParentIds: string[]; availableParentIds: string[];

View File

@ -5,7 +5,6 @@
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import applicationMenuItemsInjectable from "./application-menu-items.injectable"; import applicationMenuItemsInjectable from "./application-menu-items.injectable";
import type { Composite } from "../../../common/utils/composite/get-composite/get-composite"; 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 { computed } from "mobx";
import { pipeline } from "@ogre-tools/fp"; import { pipeline } from "@ogre-tools/fp";
import type { ApplicationMenuItemTypes } from "./menu-items/application-menu-item-injection-token"; 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 { orderByOrderNumber } from "../../../common/utils/composable-responsibilities/orderable/orderable";
import logErrorInjectable from "../../../common/log-error.injectable"; import logErrorInjectable from "../../../common/log-error.injectable";
import { isShown } from "../../../common/utils/composable-responsibilities/showable/showable"; import { isShown } from "../../../common/utils/composable-responsibilities/showable/showable";
import { getCompositeFor } from "../../../common/utils/composite/get-composite/get-composite";
export type MenuItemRoot = Discriminable<"root"> & export type MenuItemRoot = Discriminable<"root"> &
RootComposite<"root"> & RootComposite<"root"> &

View File

@ -12,8 +12,8 @@ import type { PreferenceTabsRoot } from "./preference-tab-root";
import { preferenceTabsRoot } from "./preference-tab-root"; import { preferenceTabsRoot } from "./preference-tab-root";
import logErrorInjectable from "../../../../common/log-error.injectable"; import logErrorInjectable from "../../../../common/log-error.injectable";
import { isShown } from "../../../../common/utils/composable-responsibilities/showable/showable"; 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 { orderByOrderNumber } from "../../../../common/utils/composable-responsibilities/orderable/orderable";
import { getCompositeFor } from "../../../../common/utils/composite/get-composite/get-composite";
const preferencesCompositeInjectable = getInjectable({ const preferencesCompositeInjectable = getInjectable({
id: "preferences-composite", id: "preferences-composite",