Skip to content

Commit

Permalink
#291 Fix leaf functions for new JSON format
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhad6 committed Jun 24, 2024
1 parent 9684be0 commit af20250
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,16 @@ export function leafToString(leaf: Leaf, round: boolean) {
return leaf;
}

if (leaf instanceof Array) {
switch (leaf[0]) {
case DataType.Datetime:
return formatDate(leaf[1]);
case DataType.Quantity:
return `${numberToString(leaf[1], round)} ${leaf[2]}`;
}
if (leaf === null) {
return "None";
}

if (leaf.type === DataType.Datetime) {
return formatDate(leaf.timestamp);
}

// leaf is null
return "None";
// leaf is Quantity
return `${numberToString(leaf.value, round)} ${leaf.unit}`;
}

/** Get the type (as a `LeafType` enum value) of the given leaf. */
Expand All @@ -63,27 +62,26 @@ export function getLeafType(leaf: Leaf) {
return LeafType.String;
}

if (leaf instanceof Array) {
switch (leaf[0]) {
case DataType.Datetime:
return LeafType.Datetime;
case DataType.Quantity:
return LeafType.Quantity;
}
if (leaf === null) {
return LeafType.Null;
}

if (leaf.type === DataType.Datetime) {
return LeafType.Datetime;
}

// leaf is null
return LeafType.Null;
// leaf is Quantity
return LeafType.Quantity;
}

/** Convert the given leaf to an input string and a unit input string. */
export function leafToInput(leaf: Leaf) {
if (leaf instanceof Array) {
switch (leaf[0]) {
if (typeof leaf === "object" && leaf !== null) {
switch (leaf.type) {
case DataType.Datetime:
return { input: getLocalISOString(leaf[1]), unitInput: "" };
return { input: getLocalISOString(leaf.timestamp), unitInput: "" };
case DataType.Quantity:
return { input: String(leaf[1]), unitInput: leaf[2] };
return { input: String(leaf.value), unitInput: leaf.unit };
}
}

Expand Down

0 comments on commit af20250

Please sign in to comment.