time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

push.js (2606B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 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 isArray = require( '@stdlib/assert/is-array' );
     24 var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' );
     25 var isInteger = require( '@stdlib/assert/is-integer' );
     26 var pushArray = require( './push_array.js' );
     27 var pushObject = require( './push_object.js' );
     28 var pushTypedArray = require( './push_typed_array.js' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Adds one or more elements to the end of a collection.
     35 *
     36 * @param {(Array|TypedArray|Object)} collection - collection
     37 * @param {...*} items - items to add
     38 * @throws {TypeError} must provide either an array, typed array, or an array-like object
     39 * @returns {(Array|TypedArray|Object)} updated collection
     40 *
     41 * @example
     42 * var arr = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
     43 *
     44 * arr = push( arr, 6.0, 7.0 );
     45 * // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]
     46 *
     47 * @example
     48 * var Float64Array = require( '@stdlib/array/float64' );
     49 *
     50 * var arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
     51 * // returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
     52 *
     53 * arr = push( arr, 6.0, 7.0 );
     54 * // returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]
     55 */
     56 function push( collection ) {
     57 	var items;
     58 	var i;
     59 	items = new Array( arguments.length-1 );
     60 	for ( i = 0; i < arguments.length-1; i++ ) {
     61 		items[ i ] = arguments[ i+1 ];
     62 	}
     63 	if ( isArray( collection ) ) {
     64 		return pushArray( collection, items );
     65 	}
     66 	// Check for a typed-array-like object, as verifying actual typed arrays is expensive...
     67 	if ( isTypedArrayLike( collection ) ) {
     68 		return pushTypedArray( collection, items );
     69 	}
     70 	// Check for an array-like object...
     71 	if (
     72 		collection !== null &&
     73 		typeof collection === 'object' &&
     74 		typeof collection.length === 'number' &&
     75 		isInteger( collection.length ) &&
     76 		collection.length >= 0
     77 	) {
     78 		return pushObject( collection, items );
     79 	}
     80 	throw new TypeError( 'invalid argument. First argument must be either an Array, Typed Array, or an array-like Object. Value: `'+collection+'`.' );
     81 }
     82 
     83 
     84 // EXPORTS //
     85 
     86 module.exports = push;