time-to-botec

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

main.js (3445B)


      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 isObjectLike = require( '@stdlib/assert/is-object-like' );
     24 var isFunction = require( '@stdlib/assert/is-function' );
     25 var validate = require( './validate.js' );
     26 var returnValues = require( './return_values.js' );
     27 var returnKeys = require( './return_keys.js' );
     28 var returnPairs = require( './return_pairs.js' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Groups an object's own property values according to an indicator function.
     35 *
     36 * @param {(Object|Array|TypedArray)} obj - input object
     37 * @param {Options} [options] - function options
     38 * @param {*} [options.thisArg] - execution context
     39 * @param {string} [options.returns="values"] - if `values`, values are returned; if `keys`, keys are returned; if `*`, both keys and values are returned
     40 * @param {Function} indicator - indicator function indicating which group an element in the input object belongs to
     41 * @throws {TypeError} first argument must be an object, array, or typed array
     42 * @throws {TypeError} options argument must be an object
     43 * @throws {TypeError} last argument must be a function
     44 * @throws {TypeError} must provide valid options
     45 * @returns {Object} group results
     46 *
     47 * @example
     48 * function indicator( v ) {
     49 *     return v[ 0 ];
     50 * }
     51 * var obj = {
     52 *     'a': 'beep',
     53 *     'b': 'boop',
     54 *     'c': 'foo',
     55 *     'd': 'bar'
     56 * };
     57 * var out = groupOwn( obj, indicator );
     58 * // e.g., returns { 'b': [ 'beep', 'boop', 'bar' ], 'f': [ 'foo' ] }
     59 *
     60 * @example
     61 * function indicator( v ) {
     62 *     return v[ 0 ];
     63 * }
     64 * var obj = {
     65 *     'a': 'beep',
     66 *     'b': 'boop',
     67 *     'c': 'foo',
     68 *     'd': 'bar'
     69 * };
     70 * var opts = {
     71 *     'returns': 'keys'
     72 * };
     73 * var out = groupOwn( obj, opts, indicator );
     74 * // e.g., returns { 'b': [ 'a', 'b', 'd' ], 'f': [ 'c' ] }
     75 *
     76 * @example
     77 * function indicator( v ) {
     78 *     return v[ 0 ];
     79 * }
     80 * var obj = {
     81 *     'a': 'beep',
     82 *     'b': 'boop',
     83 *     'c': 'foo',
     84 *     'd': 'bar'
     85 * };
     86 * var opts = {
     87 *     'returns': '*'
     88 * };
     89 * var out = groupOwn( obj, opts, indicator );
     90 * // e.g., returns { 'b': [ [ 'a', 'beep' ], [ 'b', 'boop' ], [ 'd', 'bar' ] ], 'f': [ [ 'c', 'foo' ] ] }
     91 */
     92 function groupOwn( obj, options, indicator ) {
     93 	var opts;
     94 	var err;
     95 	var cb;
     96 	if ( !isObjectLike( obj ) ) {
     97 		throw new TypeError( 'invalid argument. First argument must be an object. Value: `'+obj+'`.' );
     98 	}
     99 	opts = {
    100 		'returns': 'values'
    101 	};
    102 	if ( arguments.length === 2 ) {
    103 		cb = options;
    104 	} else {
    105 		err = validate( opts, options );
    106 		if ( err ) {
    107 			throw err;
    108 		}
    109 		cb = indicator;
    110 	}
    111 	if ( !isFunction( cb ) ) {
    112 		throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+cb+'`.' );
    113 	}
    114 	if ( opts.returns === 'values' ) {
    115 		return returnValues( obj, opts, cb );
    116 	}
    117 	if ( opts.returns === 'keys' ) {
    118 		return returnKeys( obj, opts, cb );
    119 	}
    120 	return returnPairs( obj, opts, cb );
    121 }
    122 
    123 
    124 // EXPORTS //
    125 
    126 module.exports = groupOwn;