README.md (5083B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # push 22 23 > Add one or more elements to the end of a collection. 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 push = require( '@stdlib/utils/push' ); 41 ``` 42 43 #### push( collection, ...items ) 44 45 Adds one or more elements to the end of a `collection`. A `collection` may be either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (i.e., an [`Object`][mdn-object] having a valid writable `length` property). 46 47 ```javascript 48 var arr = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; 49 50 var out = push( arr, 6.0, 7.0 ); 51 // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ] 52 53 var bool = ( out === arr ); 54 // returns true 55 ``` 56 57 In contrast to [`Array.prototype.push`][mdn-array-push], the function returns the extended collection, rather than the collection length. For [typed arrays][mdn-typed-array], the returned value is a new [typed array][mdn-typed-array] view whose underlying [`ArrayBuffer`][mdn-arraybuffer] may **not** equal the underlying [`ArrayBuffer`][mdn-arraybuffer] for the input `collection`. 58 59 ```javascript 60 var ArrayBuffer = require( '@stdlib/array/buffer' ); 61 var Float64Array= require( '@stdlib/array/float64' ); 62 63 var buf = new ArrayBuffer( 3*8 ); // 8 bytes per double 64 65 var arr = new Float64Array( buf, 0, 2 ); 66 arr[ 0 ] = 1.0; 67 arr[ 1 ] = 2.0; 68 69 var out = push( arr, 3.0 ); 70 // returns <Float64Array>[ 1.0, 2.0, 3.0 ] 71 72 var bool = ( out === arr ); 73 // returns false 74 75 bool = ( out.buffer === arr.buffer ); 76 // returns true 77 78 out = push( out, 4.0 ); 79 // returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ] 80 81 bool = ( out.buffer === arr.buffer ); 82 // returns false 83 ``` 84 85 </section> 86 87 <!-- /.usage --> 88 89 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 90 91 <section class="notes"> 92 93 ## Notes 94 95 - The function adds one or more elements to a [typed array][mdn-typed-array] by setting values in the underlying [`ArrayBuffer`][mdn-arraybuffer]. If an [`ArrayBuffer`][mdn-arraybuffer] does not have enough bytes in which to store all elements, the function allocates a new [`ArrayBuffer`][mdn-arraybuffer] capable of holding `2^n` elements, where `n` is the next power of `2`. This procedure is similar to how environments internally handle dynamic memory allocation for [`Arrays`][mdn-array]. 96 - Beware when providing [typed arrays][mdn-typed-array] which are views pointing to a shared (or pooled) [`ArrayBuffer`][mdn-arraybuffer]. Because the function sets [`ArrayBuffer`][mdn-arraybuffer] bytes outside of a provided [view][mdn-typed-array], the function may overwrite bytes belonging to one or more external views. This could be a potential **security vulnerability**. Prefer providing [typed arrays][mdn-typed-array] which have an exclusive [`ArrayBuffer`][mdn-arraybuffer]; otherwise, be sure to plan for and guard against mutated state. 97 98 </section> 99 100 <!-- /.notes --> 101 102 <!-- Package usage examples. --> 103 104 <section class="examples"> 105 106 ## Examples 107 108 <!-- eslint no-undef: "error" --> 109 110 ```javascript 111 var Float64Array= require( '@stdlib/array/float64' ); 112 var push = require( '@stdlib/utils/push' ); 113 114 var arr; 115 var i; 116 117 arr = new Float64Array(); 118 for ( i = 0; i < 100; i++ ) { 119 arr = push( arr, i ); 120 } 121 console.log( arr ); 122 ``` 123 124 </section> 125 126 <!-- /.examples --> 127 128 <!-- 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. --> 129 130 <section class="references"> 131 132 </section> 133 134 <!-- /.references --> 135 136 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 137 138 <section class="links"> 139 140 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 141 142 [mdn-array-push]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push 143 144 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays 145 146 [mdn-arraybuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer 147 148 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object 149 150 </section> 151 152 <!-- /.links -->