Add String.hasNonWhitespace predicate (#10341)

Co-authored-by: Effect Bot <tech-ops@effectful.co>
This commit is contained in:
Harry Solovay
2026-11-20 03:56:08 +00:00
committed by effect-bot
co-authored by Effect Bot
parent bae766195b
commit a51559ea7f
3 changed files with 41 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"effect": minor
---
Add `String.hasNonWhitespace`, which checks whether a string contains at least one non-whitespace character.
+25
View File
@@ -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.
*
+11
View File
@@ -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(""))