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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mapwise

v1.0.0

Published

![Tests](https://github.com/davidgovea/mapwise/actions/workflows/test.yml/badge.svg) ![npm version](https://img.shields.io/npm/v/mapwise) ![Bundle Size](https://img.shields.io/bundlephobia/minzip/mapwise) ![License](https://img.shields.io/github/license/d

Downloads

26

Readme

Mapwise — keyBy and groupBy for Maps in TypeScript

Tests npm version Bundle Size License

Mapwise is a lightweight, type-safe utility library for TypeScript that provides two helper functions:

  • keyBy: Transform an array into a Map keyed by a specified property or computed value.
    Optionally, provide a value transformer to choose what each map entry stores.
  • groupBy: Group array items into a Map where each key corresponds to an array of items sharing a common property or computed value.
    Optionally, provide a value transformer to transform the grouped items however you’d like.

Mapwise is designed after both lodash and the recent Map.groupBy function.

Why Maps?

Using Maps instead of Records (as in lodash) offers a clear type safety advantage. When you access a key in a Record via bracket notation (e.g. result[nonExistingKey]), TypeScript may not always include undefined in the inferred type — even if the key is absent — potentially masking runtime errors. In contrast, Maps require using the get method (e.g. resultMap.get(nonExistingKey)), which correctly returns a type of T | undefined. This ensures that the possibility of a missing key is always accounted for in consuming code.

Features

  • Type Safety: Leverage TypeScript’s strong typing with overloads that enforce correct usage for property‑ or function‑based keys.
  • Flexible Keying & Values:
    • Provide a property name or callback function to determine the map/group keys (a key extractor).
    • Optionally, provide a property name or callback function (a value transformer) to determine the stored values.
  • Explicit Nullish Handling: Fine‑grained control over whether you skip null/undefined items and keys (via excludeNullish: true).

Installation

bun add mapwise

Not using Bun? Ehem.. I guess that's OK too..

Usage

Importing

import { keyBy, groupBy } from "mapwise";

// Or as separate ES modules:
import { keyBy } from 'mapwise/keyBy';
import { groupBy } from 'mapwise/groupBy';

keyBy Examples

Key by a Property (Storing the Entire Object as the Value)

const items: Person[] = [
  { id: 1, name: "Alice", group: "admin" },
  { id: 2, name: "Bob", group: "user" },
  { id: 3, name: "Diana", group: "user" },
];

const mapById = keyBy(items, "id");
// Result: Map<number, Person>

Key by a Property with a Value Transformer

const mapIdToName = keyBy(items, "id", "name");
// Result: Map<number, string>

Key by a Function (For More Control)

const mapByCustom = keyBy(items, (item, index) => {
  // Use a key extractor callback. Handle nullish items as needed.
  return item ? item.id : index;
});

groupBy Examples

Group by a Property (Storing Entire Objects)

const groupsByGroup = groupBy(items, "group");
// Result: Map<string, Person[]>

Group by a Property with a Value Transformer

const groupsByGroupNames = groupBy(items, "group", "name");
// Result: Map<string, string[]>

Group by a Function with a Value Transformer

const groupsByCustom = groupBy(
  items,
  (item, index) => (item ? item.group : "unknown"),
  (item, index) => (item ? item.name.toUpperCase() : "N/A")
);

Handling Nullish Values

Mapwise includes null and undefined items by default. This means that all data—including nullish values—will appear in your results. To filter out any nullish items and keys, pass the option { excludeNullish: true }:

const cleanedMap = keyBy(items, "id", { excludeNullish: true });
const cleanedGroups = groupBy(items, "group", { excludeNullish: true });

Note: When working with arrays that might contain null or undefined values, using property‑based key extraction without { excludeNullish: true } is disallowed by TypeScript to ensure safety. If you need to handle such cases explicitly, use a function‑based key extractor and manage nullish values within your callback.

Testing

bun test

License

This project is open source. See the LICENSE file for details.