plane
plane copied to clipboard
fix: add optional chaining in orderArrayBy to prevent crashes
Add optional chaining operator (?.) when accessing nested object properties in the orderArrayBy function. This prevents TypeError when encountering null or undefined values in the object chain.
Without this fix, accessing nested properties like 'user.profile.name' would crash if 'user' or 'profile' is null/undefined.
Changes:
- obj[i] -> obj?.[i] in reduce function for keyA
- obj[i] -> obj?.[i] in reduce function for keyB
Description
Type of Change
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] Feature (non-breaking change which adds functionality)
- [ ] Improvement (change that would cause existing functionality to not work as expected)
- [ ] Code refactoring
- [ ] Performance improvements
- [ ] Documentation update
Screenshots and Media (if applicable)
Test Scenarios
References
[!NOTE] Use optional chaining when resolving nested sort keys and push null/undefined values to the end during sorting.
- Utilities
packages/utils/src/array.ts:
- Update
orderArrayByto resolve nested keys withobj?.[i]to avoid crashes onnull/undefined.- Adjust comparator to push
null/undefinedvalues to the end while preserving ascending/descending logic.Written by Cursor Bugbot for commit 76df866e3ac0597e1026c66bfbff18b71239d293. This will update automatically on new commits. Configure here.
Summary by CodeRabbit
- Bug Fixes
- Safer handling of nested keys during array sorting to avoid errors when intermediate values are missing.
- Entries with undefined or null sort keys are now placed at the end of sorted results for more predictable ordering.
[!NOTE]
Other AI code review bot(s) detected
CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.
Walkthrough
orderArrayBy in array utilities now uses optional chaining (obj?.[i]) to extract nested keys and treats undefined/null key values by placing those entries at the end of the sorted result.
Changes
| Cohort / File(s) | Change Summary |
|---|---|
Array utilities safety & null handlingpackages/utils/src/array.ts |
orderArrayBy now uses optional chaining (obj?.[i]) for nested key extraction and updates sort behavior to push entries with undefined or null key values to the end of the sorted array. Public signatures unchanged. |
Estimated code review effort
๐ฏ 2 (Simple) | โฑ๏ธ ~10 minutes
- Verify optional chaining yields intended undefined behavior for missing nested keys
- Confirm sort comparison treats
undefined/nullconsistently and places those items last - Check behavior across typical environments (Node/browser) and edge cases with mixed present/missing paths
Poem
๐ฐ
Hops through keys now soft and light,
Optional chains keep errors light,
Nulls and undefined tucked to the end,
Sorting hums on, my work's well-spent,
Thump-thump โ a safe, neat little mend. ๐ฅ
Pre-merge checks and finishing touches
โ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | โ Passed | The title clearly and concisely summarizes the main change: adding optional chaining to the orderArrayBy function to prevent crashes from null/undefined values. |
| Description check | โ Passed | The description includes a clear explanation of the problem, the specific changes made, and the Type of Change checkbox is properly filled (Bug fix). However, Test Scenarios and References sections are empty placeholders. |
| Docstring Coverage | โ Passed | No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. |
โจ Finishing touches
- [ ] ๐ Generate docstrings
๐งช Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
๐ Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
๐ฅ Commits
Reviewing files that changed from the base of the PR and between b0381f7ff250d7de4685d6bedddb0fb93acbc372 and 76df866e3ac0597e1026c66bfbff18b71239d293.
๐ Files selected for processing (1)
packages/utils/src/array.ts(1 hunks)
๐ง Files skipped from review as they are similar to previous changes (1)
- packages/utils/src/array.ts
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 @coderabbitai help to get the list of available commands and usage tips.