Skip to content

Commit

Permalink
Merge pull request #7 from OPEnSLab-OSU/valve_range
Browse files Browse the repository at this point in the history
Implemented valve range feature (master)
  • Loading branch information
NathanJesudason authored Sep 8, 2021
2 parents c484994 + 2eef1da commit e376d74
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
31 changes: 30 additions & 1 deletion pages/TaskConfig/SubmitCard/SubmitCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const mergeWithFormValues = (base: TaskServer, values: FormValues): TaskServer =
merged.schedule = Math.floor(schedule.getTime() / 1000);

merged.timeBetween = values.timeBetween;
merged.valves = values.valves.split(",").map(v => Number(v));
merged.valves = getValves(values.valves);

([
"flushTime",
Expand All @@ -40,6 +40,35 @@ const mergeWithFormValues = (base: TaskServer, values: FormValues): TaskServer =
return merged;
};

function getValves(valves: string) {
const regexp = /(\d+-\d+)|\d+/g;
const matches = [...valves.matchAll(regexp)];
console.log(matches);
let values: number[] = [];
const distinctValues = new Set();
matches.forEach(function (match) {
const matchString = match.toString();
if (matchString.indexOf("-")) {
const firstBound = parseInt(matchString.slice(0, matchString.indexOf("-")), 10);
const secondBound = parseInt(matchString.slice(matchString.indexOf("-") + 1), 10);
for (let i = firstBound; i < secondBound + 1; i++) {
values.push(i);
}
} else {
values.push(parseInt(matchString, 10));
}
});

values.forEach(function (value) {
distinctValues.add(value);
});
values = [];
for (const distinctValue of distinctValues) {
values.push(Number(distinctValue));
}
return values;
}

export const SubmitCard = () => {
// Get Task from store then merge with data in the form
const { taskId } = useParams<{ taskId: string }>();
Expand Down
45 changes: 37 additions & 8 deletions pages/TaskConfig/data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import z, { string, number, object } from "zod";

const ValveSchema = number().min(0).max(24);
const ValveSchema = number().min(0).max(23);

export const FormSchema = object({
name: string().max(24),
Expand All @@ -12,12 +12,41 @@ export const FormSchema = object({
}),
valves: string()
.nonempty()
.refine(s => s.split(",").every(n => !isNaN(Number(n))), {
message: "Input contains non-numeric character or doesn't follow the format",
})
.refine(s => s.split(",").every(n => ValveSchema.safeParse(Number(n)).success), {
message: "Valve number must be >= 0 and <= 23",
}),
.refine(
s => s.split(",").every(n => n.split("-").every(n => !isNaN(Number(n)) || n == "-")),
{
message: "Input contains non-numeric character or doesn't follow the format",
}
)
.refine(
s =>
s
.split(",")
.every(n => n.split("-").every(n => ValveSchema.safeParse(Number(n)).success)),
{
message: "Valve number must be >= 0 and <= 23",
}
)
.refine(
s =>
Array.from(s.matchAll(/(\d*-\d*)/g)).every(m =>
RegExp(/(\d+-\d+)/g).test(m.toString())
),
{
message: "Range missing bound",
}
)
.refine(
s =>
Array.from(s.matchAll(/(\d+-\d+)/g)).every(
m =>
Number([...m.toString().matchAll(/(\d+(?=-))-(\d+)/g)][0][1]) <
Number([...m.toString().matchAll(/(\d+(?=-))-(\d+)/g)][0][2])
),
{
message: "Larger bound is first",
}
),
timeBetween: number().min(0),
notes: string().optional(),
flushTime: number().min(0),
Expand Down Expand Up @@ -66,7 +95,7 @@ export const valveFields: FieldProps[] = [
name: "valves",
label: "Valves",
sublabel: "Valves asigned to this task",
helperText: "Comma-separated valve numbers: eg. 1,2,3,4",
helperText: "Comma-separated valve numbers & ranges: eg. 1,3-8,21",
},
{
name: "timeBetween",
Expand Down

0 comments on commit e376d74

Please sign in to comment.