From a51559ea7fe812892be70ee752eaae7c7227ee3f Mon Sep 17 00:00:00 2001 From: Harry Solovay <4893548+harrysolovay@users.noreply.github.com> Date: Tue, 3 Nov 2026 16:12:23 -0500 Subject: [PATCH] Add String.hasNonWhitespace predicate (#10341) Co-authored-by: Effect Bot --- .changeset/string-has-non-whitespace.md | 5 +++++ packages/effect/src/String.ts | 25 +++++++++++++++++++++++++ packages/effect/test/String.test.ts | 11 +++++++++++ 3 files changed, 41 insertions(+) create mode 100644 .changeset/string-has-non-whitespace.md diff --git a/.changeset/string-has-non-whitespace.md b/.changeset/string-has-non-whitespace.md new file mode 100644 index 0000000000..c3f6785253 --- /dev/null +++ b/.changeset/string-has-non-whitespace.md @@ -0,0 +1,5 @@ +--- +"effect": minor +--- + +Add `String.hasNonWhitespace`, which checks whether a string contains at least one non-whitespace character. diff --git a/packages/effect/src/String.ts b/packages/effect/src/String.ts index f284310bae..5b6be4b387 100644 --- a/packages/effect/src/String.ts +++ b/packages/effect/src/String.ts @@ -470,6 +470,31 @@ interface WellFormedNative { export const isWellFormed = (self: string): boolean => (self as WellFormedNative).isWellFormed?.() ?? !loneSurrogate.test(self) +/** + * Checks whether a string contains at least one non-whitespace character. + * + * **Details** + * + * Uses JavaScript whitespace semantics, matching `trim`. Empty strings and + * whitespace-only strings return `false`; the input is not modified. + * + * **Example** (Checking for non-whitespace characters) + * + * ```ts import.meta.vitest + * import { String } from "effect" + * + * String.hasNonWhitespace("hello") // => true + * String.hasNonWhitespace(" hello ") // => true + * String.hasNonWhitespace("") // => false + * String.hasNonWhitespace(" \t\n") // => false + * ``` + * + * @stability stable + * @category predicates + * @since 4.2.0 + */ +export const hasNonWhitespace = (self: string): boolean => /\S/u.test(self) + /** * Returns the JavaScript string length, measured in UTF-16 code units. * diff --git a/packages/effect/test/String.test.ts b/packages/effect/test/String.test.ts index 0296474177..581dbc87b8 100644 --- a/packages/effect/test/String.test.ts +++ b/packages/effect/test/String.test.ts @@ -179,6 +179,17 @@ describe("String", () => { }) }) + describe("hasNonWhitespace", () => { + it("requires a character that trim does not remove", () => { + assertFalse(S.hasNonWhitespace("")) + assertFalse(S.hasNonWhitespace(" \t\n\r\v\f")) + assertFalse(S.hasNonWhitespace("\u00a0\u1680\u2000\u2028\u2029\u202f\u205f\u3000\ufeff")) + assertTrue(S.hasNonWhitespace(" a ")) + assertTrue(S.hasNonWhitespace("\u200b")) + assertTrue(S.hasNonWhitespace("\u0085")) + }) + }) + describe("isEmpty", () => { it("returns true for empty string", () => { assertTrue(S.isEmpty(""))