Skip to content

Commit

Permalink
fix: regexp and fix filter
Browse files Browse the repository at this point in the history
  • Loading branch information
NasgulNexus committed Aug 12, 2024
1 parent 76da9d7 commit 5c27a80
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/lib/kit/components/Inputs/TimeRangeSelector/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ export const filterTimeArray = (
cutoff: string,
direction: 'greater' | 'less',
) => {
return times.filter(({value: time}) =>
direction === 'greater' ? time > cutoff : time < cutoff,
);
const isTimeFormat = (value: string) => /^\d{1,2}:\d{2}$/.test(value);

const compareValues = (a: string, b: string) => {
if (isTimeFormat(a) && isTimeFormat(b)) {
return direction === 'greater' ? a > b : a < b;
} else {
const aNum = parseInt(a, 10);
const bNum = parseInt(b, 10);

return direction === 'greater' ? aNum > bNum : aNum < bNum;
}
};

return times.filter(({value: time}) => compareValues(time, cutoff));
};

export const validateArray = (arr: {value: string}[]) =>
arr.every((obj) => /^([01]\d|2[0-3]):([0-5]\d)$/.test(obj.value));
arr.every((obj) => /^(\d+|\d{1,2}:\d{1,2})$/.test(obj.value));

0 comments on commit 5c27a80

Please sign in to comment.