count_by.js (2860B)
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 and returns group counts. 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 {Function} indicator - indicator function specifying which group an element in the input collection belongs to 42 * @param {Callback} done - function to invoke upon completion 43 * @throws {TypeError} first argument must be a collection 44 * @throws {TypeError} options argument must be an object 45 * @throws {TypeError} must provide valid options 46 * @throws {TypeError} second-to-last argument must be a function 47 * @throws {TypeError} last argument must be a function 48 * @returns {void} 49 * 50 * @example 51 * var readFile = require( '@stdlib/fs/read-file' ); 52 * 53 * function done( error, result ) { 54 * if ( error ) { 55 * throw error; 56 * } 57 * console.log( result ); 58 * } 59 * 60 * function indicator( file, next ) { 61 * var opts = { 62 * 'encoding': 'utf8' 63 * }; 64 * readFile( file, opts, onFile ); 65 * 66 * function onFile( error ) { 67 * if ( error ) { 68 * return next( null, 'nonreadable' ); 69 * } 70 * next( null, 'readable' ); 71 * } 72 * } 73 * 74 * var files = [ 75 * './beep.js', 76 * './boop.js' 77 * ]; 78 * 79 * countByAsync( files, indicator, done ); 80 */ 81 function countByAsync( collection, options, indicator, done ) { 82 if ( arguments.length < 4 ) { 83 return factory( options )( collection, indicator ); 84 } 85 factory( options, indicator )( collection, done ); 86 } 87 88 89 // EXPORTS // 90 91 module.exports = countByAsync;