From 58e7d96baed7ea73e09dff3884aeebb6150f9485 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Wed, 3 Jul 2024 17:36:43 -0400 Subject: [PATCH] feat: add `isRegExp` function (#77) --- benchmarks/typed/isRegExp.bench.ts | 9 +++++++++ docs/typed/isRegExp.mdx | 17 +++++++++++++++++ src/mod.ts | 1 + src/typed/isRegExp.ts | 5 +++++ tests/typed/isRegExp.test.ts | 23 +++++++++++++++++++++++ 5 files changed, 55 insertions(+) create mode 100644 benchmarks/typed/isRegExp.bench.ts create mode 100644 docs/typed/isRegExp.mdx create mode 100644 src/typed/isRegExp.ts create mode 100644 tests/typed/isRegExp.test.ts diff --git a/benchmarks/typed/isRegExp.bench.ts b/benchmarks/typed/isRegExp.bench.ts new file mode 100644 index 00000000..7c590dc0 --- /dev/null +++ b/benchmarks/typed/isRegExp.bench.ts @@ -0,0 +1,9 @@ +import * as _ from 'radashi' +import { bench } from 'vitest' + +describe('isRegExp', () => { + bench('with no arguments', () => { + _.isRegExp() + }) +}) + diff --git a/docs/typed/isRegExp.mdx b/docs/typed/isRegExp.mdx new file mode 100644 index 00000000..7c944ddb --- /dev/null +++ b/docs/typed/isRegExp.mdx @@ -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 +``` diff --git a/src/mod.ts b/src/mod.ts index d541d7b3..62ac1404 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -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' diff --git a/src/typed/isRegExp.ts b/src/typed/isRegExp.ts new file mode 100644 index 00000000..50932516 --- /dev/null +++ b/src/typed/isRegExp.ts @@ -0,0 +1,5 @@ +import { isTagged } from 'radashi' + +export function isRegExp(value: unknown): value is RegExp { + return isTagged(value, '[object RegExp]') +} diff --git a/tests/typed/isRegExp.test.ts b/tests/typed/isRegExp.test.ts new file mode 100644 index 00000000..b2eee9ef --- /dev/null +++ b/tests/typed/isRegExp.test.ts @@ -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) + }) +})