group.js (3316B)
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 validate = require( './validate.js' ); 25 var returnValues = require( './return_values.js' ); 26 var returnIndices = require( './return_indices.js' ); 27 var returnPairs = require( './return_pairs.js' ); 28 29 30 // MAIN // 31 32 /** 33 * Groups values as arrays associated with distinct keys. 34 * 35 * @param {Collection} collection - collection to group 36 * @param {Options} [options] - function options 37 * @param {string} [options.returns="values"] - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned 38 * @param {Collection} groups - collection defining which group an element in the input collection belongs to 39 * @throws {TypeError} first argument must be a collection 40 * @throws {TypeError} options argument must be an object 41 * @throws {TypeError} last argument must be a collection 42 * @throws {TypeError} must provide valid options 43 * @throws {RangeError} first and last arguments must be the same length 44 * @returns {Object} group results 45 * 46 * @example 47 * var arr = [ 'beep', 'boop', 'foo', 'bar' ]; 48 * var groups = [ 'b', 'b', 'f', 'b' ]; 49 * 50 * var out = group( arr, groups ); 51 * // returns { 'b': [ 'beep', 'boop', 'bar' ], 'f': [ 'foo' ] } 52 * 53 * @example 54 * var arr = [ 'beep', 'boop', 'foo', 'bar' ]; 55 * var groups = [ 'b', 'b', 'f', 'b' ]; 56 * 57 * var opts = { 58 * 'returns': 'indices' 59 * }; 60 * 61 * var out = group( arr, opts, groups ); 62 * // returns { 'b': [ 0, 1, 3 ], 'f': [ 2 ] } 63 * 64 * @example 65 * var arr = [ 'beep', 'boop', 'foo', 'bar' ]; 66 * var groups = [ 'b', 'b', 'f', 'b' ]; 67 * 68 * var opts = { 69 * 'returns': '*' 70 * }; 71 * 72 * var out = group( arr, opts, groups ); 73 * // returns { 'b': [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], 'f': [ [ 2, 'foo' ] ] } 74 */ 75 function group( collection, options, groups ) { 76 var opts; 77 var err; 78 var g; 79 if ( !isCollection( collection ) ) { 80 throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'`.' ); 81 } 82 opts = { 83 'returns': 'values' 84 }; 85 if ( arguments.length === 2 ) { 86 g = options; 87 } else { 88 err = validate( opts, options ); 89 if ( err ) { 90 throw err; 91 } 92 g = groups; 93 } 94 if ( !isCollection( g ) ) { 95 throw new TypeError( 'invalid argument. Last argument must be a collection. Value: `'+g+'`.' ); 96 } 97 if ( collection.length !== g.length ) { 98 throw new RangeError( 'invalid arguments. First and last arguments must be the same length.' ); 99 } 100 if ( opts.returns === 'values' ) { 101 return returnValues( collection, g ); 102 } 103 if ( opts.returns === 'indices' ) { 104 return returnIndices( collection, g ); 105 } 106 return returnPairs( collection, g ); 107 } 108 109 110 // EXPORTS // 111 112 module.exports = group;