Skip to content

Commit

Permalink
refactor to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-vx51 committed Nov 16, 2024
1 parent 8f21c50 commit deaf55b
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions app/packages/looker/src/worker/deserializer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { deserialize } from "../numpy";

const extractSerializedMask = (
label: object,
maskProp: string
): string | undefined => {
if (typeof label?.[maskProp] === "string") {
return label[maskProp];
} else if (typeof label?.[maskProp]?.$binary?.base64 === "string") {
return label[maskProp].$binary.base64;
}

return undefined;
};

export const DeserializerFactory = {
Detection: (label, buffers) => {
let serializedMask: string;
if (typeof label?.mask === "string") {
serializedMask = label.mask;
} else if (typeof label?.mask?.$binary?.base64 === "string") {
serializedMask = label.mask.$binary.base64;
}
const serializedMask = extractSerializedMask(label, "mask");

if (serializedMask) {
const data = deserialize(serializedMask);
Expand All @@ -27,12 +35,7 @@ export const DeserializerFactory = {
}
},
Heatmap: (label, buffers) => {
let serializedMask: string;
if (typeof label?.map === "string") {
serializedMask = label.map;
} else if (typeof label?.map?.$binary?.base64 === "string") {
serializedMask = label.map.$binary.base64;
}
const serializedMask = extractSerializedMask(label, "map");

if (serializedMask) {
const data = deserialize(serializedMask);
Expand All @@ -48,12 +51,7 @@ export const DeserializerFactory = {
}
},
Segmentation: (label, buffers) => {
let serializedMask: string;
if (typeof label?.mask === "string") {
serializedMask = label.mask;
} else if (typeof label?.mask?.$binary?.base64 === "string") {
serializedMask = label.mask.$binary.base64;
}
const serializedMask = extractSerializedMask(label, "mask");

if (serializedMask) {
const data = deserialize(serializedMask);
Expand Down

0 comments on commit deaf55b

Please sign in to comment.