mirror of
https://github.com/swc-project/swc.git
synced 2024-12-29 16:42:28 +03:00
33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
// Loaded from https://deno.land/x/ramda@v0.27.2/source/countBy.js
|
|
|
|
|
|
import reduceBy from './reduceBy.js';
|
|
|
|
|
|
/**
|
|
* Counts the elements of a list according to how many match each value of a
|
|
* key generated by the supplied function. Returns an object mapping the keys
|
|
* produced by `fn` to the number of occurrences in the list. Note that all
|
|
* keys are coerced to strings because of how JavaScript objects work.
|
|
*
|
|
* Acts as a transducer if a transformer is given in list position.
|
|
*
|
|
* @func
|
|
* @memberOf R
|
|
* @since v0.1.0
|
|
* @category Relation
|
|
* @sig (a -> String) -> [a] -> {*}
|
|
* @param {Function} fn The function used to map values to keys.
|
|
* @param {Array} list The list to count elements from.
|
|
* @return {Object} An object mapping keys to number of occurrences in the list.
|
|
* @example
|
|
*
|
|
* const numbers = [1.0, 1.1, 1.2, 2.0, 3.0, 2.2];
|
|
* R.countBy(Math.floor)(numbers); //=> {'1': 3, '2': 2, '3': 1}
|
|
*
|
|
* const letters = ['a', 'b', 'A', 'a', 'B', 'c'];
|
|
* R.countBy(R.toLower)(letters); //=> {'a': 3, 'b': 2, 'c': 1}
|
|
*/
|
|
var countBy = reduceBy(function(acc, elem) { return acc + 1; }, 0);
|
|
export default countBy;
|