README.md (4705B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # countBy 22 23 > Group values according to an indicator function and return group counts. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var countBy = require( '@stdlib/utils/count-by' ); 41 ``` 42 43 #### countBy( collection, \[options,] indicator ) 44 45 Groups values according to an `indicator` function, i.e., a function which specifies which group an element in the input `collection` belongs to, and returns group counts. 46 47 ```javascript 48 function indicator( v ) { 49 return v[ 0 ]; 50 } 51 var arr = [ 'beep', 'boop', 'foo', 'bar' ]; 52 53 var out = countBy( arr, indicator ); 54 // returns { 'b': 3, 'f': 1 } 55 ``` 56 57 An `indicator` function is provided two arguments: 58 59 - `value`: collection element 60 - `index`: collection index 61 62 ```javascript 63 function indicator( v, i ) { 64 console.log( '%d: %d', i, v ); 65 return v[ 0 ]; 66 } 67 var arr = [ 'beep', 'boop', 'foo', 'bar' ]; 68 69 var out = countBy( arr, indicator ); 70 // returns { 'b': 3, 'f': 1 } 71 ``` 72 73 The function accepts the following `options`: 74 75 - `thisArg`: execution context. 76 77 To set the `indicator` execution context, provide a `thisArg`. 78 79 ```javascript 80 function indicator( v ) { 81 this.count += 1; 82 return v[ 0 ]; 83 } 84 var context = { 85 'count': 0 86 }; 87 var opts = { 88 'thisArg': context 89 }; 90 var arr = [ 'beep', 'boop', 'foo', 'bar' ]; 91 92 var out = countBy( arr, opts, indicator ); 93 // returns { 'b': 3, 'f': 1 } 94 95 console.log( context.count ); 96 // => 4 97 ``` 98 99 </section> 100 101 <!-- /.usage --> 102 103 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 104 105 <section class="notes"> 106 107 ## Notes 108 109 - A `collection` may be either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (excluding `strings` and `functions`). 110 111 - The value returned by an `indicator` function should be a value which can be serialized as an `object` key. As a counterexample, 112 113 ```javascript 114 function indicator( v ) { 115 return {}; 116 } 117 var arr = [ 'beep', 'boop', 'foo', 'bar' ]; 118 119 var out = countBy( arr, indicator ); 120 // returns { '[object Object]': 4 } 121 ``` 122 123 while each group identifier is unique, all collection elements resolve to the same group because each group identifier serializes to the same `string`. 124 125 </section> 126 127 <!-- /.notes --> 128 129 <!-- Package usage examples. --> 130 131 <section class="examples"> 132 133 ## Examples 134 135 <!-- eslint no-undef: "error" --> 136 137 ```javascript 138 var randu = require( '@stdlib/random/base/randu' ); 139 var floor = require( '@stdlib/math/base/special/floor' ); 140 var countBy = require( '@stdlib/utils/count-by' ); 141 142 var vals; 143 var arr; 144 var out; 145 var i; 146 var j; 147 148 vals = [ 'beep', 'boop', 'foo', 'bar', 'woot', 'woot' ]; 149 150 // Generate a random collection... 151 arr = new Array( 100 ); 152 for ( i = 0; i < arr.length; i++ ) { 153 j = floor( randu()*vals.length ); 154 arr[ i ] = vals[ j ]; 155 } 156 157 function indicator( v ) { 158 // Use the first letter of each element to define groups: 159 return v[ 0 ]; 160 } 161 162 // Compute the group counts: 163 out = countBy( arr, indicator ); 164 console.log( out ); 165 ``` 166 167 </section> 168 169 <!-- /.examples --> 170 171 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 172 173 <section class="references"> 174 175 </section> 176 177 <!-- /.references --> 178 179 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 180 181 <section class="links"> 182 183 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 184 185 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 186 187 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object 188 189 </section> 190 191 <!-- /.links -->