Skip to content

Add trimValues() and formatValuesUsing() to the reader - #195

Merged
freekmurze merged 2 commits into
spatie:mainfrom
mhodge:feature/format-values-on-read
Jun 15, 2026
Merged

Add trimValues() and formatValuesUsing() to the reader#195
freekmurze merged 2 commits into
spatie:mainfrom
mhodge:feature/format-values-on-read

Conversation

@mhodge

@mhodge mhodge commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Why

The reader can already clean up header names (trimHeaderRow(), headersToSnakeCase(), formatHeadersUsing()) but offers nothing for the values themselves. Importing user-supplied files usually means dealing with stray whitespace or values that need normalizing, which today forces a manual ->map() over every row.

What

Two symmetric reader methods:

  • trimValues(?string $characters = null) — trims whitespace (or the given characters) from every value. The argument is a set of characters stripped from both ends, like PHP's trim. Non-string values (e.g. dates) are left untouched.
  • formatValuesUsing(callable $callback) — runs each value through a closure receiving ($value, $key), so values can be normalized per column.

Formatting happens per row inside the LazyCollection generator, so the package keeps its low memory usage. Includes tests and README docs.

Open question for discussion

I gave trimValues() an optional $characters argument to mirror the existing trimHeaderRow($characters). It has one sharp edge: since it's passed straight to PHP's trim(), the argument is a set of characters stripped from both ends — not a suffix. So trimValues('.com') would also turn Tom into T, which is easy to misread.

Because formatValuesUsing() already lets callers do any custom trimming with full control, I'm happy to drop the $characters argument and keep trimValues() as a simple whitespace-only helper if you'd prefer. Let me know which way you'd like it.

mhodge and others added 2 commits June 15, 2026 12:59
@
Add trimValues() and formatValuesUsing() to the reader

The reader could already clean up header names (trimHeaderRow,
headersToSnakeCase, formatHeadersUsing) but had no equivalent for the
actual cell values. This adds two symmetric reader methods:

- trimValues(?string $characters = null)
- formatValuesUsing(callable $callback)  // receives ($value, $key)

Non-string values (e.g. dates) are left untouched by trimValues().
Formatting runs per-row inside the LazyCollection, preserving the
package low memory usage. Includes tests and README docs.
@
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@freekmurze
freekmurze merged commit 2aa4cd9 into spatie:main Jun 15, 2026
9 checks passed
@freekmurze

Copy link
Copy Markdown
Member

Thanks a lot for this, @mhodge! Nicely done: symmetric to the existing header helpers, kept the per-row processing inside the lazy generator so memory usage stays low, and well covered with tests. I kept the optional $characters argument on trimValues() for consistency with trimHeaderRow(), and pushed one small commit strengthening the no-header-row test so it actually asserts trimming on the whitespace rows. Merged and released as 3.10.0. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants