time-to-botec

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

append.js (2801B)


      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 appendArray = require( './append_array.js' );
     28 var appendObject = require( './append_object.js' );
     29 var appendTypedArray = require( './append_typed_array.js' );
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Adds elements from one collection to the end 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 = append( arr, [ 6.0, 7.0 ] );
     47 * // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]
     48 *
     49 * @example
     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 = append( 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 append( collection1, collection2 ) {
     57 	if ( !isCollection( collection2 ) ) {
     58 		throw new TypeError( 'invalid argument. Second argument must be an array-like object. Value: `'+collection2+'`.' );
     59 	}
     60 	if ( isArray( collection1 ) ) {
     61 		return appendArray( collection1, collection2 );
     62 	}
     63 	// Check for a typed-array-like object, as verifying actual typed arrays is expensive...
     64 	if ( isTypedArrayLike( collection1 ) ) {
     65 		return appendTypedArray( collection1, collection2 );
     66 	}
     67 	// Check for an array-like object...
     68 	if (
     69 		collection1 !== null &&
     70 		typeof collection1 === 'object' &&
     71 		typeof collection1.length === 'number' &&
     72 		isInteger( collection1.length ) &&
     73 		collection1.length >= 0
     74 	) {
     75 		return appendObject( collection1, collection2 );
     76 	}
     77 	throw new TypeError( 'invalid argument. First argument must be either an Array, Typed Array, or an array-like Object. Value: `'+collection1+'`.' );
     78 }
     79 
     80 
     81 // EXPORTS //
     82 
     83 module.exports = append;