|
| 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 --> |
0 commit comments