plane icon indicating copy to clipboard operation
plane copied to clipboard

fix: add optional chaining in orderArrayBy to prevent crashes

Open hamed-zeidabadi opened this issue 4 weeks ago โ€ข 2 comments

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 orderArrayBy to resolve nested keys with obj?.[i] to avoid crashes on null/undefined.
      • Adjust comparator to push null/undefined values 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.

hamed-zeidabadi avatar Nov 07 '25 16:11 hamed-zeidabadi

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Nov 07 '25 16:11 CLAassistant

[!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 handling
packages/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/null consistently 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.

โค๏ธ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Nov 07 '25 16:11 coderabbitai[bot]