Created
July 30, 2021 16:52
-
-
Save jwrigh26/e1457827c84f95db125604507093e838 to your computer and use it in GitHub Desktop.
An example of using regex in React/node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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