tabulate_by.js (3091B)
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 * Generates a frequency table 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 * - The output frequency table is an array of arrays. Each sub-array corresponds to a unique value in the input collection and is structured as follows: 36 * 37 * - 0: unique value 38 * - 1: value count 39 * - 2: frequency percentage 40 * 41 * 42 * @param {Collection} collection - input collection 43 * @param {Options} [options] - function options 44 * @param {*} [options.thisArg] - execution context 45 * @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time 46 * @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 47 * @param {Function} indicator - function whose return values are used to populate the output frequency table 48 * @param {Callback} done - function to invoke upon completion 49 * @throws {TypeError} first argument must be a collection 50 * @throws {TypeError} options argument must be an object 51 * @throws {TypeError} must provide valid options 52 * @throws {TypeError} second-to-last argument must be a function 53 * @throws {TypeError} last argument must be a function 54 * @returns {void} 55 * 56 * @example 57 * var readFile = require( '@stdlib/fs/read-file' ); 58 * 59 * function done( error, result ) { 60 * if ( error ) { 61 * throw error; 62 * } 63 * console.log( result ); 64 * } 65 * 66 * function indicator( file, next ) { 67 * var opts = { 68 * 'encoding': 'utf8' 69 * }; 70 * readFile( file, opts, onFile ); 71 * 72 * function onFile( error ) { 73 * if ( error ) { 74 * return next( null, 'nonreadable' ); 75 * } 76 * next( null, 'readable' ); 77 * } 78 * } 79 * 80 * var files = [ 81 * './beep.js', 82 * './boop.js' 83 * ]; 84 * 85 * tabulateByAsync( files, indicator, done ); 86 */ 87 function tabulateByAsync( collection, options, indicator, done ) { 88 if ( arguments.length < 4 ) { 89 return factory( options )( collection, indicator ); 90 } 91 factory( options, indicator )( collection, done ); 92 } 93 94 95 // EXPORTS // 96 97 module.exports = tabulateByAsync;