time-to-botec

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

group_by.js (2997B)


      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 factory = require( './factory.js' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Groups values according to an indicator function.
     30 *
     31 * ## Notes
     32 *
     33 * -   This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
     34 *
     35 *
     36 * @param {Collection} collection - input collection
     37 * @param {Options} [options] - function options
     38 * @param {*} [options.thisArg] - execution context
     39 * @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time
     40 * @param {boolean} [options.series=false] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection
     41 * @param {string} [options.returns="values"] - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned
     42 * @param {Function} indicator - indicator function specifying which group an element in the input collection belongs to
     43 * @param {Callback} done - function to invoke upon completion
     44 * @throws {TypeError} first argument must be a collection
     45 * @throws {TypeError} options argument must be an object
     46 * @throws {TypeError} must provide valid options
     47 * @throws {TypeError} second-to-last argument must be a function
     48 * @throws {TypeError} last argument must be a function
     49 * @returns {void}
     50 *
     51 * @example
     52 * var readFile = require( '@stdlib/fs/read-file' );
     53 *
     54 * function done( error, result ) {
     55 *     if ( error ) {
     56 *         throw error;
     57 *     }
     58 *     console.log( result );
     59 * }
     60 *
     61 * function indicator( file, next ) {
     62 *     var opts = {
     63 *         'encoding': 'utf8'
     64 *     };
     65 *     readFile( file, opts, onFile );
     66 *
     67 *     function onFile( error ) {
     68 *         if ( error ) {
     69 *             return next( null, 'nonreadable' );
     70 *         }
     71 *         next( null, 'readable' );
     72 *     }
     73 * }
     74 *
     75 * var files = [
     76 *     './beep.js',
     77 *     './boop.js'
     78 * ];
     79 *
     80 * groupByAsync( files, indicator, done );
     81 */
     82 function groupByAsync( collection, options, indicator, done ) {
     83 	if ( arguments.length < 4 ) {
     84 		return factory( options )( collection, indicator );
     85 	}
     86 	factory( options, indicator )( collection, done );
     87 }
     88 
     89 
     90 // EXPORTS //
     91 
     92 module.exports = groupByAsync;