time-to-botec

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

pop.js (2285B)


      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 popObject = require( './pop_object.js' );
     27 var popTypedArray = require( './pop_typed_array.js' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Removes and returns the last element of a collection.
     34 *
     35 * @param {(Array|TypedArray|Object)} collection - collection
     36 * @throws {TypeError} must provide either an array, typed array, or an array-like object
     37 * @returns {Array} updated collection and the removed element
     38 *
     39 * @example
     40 * var arr = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
     41 *
     42 * var out = pop( arr );
     43 * // returns [ [ 1.0, 2.0, 3.0, 4.0 ], 5.0 ]
     44 *
     45 * @example
     46 * var arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
     47 * // returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
     48 *
     49 * var out = pop( arr );
     50 * // returns [ <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ], 5.0 ]
     51 */
     52 function pop( collection ) {
     53 	var v;
     54 	if ( isArray( collection ) ) {
     55 		v = collection.pop();
     56 		return [ collection, v ];
     57 	}
     58 	// Check for a typed-array-like object, as verifying actual typed arrays is expensive...
     59 	if ( isTypedArrayLike( collection ) ) {
     60 		return popTypedArray( collection );
     61 	}
     62 	// Check for an array-like object...
     63 	if (
     64 		collection !== null &&
     65 		typeof collection === 'object' &&
     66 		typeof collection.length === 'number' &&
     67 		isInteger( collection.length ) &&
     68 		collection.length >= 0
     69 	) {
     70 		return popObject( collection );
     71 	}
     72 	throw new TypeError( 'invalid argument. Must provide either an Array, Typed Array, or an array-like Object. Value: `'+collection+'`.' );
     73 }
     74 
     75 
     76 // EXPORTS //
     77 
     78 module.exports = pop;