Skip to content

Commit

Permalink
Add isRegExp type guard (#3647)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianbollinger authored and tim-smart committed Sep 25, 2024
1 parent 1c36c3f commit 24eb357
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-oranges-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

Add an `isRegExp` type guard
7 changes: 7 additions & 0 deletions packages/effect/dtslint/Predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ if (Predicate.isTupleOfAtLeast(unknowns, 3)) {
unknowns
}

// -------------------------------------------------------------------------------------
// isRegExp
// -------------------------------------------------------------------------------------

// $ExpectType RegExp[]
unknowns.filter(Predicate.isRegExp)

// -------------------------------------------------------------------------------------
// compose
// -------------------------------------------------------------------------------------
Expand Down
16 changes: 16 additions & 0 deletions packages/effect/src/Predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,22 @@ export const isPromiseLike = (
input: unknown
): input is PromiseLike<unknown> => hasProperty(input, "then") && isFunction(input.then)

/**
* Tests if a value is a `RegExp`.
*
* @param input - The value to test.
*
* @example
* import { Predicate } from "effect"
*
* assert.deepStrictEqual(Predicate.isRegExp(/a/), true)
* assert.deepStrictEqual(Predicate.isRegExp("a"), false)
*
* @category guards
* @since 3.9.0
*/
export const isRegExp = (input: unknown): input is RegExp => input instanceof RegExp

/**
* @since 2.0.0
*/
Expand Down
17 changes: 17 additions & 0 deletions packages/effect/src/RegExp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
*
* @since 2.0.0
*/
import * as predicate from "./Predicate.js"

/**
* Tests if a value is a `RegExp`.
*
* @param input - The value to test.
*
* @example
* import { RegExp } from "effect"
*
* assert.deepStrictEqual(RegExp.isRegExp(/a/), true)
* assert.deepStrictEqual(RegExp.isRegExp("a"), false)
*
* @category guards
* @since 3.9.0
*/
export const isRegExp: (input: unknown) => input is RegExp = predicate.isRegExp

/**
* Escapes special characters in a regular expression pattern.
Expand Down
6 changes: 6 additions & 0 deletions packages/effect/test/Predicate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,10 @@ describe("Predicate", () => {
assert.deepStrictEqual(_.isTupleOfAtLeast([1, 2, 3], 2), true)
assert.deepStrictEqual(_.isTupleOfAtLeast([1, 2, 3], 4), false)
})

it("isRegExp", () => {
assert.deepStrictEqual(_.isRegExp(/a/), true)
assert.deepStrictEqual(_.isRegExp(null), false)
assert.deepStrictEqual(_.isRegExp("a"), false)
})
})
6 changes: 6 additions & 0 deletions packages/effect/test/RegExp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import * as RegExp from "effect/RegExp"
import { describe, expect, it } from "vitest"

describe("RegExp", () => {
it("isRegExp", () => {
expect(RegExp.isRegExp(/a/)).toEqual(true)
expect(RegExp.isRegExp(null)).toEqual(false)
expect(RegExp.isRegExp("a")).toEqual(false)
})

describe("escape", () => {
it("should escape special characters correctly", () => {
const testCases: Array<[string, string]> = [
Expand Down

0 comments on commit 24eb357

Please sign in to comment.