time-to-botec

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

bifurcate.js (3302B)


      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 isCollection = require( '@stdlib/assert/is-collection' );
     24 var validate = require( './validate.js' );
     25 var returnValues = require( './return_values.js' );
     26 var returnIndices = require( './return_indices.js' );
     27 var returnPairs = require( './return_pairs.js' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Splits values into two groups.
     34 *
     35 * @param {Collection} collection - input collection
     36 * @param {Options} [options] - function options
     37 * @param {string} [options.returns="values"] - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned
     38 * @param {Collection} filter - collection indicating which group an element in the input collection belongs to
     39 * @throws {TypeError} first argument must be a collection
     40 * @throws {TypeError} options argument must be an object
     41 * @throws {TypeError} must provide valid options
     42 * @throws {TypeError} last argument must be a collection
     43 * @throws {RangeError} first and last arguments must be the same length
     44 * @returns {(Array|ArrayArray)} results
     45 *
     46 * @example
     47 * var arr = [ 'beep', 'boop', 'foo', 'bar' ];
     48 * var filter = [ true, true, false, true ];
     49 *
     50 * var out = bifurcate( arr, filter );
     51 * // returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ]
     52 *
     53 * @example
     54 * var arr = [ 'beep', 'boop', 'foo', 'bar' ];
     55 * var filter = [ true, true, false, true ];
     56 *
     57 * var opts = {
     58 *     'returns': 'indices'
     59 * };
     60 *
     61 * var out = bifurcate( arr, opts, filter );
     62 * // returns [ [ 0, 1, 3 ], [ 2 ] ]
     63 *
     64 * @example
     65 * var arr = [ 'beep', 'boop', 'foo', 'bar' ];
     66 * var filter = [ true, true, false, true ];
     67 *
     68 * var opts = {
     69 *     'returns': '*'
     70 * };
     71 *
     72 * var out = bifurcate( arr, opts, filter );
     73 * // returns [ [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], [ [ 2, 'foo' ] ] ]
     74 */
     75 function bifurcate( collection, options, filter ) {
     76 	var opts;
     77 	var err;
     78 	var f;
     79 	if ( !isCollection( collection ) ) {
     80 		throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'`.' );
     81 	}
     82 	opts = {
     83 		'returns': 'values'
     84 	};
     85 	if ( arguments.length === 2 ) {
     86 		f = options;
     87 	} else {
     88 		err = validate( opts, options );
     89 		if ( err ) {
     90 			throw err;
     91 		}
     92 		f = filter;
     93 	}
     94 	if ( !isCollection( f ) ) {
     95 		throw new TypeError( 'invalid argument. Last argument must be a collection. Value: `'+f+'`.' );
     96 	}
     97 	if ( collection.length !== f.length ) {
     98 		throw new RangeError( 'invalid arguments. First and last arguments must be the same length.' );
     99 	}
    100 	if ( opts.returns === 'values' ) {
    101 		return returnValues( collection, f );
    102 	}
    103 	if ( opts.returns === 'indices' ) {
    104 		return returnIndices( collection, f );
    105 	}
    106 	return returnPairs( collection, f );
    107 }
    108 
    109 
    110 // EXPORTS //
    111 
    112 module.exports = bifurcate;