Skip to content

Commit

Permalink
move createInstrumentGroupsFromInstlist to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
rerpha committed Nov 27, 2024
1 parent 1ed10b5 commit c98ebb2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
28 changes: 3 additions & 25 deletions app/instruments/page.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,11 @@
"use client";
import Link from "next/link";
import { useEffect, useState } from "react";
import {
IfcPVWSMessage,
IfcPVWSRequest,
instList,
PVWSRequestType,
} from "@/app/types";
import {
dehex_and_decompress,
instListFromBytes,
} from "@/app/components/dehex_and_decompress";
import { IfcPVWSMessage, IfcPVWSRequest } from "@/app/types";
import { instListFromBytes } from "@/app/components/dehex_and_decompress";
import useWebSocket from "react-use-websocket";
import { instListPV, instListSubscription, socketURL } from "@/app/commonVars";

export function createInstrumentGroupsFromInstlist(
jsonInstList: instList,
): Map<string, Array<string>> {
let newInstrumentGroups: Map<string, Array<string>> = new Map();
for (let inst of jsonInstList) {
for (let group of inst["groups"]) {
if (!newInstrumentGroups.has(group)) {
newInstrumentGroups.set(group, []);
}
newInstrumentGroups.get(group)!.push(inst["name"]);
}
}
return newInstrumentGroups;
}
import createInstrumentGroupsFromInstlist from "@/app/instruments/utils";

export default function Instruments() {
const [instrumentGroups, setInstrumentGroups] = useState<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createInstrumentGroupsFromInstlist } from "@/app/instruments/page";
import createInstrumentGroupsFromInstlist from "@/app/instruments/utils";
import { instList } from "@/app/types";

test("createInstrumentGroupsFromInstlist adds an instrument to a group if it has one", () => {
Expand Down
16 changes: 16 additions & 0 deletions app/instruments/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { instList } from "@/app/types";

export default function createInstrumentGroupsFromInstlist(
jsonInstList: instList,
): Map<string, Array<string>> {
let newInstrumentGroups: Map<string, Array<string>> = new Map();
for (let inst of jsonInstList) {
for (let group of inst["groups"]) {
if (!newInstrumentGroups.has(group)) {
newInstrumentGroups.set(group, []);
}
newInstrumentGroups.get(group)!.push(inst["name"]);
}
}
return newInstrumentGroups;
}

0 comments on commit c98ebb2

Please sign in to comment.