Str — String Utilities
Strings are the primary medium for untrusted data entering our applications. Whether we are parsing HTTP headers, processing CSV uploads, or cleaning up user-submitted text fields, string transformation is a constant necessity.
In standard JavaScript, the String prototype offers a wide array of methods. However, because
these methods are “data-first” — called directly on the string object itself — they do not compose
cleanly. When we write functional pipelines using pipe, we are forced to wrap prototype calls in
anonymous arrow functions (e.g., s => s.trim()), which breaks the natural flow of data and
introduces visual noise.
Str provides standard string operations as pure, curried, data-last functions. It turns prototype
operations into modular building blocks that fit directly into pipelines, alongside safe numeric
parsers that model parsing failures using the Maybe type.
The problem with prototype chaining and unsafe parsing
Section titled “The problem with prototype chaining and unsafe parsing”Consider a backend handler that parses a comma-separated list of tags from a form submission:
This prototype-chaining approach is familiar, but it only works when the operations are methods on the string class. The moment we want to mix in custom helpers, external validations, or conditional transformations, the chain breaks. We are forced to intercept the chain or assign intermediate variables:
Furthermore, standard parsing functions like parseInt are unsafe: they return NaN when given
invalid input, which silently propagates through calculations and eventually causes runtime errors
far from the source of the bad data.
The shift to pipeline-ready operations
Section titled “The shift to pipeline-ready operations”Str treats string transformations as independent, modular steps. By shifting the data argument to
the last position and currying the parameters, string operations can be composed point-free directly
inside pipe.
flowchart TD
A["Raw Input"] --> B["Str.trim"]
B --> C["Str.split(',')"]
C --> D["Arr.map(Str.trim)"]
D --> E["Arr.filter(Str.includes('...'))"]
Transforming cases and cleaning input
Section titled “Transforming cases and cleaning input”Basic formatting operations are wrapped as pure functions that fit cleanly into array transformations and pipe flows:
Splitting and segmenting text
Section titled “Splitting and segmenting text”Standard JavaScript split returns a plain array, but handles multi-line endings and multiple
spaces awkwardly. Str provides robust segmentation helpers:
Replacing text
Section titled “Replacing text”Str.replace and Str.replaceAll are curried wrappers around native string replacement. They
accept the search pattern and replacement string first, leaving the target string for last:
Reusable predicates for filtering
Section titled “Reusable predicates for filtering”String matching operations function as curried predicates, which can be passed directly to array filters:
Safe numeric parsing
Section titled “Safe numeric parsing”Str.parse provides two safe alternatives to standard number parsing. Both Str.parse.int and
Str.parse.float inspect the string and return a Maybe<number> context, eliminating the need for
boilerplate isNaN checks:
These safe parsers compose cleanly to resolve safe fallback defaults:
Composing a string pipeline
Section titled “Composing a string pipeline”By combining these utilities, we can assemble multi-step text cleanup pipelines that are self-documenting and highly maintainable:
Problems it solves
Section titled “Problems it solves”- Form input sanitization in data pipelines: When cleaning user input (such as trimming
whitespace, normalizing casing, removing special characters, or splitting comma-delimited tags),
native string methods require writing manual arrow wrappers inside
pipe.Strprovides curried, data-last combinators (Str.trim,Str.toLowerCase,Str.split,Str.replace) that chain directly. - Safe numeric string conversion: Parsing numbers from HTTP parameters or form text with
Number()orparseInt()yieldsNaNon invalid input without static compiler warnings.Str.toNumberandStr.toIntegerreturnMaybe<number>, ensuring non-numeric inputs are handled safely before doing math. - URL slug generation and search normalisation: Building SEO-friendly article slugs or normalising search queries by chaining case conversion, whitespace collapsing, and character replacement point-free.
- Named string predicates in array pipelines: Filtering arrays of strings (such as finding lines
starting with prefixes, non-empty tags, or matching extensions) with named predicates
(
Str.isNonEmpty,Str.startsWith,Str.contains) without writing inline lambda wrappers. - Cross-platform multi-line and token text parsing: Splitting text into lines or words often
breaks on mixed CRLF/LF line endings or multiple consecutive whitespace characters.
Str.linesandStr.wordshandle cross-platform line breaks and variable spacing automatically.