fix: splitUnit returns original unit when called as a method with array parts (#3644)#3671
Open
MaksZhukov wants to merge 1 commit into
Open
Conversation
When splitUnit is called as a method in the expression parser (e.g. `(1 m).splitUnit([ft, in])`), the parts argument is passed as a Matrix rather than a plain Array. Unit.prototype.splitUnit iterated over `parts.length`, which is undefined for a Matrix, so the loop was skipped and the original unit was returned unchanged. Normalize `parts` to an Array (via toArray) when it is a Matrix so the method-call form behaves the same as the function-call form. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
What
Fixes the
splitUnitmethod-call form in the expression parser, which silently returned the original unit unchanged.Closes #3644.
Why
Unit.prototype.splitUnititerates overparts.length. WhensplitUnitis invoked as a method in the expression parser (e.g.(1 m).splitUnit([ft, in])), the parser dispatches the call directly to the prototype method viagetSafeMethod/fn.apply(object, values), passing the array literal as a Matrix (the defaultmatrix: 'Matrix'config) rather than a plain Array.A
Matrixhas no.lengthproperty, soparts.lengthisundefined, theforloop never executes, and the method falls through to returning[this]— the original, unsplit unit.The function form
splitUnit(1 m, [ft, in])was unaffected because it goes through the typed-function'Unit, Array'signature, which converts the Matrix to an Array before reaching the prototype method.Fix
Normalize
partsto an Array (viatoArray()) when it is a Matrix, so the method-call form behaves identically to the function-call form. Minimal change, no new dependencies.How to test
A regression test covering the Matrix/method-call form was added to
test/unit-tests/type/unit/function/splitUnit.test.js. The full unit-test suite (npx mocha test/unit-tests) passes (6653 passing) andeslintis clean on the changed files.🤖 Generated with Claude Code