time-to-botec

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

prepend.js (2879B)


      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 isCollection = require( '@stdlib/assert/is-collection' );
     25 var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' );
     26 var isInteger = require( '@stdlib/assert/is-integer' );
     27 var prependArray = require( './prepend_array.js' );
     28 var prependObject = require( './prepend_object.js' );
     29 var prependTypedArray = require( './prepend_typed_array.js' );
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Adds elements from one collection to the beginning of another collection.
     36 *
     37 * @param {Collection} collection1 - collection
     38 * @param {Collection} collection2 - collection containing elements to add
     39 * @throws {TypeError} first argument must be either an array, typed array, or an array-like object
     40 * @throws {TypeError} second argument must be an array-like object
     41 * @returns {Collection} updated collection
     42 *
     43 * @example
     44 * var arr = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
     45 *
     46 * arr = prepend( arr, [ 6.0, 7.0 ] );
     47 * // returns [ 6.0, 7.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]
     48 *
     49 * @example
     50 * var Float64Array = require( '@stdlib/array/float64' );
     51 *
     52 * var arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
     53 * // returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
     54 *
     55 * arr = prepend( arr, [ 6.0, 7.0 ] );
     56 * // returns <Float64Array>[ 6.0, 7.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]
     57 */
     58 function prepend( collection1, collection2 ) {
     59 	if ( !isCollection( collection2 ) ) {
     60 		throw new TypeError( 'invalid argument. Second argument must be an array-like object. Value: `'+collection2+'`.' );
     61 	}
     62 	if ( isArray( collection1 ) ) {
     63 		return prependArray( collection1, collection2 );
     64 	}
     65 	// Check for a typed-array-like object, as verifying actual typed arrays is expensive...
     66 	if ( isTypedArrayLike( collection1 ) ) {
     67 		return prependTypedArray( collection1, collection2 );
     68 	}
     69 	// Check for an array-like object...
     70 	if (
     71 		collection1 !== null &&
     72 		typeof collection1 === 'object' &&
     73 		typeof collection1.length === 'number' &&
     74 		isInteger( collection1.length ) &&
     75 		collection1.length >= 0
     76 	) {
     77 		return prependObject( collection1, collection2 );
     78 	}
     79 	throw new TypeError( 'invalid argument. First argument must be either an Array, Typed Array, or an array-like Object. Value: `'+collection1+'`.' );
     80 }
     81 
     82 
     83 // EXPORTS //
     84 
     85 module.exports = prepend;