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:
parent
e7b652c627
commit
5b916be7c0
@ -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 }>;
|
||||
|
||||
@ -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 }>;
|
||||
|
||||
@ -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", () => {
|
||||
|
||||
@ -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", () => {
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -31,105 +31,105 @@ interface Configuration<T> {
|
||||
handleMissingParentIds?: (parentIdsForHandling: ParentIdsForHandling) => void;
|
||||
}
|
||||
|
||||
export default <T>({
|
||||
export const getCompositeFor = <T>({
|
||||
rootId = undefined,
|
||||
getId,
|
||||
getParentId,
|
||||
transformChildren = identity,
|
||||
handleMissingParentIds = throwMissingParentIds,
|
||||
}: Configuration<T>) => (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<T> => {
|
||||
const thingId = getId(thing);
|
||||
if (missingParentIds.length) {
|
||||
handleMissingParentIds({ missingParentIds, availableParentIds: allIds });
|
||||
}
|
||||
|
||||
return {
|
||||
id: thingId,
|
||||
parentId: getParentId(thing),
|
||||
value: thing,
|
||||
const toComposite = (thing: T): Composite<T> => {
|
||||
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[];
|
||||
|
||||
@ -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"> &
|
||||
|
||||
@ -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",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user