make a util for page selection

This commit is contained in:
EthanHealy01 2025-09-17 20:06:54 +01:00
parent ad7cd6a417
commit 5eecb737af
3 changed files with 27 additions and 46 deletions

View File

@ -1,6 +1,7 @@
import { Stack, TextInput } from "@mantine/core";
import { useTranslation } from "react-i18next";
import { RemovePagesParameters } from "../../../hooks/tools/removePages/useRemovePagesParameters";
import { validatePageNumbers } from "../../../utils/pageSelection";
interface RemovePagesSettingsProps {
parameters: RemovePagesParameters;
@ -8,29 +9,6 @@ interface RemovePagesSettingsProps {
disabled?: boolean;
}
// Validation function for page numbers (same as in parameters hook)
const validatePageNumbers = (pageNumbers: string): boolean => {
if (!pageNumbers.trim()) return false;
// Normalize input for validation: remove spaces around commas and other spaces
const normalized = pageNumbers.replace(/\s*,\s*/g, ',').replace(/\s+/g, '');
const parts = normalized.split(',');
// Regular expressions for different page number formats
const singlePageRegex = /^\d+$/; // Single page: 1, 2, 3, etc.
const rangeRegex = /^\d+-\d*$/; // Range: 1-5, 10-, etc.
const negativeRegex = /^-\d+$/; // Negative: -3 (last 3 pages)
const mathRegex = /^\d*[n]\d*[+\-*/]\d+$/; // Mathematical: 2n+1, n-1, etc.
return parts.every(part => {
if (!part) return false;
return singlePageRegex.test(part) ||
rangeRegex.test(part) ||
negativeRegex.test(part) ||
mathRegex.test(part);
});
};
const RemovePagesSettings = ({ parameters, onParameterChange, disabled = false }: RemovePagesSettingsProps) => {
const { t } = useTranslation();

View File

@ -1,5 +1,6 @@
import { BaseParameters } from '../../../types/parameters';
import { useBaseParameters, BaseParametersHook } from '../shared/useBaseParameters';
import { validatePageNumbers } from '../../../utils/pageSelection';
export interface RemovePagesParameters extends BaseParameters {
pageNumbers: string; // comma-separated page numbers or ranges (e.g., "1,3,5-8")
@ -11,29 +12,6 @@ export const defaultParameters: RemovePagesParameters = {
export type RemovePagesParametersHook = BaseParametersHook<RemovePagesParameters>;
// Validation function for page numbers
const validatePageNumbers = (pageNumbers: string): boolean => {
if (!pageNumbers.trim()) return false;
// Normalize input for validation: remove spaces around commas and other spaces
const normalized = pageNumbers.replace(/\s*,\s*/g, ',').replace(/\s+/g, '');
const parts = normalized.split(',');
// Regular expressions for different page number formats
const singlePageRegex = /^\d+$/; // Single page: 1, 2, 3, etc.
const rangeRegex = /^\d+-\d*$/; // Range: 1-5, 10-, etc.
const negativeRegex = /^-\d+$/; // Negative: -3 (last 3 pages)
const mathRegex = /^\d*[n]\d*[+\-*/]\d+$/; // Mathematical: 2n+1, n-1, etc.
return parts.every(part => {
if (!part) return false;
return singlePageRegex.test(part) ||
rangeRegex.test(part) ||
negativeRegex.test(part) ||
mathRegex.test(part);
});
};
export const useRemovePagesParameters = (): RemovePagesParametersHook => {
return useBaseParameters({
defaultParameters,

View File

@ -0,0 +1,25 @@
export const validatePageNumbers = (pageNumbers: string): boolean => {
if (!pageNumbers.trim()) return false;
// Normalize input for validation: remove spaces around commas and other spaces
const normalized = pageNumbers.replace(/\s*,\s*/g, ',').replace(/\s+/g, '');
const parts = normalized.split(',');
// Regular expressions for different page number formats
const singlePageRegex = /^\d+$/; // Single page: 1, 2, 3, etc.
const rangeRegex = /^\d+-\d*$/; // Range: 1-5, 10-, etc.
const negativeRegex = /^-\d+$/; // Negative: -3 (last 3 pages)
const mathRegex = /^\d*[n]\d*[+\-*/]\d+$/; // Mathematical: 2n+1, n-1, etc.
return parts.every(part => {
if (!part) return false;
return (
singlePageRegex.test(part) ||
rangeRegex.test(part) ||
negativeRegex.test(part) ||
mathRegex.test(part)
);
});
};