Add trimValues() and formatValuesUsing() to the reader - #195
Merged
Conversation
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>
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'strim. 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
LazyCollectiongenerator, so the package keeps its low memory usage. Includes tests and README docs.Open question for discussion
I gave
trimValues()an optional$charactersargument to mirror the existingtrimHeaderRow($characters). It has one sharp edge: since it's passed straight to PHP'strim(), the argument is a set of characters stripped from both ends — not a suffix. SotrimValues('.com')would also turnTomintoT, which is easy to misread.Because
formatValuesUsing()already lets callers do any custom trimming with full control, I'm happy to drop the$charactersargument and keeptrimValues()as a simple whitespace-only helper if you'd prefer. Let me know which way you'd like it.