Skip to content

Commit b797c65

Browse files
committed
Auto-generated commit
1 parent d3ad9c0 commit b797c65

File tree

13 files changed

+772
-1
lines changed

13 files changed

+772
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
### Features
1212

13+
- [`addcce3`](https://github.com/stdlib-js/stdlib/commit/addcce3ebb1f88d8c258f34a03c61b6eb14e5c87) - add `toInsertedAt` to namespace
14+
- [`b0b7cf6`](https://github.com/stdlib-js/stdlib/commit/b0b7cf605445a13f21685c78d55a477cf86e2655) - add `insertAt` to namespace
15+
- [`8d0488a`](https://github.com/stdlib-js/stdlib/commit/8d0488a14aeae75e8d9bba5b37157eff56cc61ec) - add `array/base/insert-at`
1316
- [`77e0108`](https://github.com/stdlib-js/stdlib/commit/77e010849948df11c24123f781e9ca7689be44ff) - add `array/base/to-inserted-at` [(#6807)](https://github.com/stdlib-js/stdlib/pull/6807)
1417
- [`b066b48`](https://github.com/stdlib-js/stdlib/commit/b066b488b56954ec0761d625f3b0f820a65ff94c) - add missing functions to namespace
1518
- [`c8567dc`](https://github.com/stdlib-js/stdlib/commit/c8567dc46194708845553af0631c54cc81b25170) - add `rekeyViews` to namespace
@@ -248,6 +251,10 @@ A total of 33 issues were closed in this release:
248251

249252
<details>
250253

254+
- [`addcce3`](https://github.com/stdlib-js/stdlib/commit/addcce3ebb1f88d8c258f34a03c61b6eb14e5c87) - **feat:** add `toInsertedAt` to namespace _(by Athan Reines)_
255+
- [`b0b7cf6`](https://github.com/stdlib-js/stdlib/commit/b0b7cf605445a13f21685c78d55a477cf86e2655) - **feat:** add `insertAt` to namespace _(by Athan Reines)_
256+
- [`8d0488a`](https://github.com/stdlib-js/stdlib/commit/8d0488a14aeae75e8d9bba5b37157eff56cc61ec) - **feat:** add `array/base/insert-at` _(by Athan Reines)_
257+
- [`2384637`](https://github.com/stdlib-js/stdlib/commit/2384637c93769537900b4b4bde3b183dabe80912) - **docs:** fix example _(by Athan Reines)_
251258
- [`77e0108`](https://github.com/stdlib-js/stdlib/commit/77e010849948df11c24123f781e9ca7689be44ff) - **feat:** add `array/base/to-inserted-at` [(#6807)](https://github.com/stdlib-js/stdlib/pull/6807) _(by Shabareesh Shetty, Athan Reines, stdlib-bot)_
252259
- [`627ead4`](https://github.com/stdlib-js/stdlib/commit/627ead48e08fcf20d5cb72c14014bb9df478adf9) - **docs:** fix example _(by Athan Reines)_
253260
- [`ef39db1`](https://github.com/stdlib-js/stdlib/commit/ef39db15d5ced85b67f9652862ce8c51321b6bcc) - **docs:** fix example _(by Athan Reines)_

base/insert-at/README.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# insertAt
22+
23+
> Insert an element into an array.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var insertAt = require( '@stdlib/array/base/insert-at' );
41+
```
42+
43+
#### insertAt( x, index, value )
44+
45+
Inserts an element into an array.
46+
47+
```javascript
48+
var x = [ 1, 1, 2, 3, 3 ];
49+
50+
var y = insertAt( x, -3, 4 );
51+
// returns [ 1, 1, 4, 2, 3, 3 ]
52+
53+
var bool = ( x === y );
54+
// returns true
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: an input array.
60+
- **index**: element index.
61+
- **value**: value to insert.
62+
63+
</section>
64+
65+
<!-- /.usage -->
66+
67+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
68+
69+
<section class="notes">
70+
71+
## Notes
72+
73+
- Negative indices are resolved relative to the last array element, with the last element corresponding to `-1`.
74+
- If provided an out-of-bounds index, the function clamps the index to the beginning or end of the array.
75+
- The function mutates the input array.
76+
77+
</section>
78+
79+
<!-- /.notes -->
80+
81+
<!-- Package usage examples. -->
82+
83+
<section class="examples">
84+
85+
## Examples
86+
87+
<!-- eslint no-undef: "error" -->
88+
89+
```javascript
90+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
91+
var randi = require( '@stdlib/random/base/discrete-uniform' );
92+
var insertAt = require( '@stdlib/array/base/insert-at' );
93+
94+
// Create an array of random numbers:
95+
var x = discreteUniform( 10, 0, 5, {
96+
'dtype': 'generic'
97+
});
98+
// returns [...]
99+
100+
console.log( x );
101+
102+
// Insert a random element:
103+
var y = insertAt( x, randi( 0, x.length ), randi( 100, 105 ) );
104+
// returns [...]
105+
106+
console.log( y );
107+
```
108+
109+
</section>
110+
111+
<!-- /.examples -->
112+
113+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
114+
115+
<section class="references">
116+
117+
</section>
118+
119+
<!-- /.references -->
120+
121+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
122+
123+
<section class="related">
124+
125+
</section>
126+
127+
<!-- /.related -->
128+
129+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
130+
131+
<section class="links">
132+
133+
</section>
134+
135+
<!-- /.links -->

base/insert-at/benchmark/benchmark.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var pkg = require( './../package.json' ).name;
27+
var insertAt = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':index=-1', function benchmark( b ) {
33+
var out;
34+
var x;
35+
var i;
36+
37+
x = uniform( 1, 0.0, 10.0, {
38+
'dtype': 'generic'
39+
});
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
out = insertAt( x, -1, i );
44+
if ( out.length === i+100 ) {
45+
b.fail( 'unexpected length' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isArray( out ) ) {
50+
b.fail( 'should return an array' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
55+
56+
bench( pkg+':index=0', function benchmark( b ) {
57+
var out;
58+
var x;
59+
var i;
60+
61+
x = uniform( 1, 0.0, 10.0, {
62+
'dtype': 'generic'
63+
});
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
out = insertAt( x, 0, i );
68+
if ( out.length === i+100 ) {
69+
b.fail( 'unexpected length' );
70+
}
71+
}
72+
b.toc();
73+
if ( !isArray( out ) ) {
74+
b.fail( 'should return an array' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
});

base/insert-at/docs/repl.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}( x, index, value )
3+
Inserts an element into an array.
4+
5+
Negative indices are resolved relative to the last array element, with the
6+
last element corresponding to `-1`.
7+
8+
If provided an out-of-bounds index, the function clamps the index to the
9+
beginning or end of the array.
10+
11+
The function mutates the input array.
12+
13+
Parameters
14+
----------
15+
x: Array
16+
Input array.
17+
18+
index: integer
19+
Element index.
20+
21+
value: any
22+
Value to insert.
23+
24+
Returns
25+
-------
26+
out: Array
27+
Input array.
28+
29+
Examples
30+
--------
31+
> var x = [ 1, 1, 2, 3, 3 ];
32+
> var out = {{alias}}( x, -3, 4 )
33+
[ 1, 1, 4, 2, 3, 3 ]
34+
> var bool = ( out === x )
35+
true
36+
37+
See Also
38+
--------
39+

base/insert-at/docs/types/index.d.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Inserts an element into an array.
23+
*
24+
* ## Notes
25+
*
26+
* - The function mutates the input array.
27+
*
28+
* @param x - input array
29+
* @param index - element index
30+
* @param value - value to insert
31+
* @returns input array
32+
*
33+
* @example
34+
* var x = [ 1, 1, 2, 3, 3 ];
35+
*
36+
* var y = insertAt( x, -3, 4 );
37+
* // returns [ 1, 1, 4, 2, 3, 3 ]
38+
*
39+
* var bool = ( x === y );
40+
* // returns true
41+
*/
42+
declare function insertAt<T = unknown>( x: Array<T>, index: number, value: T ): Array<T>;
43+
44+
45+
// EXPORTS //
46+
47+
export = insertAt;

base/insert-at/docs/types/test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import insertAt = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array...
25+
{
26+
insertAt( [ 1, 2, 3 ], -1, 4 ); // $ExpectType number[]
27+
}
28+
29+
// The compiler throws an error if the function is provided a first argument which is not an array...
30+
{
31+
insertAt( '5', -1, 4 ); // $ExpectError
32+
insertAt( 5, -1, 4 ); // $ExpectError
33+
insertAt( true, -1, 4 ); // $ExpectError
34+
insertAt( false, -1, 4 ); // $ExpectError
35+
insertAt( null, -1, 4 ); // $ExpectError
36+
insertAt( void 0, -1, 4 ); // $ExpectError
37+
insertAt( {}, -1, 4 ); // $ExpectError
38+
insertAt( ( x: number ): number => x, -1, 4 ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided a second argument which is not a number...
42+
{
43+
const x = [ 1, 2, 3 ];
44+
45+
insertAt( x, '5', 4 ); // $ExpectError
46+
insertAt( x, true, 4 ); // $ExpectError
47+
insertAt( x, false, 4 ); // $ExpectError
48+
insertAt( x, null, 4 ); // $ExpectError
49+
insertAt( x, void 0, 4 ); // $ExpectError
50+
insertAt( x, {}, 4 ); // $ExpectError
51+
insertAt( x, [], 4 ); // $ExpectError
52+
insertAt( x, ( x: number ): number => x, 4 ); // $ExpectError
53+
}
54+
55+
// The compiler throws an error if the function is provided an unsupported number of arguments...
56+
{
57+
const x = [ 1, 2, 3 ];
58+
59+
insertAt(); // $ExpectError
60+
insertAt( x ); // $ExpectError
61+
insertAt( x, -1 ); // $ExpectError
62+
insertAt( x, -1, 4, {} ); // $ExpectError
63+
}

0 commit comments

Comments
 (0)