npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

custom-categorize

v2.0.0

Published

Tiny, flexible library to group/categorize arrays by a key, dot-path, or function — with optional paginated-data support.

Readme

custom-categorize

npm version License: MIT

A tiny, zero-dependency library to group arrays of objects by a key, a dot-path, or a function. Written in TypeScript.

import { categorize } from "custom-categorize";

categorize(users, "language");
categorize(users, "userStatus.status");
categorize(users, (u) => u.tags);             // multi-bucket (array return)
categorize(users, "country", { only: ["USA", "UK"] });

Install

npm install custom-categorize
# or
yarn add custom-categorize

Quick start

import { categorize } from "custom-categorize";

const users = [
  { name: "Ada",   language: "en", userStatus: { status: "active"   } },
  { name: "Bisi",  language: "pt", userStatus: { status: "active"   } },
  { name: "Chen",  language: "en", userStatus: { status: "inactive" } },
];

categorize(users, "language");
// { en: [Ada, Chen], pt: [Bisi] }

categorize(users, "userStatus.status");
// { active: [Ada, Bisi], inactive: [Chen] }

API

categorize(items, key, options?)

Group items by key.

| Param | Type | Description | | --------- | ------------------------------------------ | ----------- | | items | T[] | Items to group. | | key | string \| (item: T) => unknown | Property name, dot-path ("a.b.c"), or function. If the function returns an array the item is placed in every returned bucket. | | options | CategorizeOptions | See below. |

Returns: Record<string, T[]> — buckets keyed by the resolved value.

Options

| Option | Type | Default | What it does | | ------------------- | ----------------------------- | -------------- | ------------ | | only | (string \| number)[] | — | Only keep these bucket keys. Empty buckets in this list are still returned. | | keyAs | (raw: string) => string | — | Transform bucket keys (e.g. k => k.toLowerCase()). | | includeUnmatched | boolean | false | Put items whose resolved value is null/undefined/filtered-out into an extra bucket. | | unmatchedKey | string | "_unmatched" | Name of that extra bucket. |

Examples

// Whitelist with stable shape (missing keys still come back as empty arrays)
categorize(users, "language", { only: ["en", "fr"] });
// { en: [...], fr: [] }

// Normalize keys
categorize(users, "country", { keyAs: (k) => k.toLowerCase() });

// Multi-bucket via function
categorize(posts, (p) => p.tags);   // each post lands in every tag bucket

// Capture leftovers
categorize(users, "language", { only: ["en"], includeUnmatched: true });
// { en: [...], _unmatched: [...everyone else...] }

categorizePages(pages, key, options?)

Convenience helper for paginated payloads of the shape:

[{ data: T[], total?, count?, page?, pageCount? }, ...]

Returns one bucket per category with the matching items concatenated and pagination counters aggregated:

{
  [bucket: string]: {
    data: T[];
    total: number;
    count: number;
    page: number;
    pageCount: number;
  }
}
import { categorizePages } from "custom-categorize";

const pages = [
  {
    data: [
      { country: "USA", language: "en" },
      { country: "UK",  language: "en" },
      { country: "Brazil", language: "pt" },
    ],
    total: 3, count: 3, page: 1, pageCount: 1,
  },
];

categorizePages(pages, "language", { only: ["en"] });
// {
//   en: {
//     data: [{country:"USA",...}, {country:"UK",...}],
//     total: 3, count: 3, page: 1, pageCount: 1
//   }
// }

TypeScript

Everything is typed. Public types:

import type {
  CategorizeOptions,
  Grouped,
  GroupedPage,
  GroupedPages,
  KeyResolver,
  Page,
} from "custom-categorize";

Migration from 1.x

1.x exposed a single categorize(users, selectedFilters, targetName, filterFn?) that required a paginated wrapper shape. 2.x is a clean redesign.

| 1.x | 2.x | | ------------------------------------------------------------ | --- | | categorize(pages, ["en"], "language") | categorizePages(pages, "language", { only: ["en"] }) | | categorize(pages, ["active"], "status", customFn) | categorizePages(pages, item => customFn(item, ...), ...) or categorizePages(pages, "userStatus.status", { only: ["active"] }) | | Plain array of items | categorize(items, "language", { only: ["en"] }) |


License

MIT