Skip to content
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

feat: add functional equivalent of Array.prototype.copyWithin and TypedArray.prototype.copyWithin #6516

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions lib/node_modules/@stdlib/utils/copy-within/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<!--

@license Apache-2.0

Copyright (c) 2025 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# copyWithin

> Copy a sequence of elements within a collection to another location in the same collection.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var copyWithin = require( '@stdlib/utils/copy-within' );
```

#### copyWithin( collection, target\[, start\[, end]] )

Copies a sequence of elements within a collection to another location in the same collection.

```javascript
var arr = [ 1, 2, 3, 4, 5 ];

var out = copyWithin( arr, 0, 3 );
// returns [ 4, 5, 3, 4, 5 ]
```

The function accepts the following arguments:

- **collection**: the input collection.
- **target**: target index (zero-based).
- **start**: source start index (zero-based). Default: 0.
- **end**: source end index (zero-based, non-inclusive). Default: collection.length.

If a provided collection has a built-in `copyWithin` method (e.g., [Array.prototype.copyWithin][mdn-copy-within] or [TypedArray.prototype.copyWithin][mdn-typed-array-copy-within]), the function defers to that method. Otherwise, the function provides a polyfill, thus extending `copyWithin` support to environments lacking the built-in API for Array and TypedArray.

The function supports negative indices, which are interpreted as being relative to the end of the collection. For example, if provided `-2` for `target`, the function interprets the index as `collection.length - 2`.

```javascript
var arr = [ 1, 2, 3, 4, 5 ];

var out = copyWithin( arr, -2, -3, -1 );
// returns [ 1, 2, 3, 3, 4 ]
```

If `target` is greater than or equal to the collection length, nothing is copied. If `target` is positioned after `start` after normalization, copying only happens until the end of the collection length (in other words, `copyWithin` never extends the collection).

```javascript
var arr = [ 1, 2, 3, 4, 5 ];

var out = copyWithin( arr, 10, 0 );
// returns [ 1, 2, 3, 4, 5 ]
```

If `start` is greater than or equal to the collection length, nothing is copied.

```javascript
var arr = [ 1, 2, 3, 4, 5 ];

var out = copyWithin( arr, 0, 10 );
// returns [ 1, 2, 3, 4, 5 ]
```

For Uint8Array inputs, the function uses an optimized implementation that copies in 4-byte increments when possible, providing better performance for large arrays.

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- The function works like the built-in `Array.prototype.copyWithin` and `TypedArray.prototype.copyWithin` methods but extends support to all collection types.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var copyWithin = require( '@stdlib/utils/copy-within' );

// Regular arrays:
var arr = [ 1, 2, 3, 4, 5 ];
console.log( copyWithin( arr, 0, 3 ) );
// => [ 4, 5, 3, 4, 5 ]

arr = [ 1, 2, 3, 4, 5 ];
console.log( copyWithin( arr, 0, 3, 4 ) );
// => [ 4, 2, 3, 4, 5 ]

arr = [ 1, 2, 3, 4, 5 ];
console.log( copyWithin( arr, -2, -3, -1 ) );
// => [ 1, 2, 3, 3, 4 ]

// Typed arrays:
var Uint8Array = require( '@stdlib/array/uint8' );
var typed = new Uint8Array( [ 1, 2, 3, 4, 5 ] );
console.log( copyWithin( typed, 0, 3 ) );
// => <Uint8Array>[ 4, 5, 3, 4, 5 ]

// Array-like objects:
var obj = {
'length': 5,
'0': 1,
'1': 2,
'2': 3,
'3': 4,
'4': 5
};
console.log( copyWithin( obj, 0, 3 ) );
// => { '0': 4, '1': 5, '2': 3, '3': 4, '4': 5, 'length': 5 }
```

</section>

<!-- /.examples -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/utils/copy`][@stdlib/utils/copy]</span><span class="delimiter">: </span><span class="description">copy or deep clone a value to an arbitrary depth.</span>

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[mdn-copy-within]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin
[mdn-typed-array-copy-within]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin

<!-- <related-links> -->

[@stdlib/utils/copy]: https://github.com/stdlib-js/utils-copy

<!-- </related-links> -->

</section>

<!-- /.links -->
Loading
Loading