2019-08-31 20:51:14 -07:00
|
|
|
// TODO: [META-8255] These are placeholder utils that exist here until we find them a home in the proper location.
|
|
|
|
|
2018-05-02 09:45:26 -07:00
|
|
|
/**
|
|
|
|
* Constant value for an empty regex source string
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const emptyRegexSource = '(?:)';
|
|
|
|
|
2018-06-17 00:52:06 -07:00
|
|
|
/**
|
|
|
|
* Checks that the pattern parameter is a valid RegExp string and optionally checks with a customChecker as well
|
|
|
|
* @param {string} pattern the string to validate
|
|
|
|
* @param {(pattern: string) => boolean} [customChecker] an optional function to check the pattern against
|
|
|
|
* @return {boolean}
|
2018-06-18 15:50:07 -07:00
|
|
|
* @throws SyntaxError
|
2018-06-17 00:52:06 -07:00
|
|
|
*/
|
2019-08-31 20:51:14 -07:00
|
|
|
export const validateRegExp = (
|
2018-06-18 15:50:07 -07:00
|
|
|
pattern: string | null | undefined,
|
2018-06-17 01:16:00 -07:00
|
|
|
customChecker?: (pattern: any) => boolean
|
2018-06-17 00:52:06 -07:00
|
|
|
): { isValid: boolean; regExp: RegExp } => {
|
2018-06-18 15:50:07 -07:00
|
|
|
const regExp = new RegExp(pattern!);
|
2018-06-17 00:52:06 -07:00
|
|
|
const { source } = regExp;
|
2018-06-17 01:16:00 -07:00
|
|
|
const isValid = !!pattern && source !== emptyRegexSource;
|
2018-06-17 00:52:06 -07:00
|
|
|
|
|
|
|
if (isValid && customChecker && customChecker(pattern)) {
|
|
|
|
return { isValid, regExp };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { isValid, regExp };
|
|
|
|
};
|
|
|
|
|
2018-04-16 10:30:26 -07:00
|
|
|
/**
|
2019-08-31 20:51:14 -07:00
|
|
|
* Checks that a string matches the expected valuePatternRegex
|
|
|
|
* @param {string} candidate the supplied pattern string
|
|
|
|
* @return {boolean}
|
2018-04-16 10:30:26 -07:00
|
|
|
*/
|
2019-08-31 20:51:14 -07:00
|
|
|
export const isValidCustomValuePattern = (candidate: string): boolean => !!candidate;
|