mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-05 03:45:36 +08:00
Add String.hasNonWhitespace predicate (#10341)
Co-authored-by: Effect Bot <tech-ops@effectful.co>
This commit is contained in:
committed by
effect-bot
co-authored by
Effect Bot
parent
bae766195b
commit
a51559ea7f
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"effect": minor
|
||||
---
|
||||
|
||||
Add `String.hasNonWhitespace`, which checks whether a string contains at least one non-whitespace character.
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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(""))
|
||||
|
||||
Reference in New Issue
Block a user