Skip to content

fix: avoid mutating source array - #16346

Merged
kgryte merged 2 commits into
stdlib-js:developfrom
sarthakkaushik927:fix/fixed-endian-factory-with
Oct 29, 2026
Merged

kgryte merged 2 commits into
stdlib-js:developfrom
sarthakkaushik927:fix/fixed-endian-factory-with

Conversation

@sarthakkaushik927

@sarthakkaushik927 sarthakkaushik927 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • fixes the with method in array/fixed-endian-factory, which created the returned array from the same underlying ArrayBuffer as the source array. As a consequence, (1) setting the new value mutated the source array and (2) for arrays which are views on a buffer having a non-zero byte offset and/or a shorter length, the returned array had the wrong length and the new value was written at the wrong position.

    var Float64ArrayFE = fixedEndianFactory( 'float64' );
    
    var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
    var out = arr.with( 0, 99.0 );
    
    var str = arr.toString();
    // before: '99,2,3'
    // after: '1,2,3'
    
    var buf = new ArrayBuffer( 32 );
    arr = new Float64ArrayFE( 'little-endian', buf, 8, 2 );
    arr.set( [ 5.0, 6.0 ] );
    out = arr.with( 0, 7.0 );
    
    str = out.toString();
    // before: '7,5,6,0'
    // after: '7,6'
  • copies the source elements into a newly allocated array before setting the provided value, and adds regression tests for source mutation, views on a buffer, and byte order preservation.

Related Issues

Does this pull request have any related issues?

No.

Reference Packages

Did the content of this pull request follow one or more existing packages? Answering is optional.

This pull request followed these packages:

  • array/fixed-endian-factory (existing filter and map methods)

What carried over from the reference(s)?

  • element-wise copying via the data view getter/setter and test structure

What differed, or what did you have to work out yourself?

  • none.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Documentation (including examples)
  • Test/benchmark generation
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution.

I used Claude Code to help generate the tests and documentation. I reviewed all changes and ran the package tests locally.


@stdlib-js/reviewers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: sarthakkaushik927 <sarthakkaushik927@gmail.com>
@sarthakkaushik927
sarthakkaushik927 requested a review from a team October 29, 2026 05:32
@stdlib-bot stdlib-bot added Needs Review A pull request which needs code review. First-time Contributor A pull request from a contributor who has never previously committed to the project repository. labels Oct 29, 2026
Comment thread lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js Outdated
Comment thread lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.with.js Outdated

@kgryte kgryte left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left initial comments.

@kgryte kgryte added difficulty: 1 Low degree of difficulty. Should be straightforward to implement and/or resolve. Needs Changes Pull request which needs changes before being merged. review: 3 and removed Needs Review A pull request which needs code review. labels Oct 29, 2026
Copy the underlying bytes directly instead of copying element-by-element
via accessors, and avoid testing private properties.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: sarthakkaushik927 <sarthakkaushik927@gmail.com>
@kgryte kgryte removed the Needs Changes Pull request which needs changes before being merged. label Oct 29, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
array/fixed-endian-factory $\\color{red}1238/1445$
$\\color{green}+85.67\\%$
$\\color{red}161/185$
$\\color{green}+87.03\\%$
$\\color{red}25/33$
$\\color{green}+75.76\\%$
$\\color{red}1238/1445$
$\\color{green}+85.67\\%$

The above coverage report was generated for the changes in this PR.

@kgryte

kgryte commented Oct 29, 2026

Copy link
Copy Markdown
Member

As the CI failure is not due to changes introduced in this PR, I'll go ahead and merge.

@kgryte kgryte changed the title fix: avoid mutating source array in array/fixed-endian-factory#with fix: avoid mutating source array Oct 29, 2026
@kgryte
kgryte merged commit a163565 into stdlib-js:develop Oct 29, 2026
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

difficulty: 1 Low degree of difficulty. Should be straightforward to implement and/or resolve. First-time Contributor A pull request from a contributor who has never previously committed to the project repository. review: 3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants