Skip to content

Commit

Permalink
feat: add isRegExp function (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jul 9, 2024
1 parent 4f2e48c commit 58e7d96
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions benchmarks/typed/isRegExp.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as _ from 'radashi'
import { bench } from 'vitest'

describe('isRegExp', () => {
bench('with no arguments', () => {
_.isRegExp()
})
})

17 changes: 17 additions & 0 deletions docs/typed/isRegExp.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: isRegExp
description: Returns true for RegExp instances
---

## Basic usage

Returns true for `RegExp` instances, even if they are subclass instances or from
other [realms](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof#instanceof_and_multiple_realms).

```ts
import * as _ from 'radashi'

_.isRegExp(/.+/) // true
_.isRegExp(new RegExp('.+')) // true
_.isRegExp(new (class extends RegExp {})('.+')) // true
```
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export * from './typed/isObject.ts'
export * from './typed/isPlainObject.ts'
export * from './typed/isPrimitive.ts'
export * from './typed/isPromise.ts'
export * from './typed/isRegExp.ts'
export * from './typed/isString.ts'
export * from './typed/isSymbol.ts'
export * from './typed/isTagged.ts'
Expand Down
5 changes: 5 additions & 0 deletions src/typed/isRegExp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { isTagged } from 'radashi'

export function isRegExp(value: unknown): value is RegExp {
return isTagged(value, '[object RegExp]')
}
23 changes: 23 additions & 0 deletions tests/typed/isRegExp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as vm from 'node:vm'
import * as _ from 'radashi'

describe('isRegExp', () => {
test('returns true for RegExp instances', () => {
expect(_.isRegExp(/.+/)).toBe(true)
})
test('returns true for RegExp subclass instances', () => {
expect(_.isRegExp(new (class extends RegExp {})('.+'))).toBe(true)
})
test('returns true for RegExp instances from other realms', () => {
expect(_.isRegExp(vm.runInNewContext('/.+/'))).toBe(true)
})
test('returns false for undefined', () => {
expect(_.isRegExp(undefined)).toBe(false)
})
test('returns false for null', () => {
expect(_.isRegExp(null)).toBe(false)
})
test('returns false for non-Set objects', () => {
expect(_.isRegExp({})).toBe(false)
})
})

0 comments on commit 58e7d96

Please sign in to comment.