mirror of
https://github.com/Dictionarry-Hub/profilarr.git
synced 2026-02-01 15:20:49 +01:00
- add new regex patterns, matched using PCRE2, with case insensitivity - name, description, pattern, tags - add unit tests, attempt to highlight matches
59 lines
1.9 KiB
JavaScript
59 lines
1.9 KiB
JavaScript
// useRegexTesting.js
|
|
import {useState, useCallback} from 'react';
|
|
import {RegexPatterns} from '@api/data';
|
|
import Alert from '@ui/Alert';
|
|
|
|
export const useRegexTesting = onUpdateTests => {
|
|
const [isRunningTests, setIsRunningTests] = useState(false);
|
|
|
|
const runTests = useCallback(
|
|
async (pattern, tests) => {
|
|
if (!pattern?.trim() || !tests?.length) {
|
|
return tests;
|
|
}
|
|
|
|
setIsRunningTests(true);
|
|
try {
|
|
const result = await RegexPatterns.runTests(pattern, tests);
|
|
if (result.success) {
|
|
// Calculate test statistics
|
|
const totalTests = result.tests.length;
|
|
const passedTests = result.tests.filter(
|
|
test => test.passes
|
|
).length;
|
|
|
|
// Show success message with statistics
|
|
Alert.success(
|
|
`Tests completed: ${passedTests}/${totalTests} passed`,
|
|
{
|
|
autoClose: 3000,
|
|
hideProgressBar: false
|
|
}
|
|
);
|
|
|
|
// Update tests through the callback
|
|
if (onUpdateTests) {
|
|
onUpdateTests(result.tests);
|
|
}
|
|
return result.tests;
|
|
} else {
|
|
Alert.error(result.message || 'Failed to run tests');
|
|
return tests;
|
|
}
|
|
} catch (error) {
|
|
console.error('Error running tests:', error);
|
|
Alert.error('An error occurred while running tests');
|
|
return tests;
|
|
} finally {
|
|
setIsRunningTests(false);
|
|
}
|
|
},
|
|
[onUpdateTests]
|
|
);
|
|
|
|
return {
|
|
isRunningTests,
|
|
runTests
|
|
};
|
|
};
|