tabulate_by.js (3218B)
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 isFunction = require( '@stdlib/assert/is-function' ); 25 var indexOf = require( './../../index-of' ); 26 var validate = require( './validate.js' ); 27 28 29 // MAIN // 30 31 /** 32 * Generates a frequency table according to a provided function. 33 * 34 * ## Notes 35 * 36 * - The output is an array of arrays. Each sub-array corresponds to a unique value in the input collection and is structured as follows: 37 * 38 * - 0: unique value 39 * - 1: value count 40 * - 2: frequency percentage 41 * 42 * 43 * @param {Collection} collection - input collection 44 * @param {Options} [options] - function options 45 * @param {*} [options.thisArg] - execution context 46 * @param {Function} indicator - function whose return values are used to populate the output frequency table 47 * @throws {TypeError} first argument must be a collection 48 * @throws {TypeError} options argument must be an object 49 * @throws {TypeError} last argument must be a function 50 * @throws {TypeError} must provide valid options 51 * @returns {(Array<Array>|Array)} frequency table 52 * 53 * @example 54 * function indicator( value ) { 55 * return value[ 0 ]; 56 * } 57 * 58 * var arr = [ 'beep', 'boop', 'foo', 'beep' ]; 59 * 60 * var out = tabulateBy( arr, indicator ); 61 * // returns [ [ 'b', 3, 0.75 ], [ 'f', 1, 0.25 ] ] 62 */ 63 function tabulateBy( collection, options, indicator ) { 64 var thisArg; 65 var count; 66 var opts; 67 var tmp; 68 var len; 69 var out; 70 var err; 71 var cb; 72 var v; 73 var i; 74 var j; 75 if ( !isCollection( collection ) ) { 76 throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'`.' ); 77 } 78 opts = {}; 79 if ( arguments.length === 2 ) { 80 cb = options; 81 } else { 82 err = validate( opts, options ); 83 if ( err ) { 84 throw err; 85 } 86 cb = indicator; 87 } 88 if ( !isFunction( cb ) ) { 89 throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+cb+'`.' ); 90 } 91 thisArg = opts.thisArg; 92 count = 0; 93 tmp = []; 94 out = []; 95 96 // For each collection element, determine if we've seen the element before. If not, cache a reference which points to its location in the output array; otherwise, update the running count. 97 len = collection.length; 98 for ( i = 0; i < len; i++ ) { 99 v = cb.call( thisArg, collection[ i ], i ); 100 count += 1; 101 j = indexOf( tmp, v ); 102 if ( j === -1 ) { 103 tmp.push( v ); 104 out.push( [ v, 1, 0 ] ); 105 } else { 106 out[ j ][ 1 ] += 1; 107 } 108 } 109 // Compute percentages... 110 len = out.length; 111 for ( i = 0; i < len; i++ ) { 112 out[ i ][ 2 ] = out[ i ][ 1 ] / count; 113 } 114 return out; 115 } 116 117 118 // EXPORTS // 119 120 module.exports = tabulateBy;