npm.io
2.0.0 • Published 1 week ago

array-types-counter

Licence
MIT
Version
2.0.0
Deps
0
Size
8 kB
Vulns
0
Weekly
0

array-types-counter

Counts the item types of an array. Strings that can be parsed as numbers count as number, and arrays count as object.

Installation

npm i array-types-counter
pnpm add array-types-counter

Usage

ESM
import { countArrayTypes } from "array-types-counter";

const counts = countArrayTypes([1, 2, "three", [1, 2, 3]]);
// [{ type: 'number', count: 2 }, { type: 'string', count: 1 }, { type: 'object', count: 1 }]
CommonJS
const { countArrayTypes } = require("array-types-counter");

const counts = countArrayTypes([1, 2, "3", [1, 2, 3]]);
// [{ type: 'number', count: 3 }, { type: 'object', count: 1 }]

API

countArrayTypes(array: unknown[], sampleSize?: number): TypeCount[]

Counts the item types of array and returns them ordered by count, descending. sampleSize limits how many items are inspected from the beginning of the array; it defaults to the array's length and must be a non-negative integer.

  • Strings that can be parsed as numbers count as number.
  • Arrays and objects both count as object.
  • null counts as object (JavaScript typeof).
TypeCount
{
  type: string;
  count: number;
}

License

MIT

Keywords