https://github.com/opensly/is-true-regex
Determines if a given value is a regular expression.
https://github.com/opensly/is-true-regex
Last synced: 5 months ago
JSON representation
Determines if a given value is a regular expression.
- Host: GitHub
- URL: https://github.com/opensly/is-true-regex
- Owner: opensly
- License: mit
- Created: 2024-11-09T02:58:32.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-15T06:01:22.000Z (12 months ago)
- Last Synced: 2025-06-15T06:49:57.075Z (12 months ago)
- Language: TypeScript
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# is-true-regex
[](https://www.npmjs.com/package/is-true-regex)
[](https://opensource.org/licenses/MIT)
[](https://www.typescriptlang.org/)
A robust utility to check if a value is a RegExp object. Works with both JavaScript and TypeScript, supports cross-realm/iframe scenarios, and handles edge cases securely.
## Table of Contents
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [JavaScript](#javascript)
- [TypeScript](#typescript)
- [API](#api)
- [Security](#security)
- [Performance](#performance)
- [License](#license)
## Features
- ✅ TypeScript support with type guards
- ✅ Cross-realm/iframe compatibility
- ✅ Secure against spoofing attempts
- ✅ Handles edge cases and error conditions
- ✅ Zero dependencies
- ✅ High performance with early returns
## Installation
```bash
npm install is-true-regex
```
## Usage
### JavaScript
```javascript
import isTrueRegex from "is-true-regex";
// Basic usage
isTrueRegex(/abc/); // true
isTrueRegex(new RegExp('abc')); // true
isTrueRegex('abc'); // false
isTrueRegex(null); // false
isTrueRegex(undefined); // false
// Edge cases
isTrueRegex({}); // false
isTrueRegex([]); // false
isTrueRegex(function() {}); // false
isTrueRegex(new Date()); // false
```
### TypeScript
```typescript
import isTrueRegex from "is-true-regex";
// Type guard usage
const value: unknown = /abc/;
if (isTrueRegex(value)) {
// value is now typed as RegExp
value.test('abc'); // TypeScript knows this is safe
}
// Basic usage
isTrueRegex(/abc/); // true
isTrueRegex(new RegExp('abc')); // true
isTrueRegex('abc'); // false
```
## API
### `isTrueRegex(value: unknown): value is RegExp`
Checks if the provided value is a RegExp object. The function is designed to be secure and reliable, handling various edge cases:
- Cross-realm/iframe scenarios
- Objects with manipulated `Symbol.toStringTag`
- Proxy objects
- Frozen and sealed objects
- Objects with getters that throw
- Objects with circular references
- Subclassed RegExp objects
#### Parameters
- `value` (`unknown`): The value to check
#### Returns
- `boolean`: `true` if the value is a RegExp object, `false` otherwise
## Security
The implementation is designed to be secure against common spoofing attempts:
- Rejects objects that only mimic RegExp properties
- Validates prototype chain integrity
- Handles Symbol.toStringTag manipulation
- Safely handles error conditions
## Performance
The implementation is optimized for performance:
- Early returns for null/undefined values
- Early returns for primitive types
- Efficient prototype chain traversal
- No unnecessary property checks
## License
MIT