UNPKG

array-types-counter

Version:

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

59 lines (48 loc) 1.23 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var numberHelperFunctions = require('number-helper-functions'); var lodash = require('lodash'); /** * 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' && numberHelperFunctions.isNumber(item)) { type = 'number'; } if (agg[type] == null) { agg[type] = 1; } else { agg[type]++; } return agg; }, {}); return lodash.orderBy(Object.entries(countObj).map(function (_ref) { var type = _ref[0], count = _ref[1]; return new TypeCount(type, count); }), 'count', 'desc'); } exports.countArrayTypes = countArrayTypes; //# sourceMappingURL=array-types-counter.cjs.development.js.map