-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Added additional tests to detail the behavior of the split helper #25283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe diff adds multiple new unit tests for the split helper covering block and inline modes (leading/trailing separators, repeated separators producing middle empties, and a string consisting only of separators), plus length/accessor assertions and integration scenarios with foreach and match. It also changes the split helper implementation to filter out empty items from split results and to short-circuit/return early when no non-empty items remain, altering rendering control flow for all-empty inputs. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20–30 minutes
Pre-merge checks and finishing touches❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be reasonable to change this default behaviour for this helper. It is standard JS behaviour, the helper isn't doing the wrong thing, but it's not doing what you need or expect.
Working around it with a match helper is great, but every extra helper you have to use adds overhead in terms of processing but more importantly maintenance of theme code.
I'd happily approve a PR that changed this behaviour, especially this early in the helper's life.
The way I'd think about this would be changing the behaviour "trim" empty strings from the end of the array by default. If we end up crossing the bridge that the default behaviour is desirable for some users they could add trim=false as a property to the helper to disable the trim by default behaviour, but I wouldn't add it unless it was asked for by a theme dev.
|
Hi @ErisDS , thanks for the review! I went ahead and implemented the altered behavior for split to eliminate empty strings anywhere they occur. Given that we're generally using split elements that'll be looped over to produce text, empty items that result in nothing being rendered but that change the length of the array are a potential source of confusion. I added more tests, including the case where the string is nothing but separators. Marking this for re-review since it's in no way what you approved previously, but leaving it on this branch/PR so that we don't lose the context. Thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
ghost/core/core/frontend/helpers/split.js (1)
31-38: Consider documenting the non-standard filtering behavior and remove redundant check.The filtering of empty strings (line 33) deviates from JavaScript's native
split(), which preserves empty strings at leading, trailing, and middle positions. This could surprise users expecting standard behavior or break existing templates that rely on empty elements for positional logic.Additionally, the early return check (lines 36-38) is redundant since
renderResult([])on line 40 would handle an empty array identically.Apply this diff to remove the redundant check:
// Filter out all empty strings const result = string.split(separator) .filter(item => item !== '') .map(item => new SafeString(item)); - if (result.length === 0) { - return renderResult([], options, data); - } - return renderResult(result, options, data);Additionally, consider adding a JSDoc comment documenting this non-standard behavior:
/** * Splits a string by separator and returns non-empty segments as SafeStrings. * Note: Unlike JavaScript's native split(), this filters out all empty strings * from the result, including those from leading/trailing/repeated separators. */ module.exports = function split(...args) {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
ghost/core/core/frontend/helpers/split.js(1 hunks)ghost/core/test/unit/frontend/helpers/split.test.js(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
ghost/core/core/frontend/**
📄 CodeRabbit inference engine (AGENTS.md)
Frontend and theme rendering for core should be under ghost/core/core/frontend/
Files:
ghost/core/core/frontend/helpers/split.js
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: Ghost-CLI tests
- GitHub Check: Acceptance tests (Node 22.13.1, mysql8)
- GitHub Check: Build & Push
🔇 Additional comments (1)
ghost/core/test/unit/frontend/helpers/split.test.js (1)
115-151: Excellent test coverage for the empty-string filtering behavior.These tests comprehensively validate the new filtering behavior across all edge cases:
- Leading and trailing separators (no boundary empties)
- Repeated separators (no middle empties)
- String consisting only of separators (empty result)
- Practical use case of suffix removal
The dual assertions (output + length check) effectively verify both the content and array size after filtering.
Splitting an element with a trailing separator results in a "" element being included in the returned array. This is typical JavaScript behavior, which I'm preserving here (although I don't love it). This PR adds some additional tests that show this behavior.
Will also add to docs.