Skip to content

Instantly share code, notes, and snippets.

@jwrigh26
Created July 30, 2021 16:52
Show Gist options
  • Save jwrigh26/e1457827c84f95db125604507093e838 to your computer and use it in GitHub Desktop.
Save jwrigh26/e1457827c84f95db125604507093e838 to your computer and use it in GitHub Desktop.
An example of using regex in React/node.js
useEffect(() => {
const pathname = location?.pathname;
const pathNameChanged = hasValue(pathname) && pathname !== prevPathname;
const managedPaths = {
// regex looks for the id and value of foo
create: () => {
const regex = new RegExp(`^(?:.*)(${id})(?:(?:.*)(${foo}))?`);
const results = regex.exec(pathname) ?? [];
return R.includes(id, results) && R.includes(foo, results);
},
// regext is looking of the id and value of herp
punchCategorySettings: () => {
const regex = new RegExp(
`^(?:.*)(${id})(?:(?:.*)(${route.herp}))?`
);
const results = regex.exec(pathname) ?? [];
return (
R.includes(id, results) &&
R.includes(route.herp, results)
);
},
};
const setValuesForForm = key => {
const cachedValues = initialValuesCache[key];
const def = defaultForms[key];
const values = hasValue(cachedValues) ? cachedValues : def.initialValues;
dispatch(setForm({ form: { ...def, initialValues: values } }));
};
if (pathNameChanged && managedPaths.create()) {
setValuesForForm('policyCreate');
}
if (pathNameChanged && managedPaths.punchCategorySettings()) {
setValuesForForm('punchCategorySettings');
}
// Finaly update prev path
setPrevPathname(pathname);
}, [location]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment