guardz
Version:
A simple and lightweight TypeScript type guard library for runtime type validation.
52 lines (51 loc) • 1.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isRegex = void 0;
const generateTypeGuardError_1 = require("./generateTypeGuardError");
/**
* Checks if a value is a RegExp object.
*
* This type guard validates that a value is a regular expression object,
* including both literal regex patterns and RegExp constructor instances.
*
* @param value - The value to check
* @param config - Optional configuration for error handling
* @returns True if the value is a RegExp, false otherwise
*
* @example
* ```typescript
* import { isRegex } from 'guardz';
*
* console.log(isRegex(/abc/)); // true
* console.log(isRegex(new RegExp('abc'))); // true
* console.log(isRegex(/^[a-z]+$/)); // true
* console.log(isRegex('not a regex')); // false
* console.log(isRegex(123)); // false
* console.log(isRegex(null)); // false
*
* // With type narrowing
* const data: unknown = getPattern();
* if (isRegex(data)) {
* // data is now typed as RegExp
* console.log(data.test('test string'));
* }
*
* // With error handling
* const pattern: unknown = getPattern();
* if (!isRegex(pattern, { identifier: 'emailPattern' })) {
* console.error('Invalid regex pattern');
* return;
* }
* // pattern is now typed as RegExp
* ```
*/
const isRegex = function (value, config) {
if (!(value instanceof RegExp)) {
if (config) {
config.callbackOnError((0, generateTypeGuardError_1.generateTypeGuardError)(value, config.identifier, 'RegExp'));
}
return false;
}
return true;
};
exports.isRegex = isRegex;