Skip to content

Commit 7ef71cc

Browse files
committed
[gephi-lite] Fixes castScalarToModelValue type
Details: - Clarifies in types that castScalarToModelValue can return undefined values - Fixes various code places that weren't handling properly that cast values could indeed be undefined
1 parent be71736 commit 7ef71cc

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

packages/gephi-lite/src/core/filters/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FilteredGraph, Scalar, gephiLiteStringify } from "@gephi/gephi-lite-sdk";
22
import { subgraph } from "graphology-operators";
3-
import { isNumber } from "lodash";
3+
import { isNil, isNumber } from "lodash";
44
import { DateTime } from "luxon";
55

66
import {
@@ -47,7 +47,7 @@ export function filterValue(
4747
if (value instanceof DateTime || isNumber(value)) {
4848
return !!filter.keepMissingValues;
4949
}
50-
const strings = Array.isArray(value) ? value : [value];
50+
const strings = Array.isArray(value) ? value : !isNil(value) ? [value] : [];
5151
return strings.some((string) => !filter.terms || filter.terms.has(string));
5252
}
5353
}
@@ -100,7 +100,7 @@ export function filterGraph<G extends DatalessGraph | SigmaGraph>(
100100

101101
// Edges:
102102
else {
103-
let edges: string[] = [];
103+
let edges: string[];
104104
if (filter.type === "script") {
105105
const fullGraph = dataGraphToFullGraph(dataset, graph);
106106
edges = graph.filterEdges((edgeID) =>

packages/gephi-lite/src/core/graph/fieldModel.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,25 +132,19 @@ export function inferFieldType(fieldName: string, values: Scalar[], itemsCount:
132132
export function castScalarToModelValue<T extends FieldModelType = FieldModelType>(
133133
scalar: Scalar,
134134
fieldModel: FieldModelTypeSpecCollection[T],
135-
): FieldModelAbstraction[T]["expectedOutput"] {
135+
): FieldModelAbstraction[T]["expectedOutput"] | undefined {
136136
switch (fieldModel.type) {
137137
case "number":
138-
return toNumber(scalar) as FieldModelAbstraction["number"]["expectedOutput"];
138+
return toNumber(scalar);
139139
case "category":
140140
case "text":
141141
case "url":
142142
case "color":
143-
return toString(scalar) as FieldModelAbstraction["text"]["expectedOutput"];
143+
return toString(scalar) || "";
144144
case "keywords":
145-
return toStringArray(
146-
scalar,
147-
(fieldModel as FieldModelAbstraction["keywords"]["options"]).separator,
148-
) as FieldModelAbstraction["keywords"]["expectedOutput"];
145+
return toStringArray(scalar, (fieldModel as FieldModelAbstraction["keywords"]["options"]).separator);
149146
case "date":
150-
return toDate(
151-
scalar,
152-
(fieldModel as FieldModelAbstraction["date"]["options"]).format,
153-
) as FieldModelAbstraction["date"]["expectedOutput"];
147+
return toDate(scalar, (fieldModel as FieldModelAbstraction["date"]["options"]).format);
154148
default:
155149
throw new Error(`Unknown field type ${fieldModel.type}`);
156150
}

packages/gephi-lite/src/hooks/useDataCollection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function useDataCollection(field: FieldModel) {
2121
}
2222
case "keywords": {
2323
for (const itemId in data) {
24-
const keywords = castScalarToModelValue<"keywords">(data[itemId][field.id], field);
24+
const keywords = castScalarToModelValue<"keywords">(data[itemId][field.id], field) || [];
2525
for (let i = 0; i < keywords.length; i++) {
2626
if (!isNil(keywords[i])) values.add(keywords[i]);
2727
}

0 commit comments

Comments
 (0)