UNPKG

array-types-counter

Version:

Simple helper to obtain the count of different array item's types.

55 lines (46 loc) 1.09 kB
import { isNumber } from 'number-helper-functions'; import { orderBy } from 'lodash-es'; /** * TypeCount class * * @class TypeCount */ var TypeCount = /** * Creates an instance of TypeCount. * @param {string} type * @param {number} count * @memberof TypeCount */ function TypeCount(type, count) { this.type = type; this.count = count; }; /** * Counts the array's item types * * @export * @param {any[]} array * @return {TypeCount[]} The array item types ordered by count, descending */ function countArrayTypes(array) { var countObj = array.reduce(function (agg, item) { var type = typeof item; if (type === 'string' && isNumber(item)) { type = 'number'; } if (agg[type] == null) { agg[type] = 1; } else { agg[type]++; } return agg; }, {}); return orderBy(Object.entries(countObj).map(function (_ref) { var type = _ref[0], count = _ref[1]; return new TypeCount(type, count); }), 'count', 'desc'); } export { countArrayTypes }; //# sourceMappingURL=array-types-counter.esm.js.map