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

@haydn/grid-fns

v0.0.5

Published

<div align="center"> <h1> <img src="https://unpkg.com/@haydn/grid-fns/logo.png" alt="grid-fns" width="160" /> </h1> <p>A JavaScript utility library for working with grids.</p> <p> <img alt="npm bundle size" src="https://img.shields.io/bund

Readme

Features

  • Lightweight.
  • Pure functions.
  • TypeScript declarations included.
  • ESM package.

Installation

bun add @haydn/grid-fns
deno add npm:@haydn/grid-fns
npm install @haydn/grid-fns
pnpm add @haydn/grid-fns
yarn add @haydn/grid-fns

Usage

import { createGrid } from '@haydn/grid-fns';

const grid = createGrid(3, 3);
//=> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

:toolbox: Functions

:gear: createGrid

Creates a grid with the given dimensions.

| Function | Type | | ---------- | ---------- | | createGrid | (cols: number, rows: number, mapFn?: ((point: Vec2) => number) or undefined) => Grid |

Parameters:

  • cols: Number of columns.
  • rows: Number of rows.
  • mapFn: Optional function to set the initial value of each cell. If not provided, all cells are initialized to 0.

Returns:

A grid with the given dimensions.

Examples:

createGrid([3, 3], ([row, col]) => (row + 1) * 10 + col);
//=> [[10, 11, 12], [20, 21, 22], [30, 31, 32]]

:gear: getValue

Retrieves the value of a cell in the given grid.

| Function | Type | | ---------- | ---------- | | getValue | (grid: Grid, cell: Vec2) => number |

Parameters:

  • grid: The grid.
  • cell: The cell.

Returns:

The value of the cell or 0 if the cell has no specified value.

Examples:

getValue([[0, 0, 0], [0, 2.5, 0], [0, 0, 0]], [1, 1]);
//=> 2.5

:gear: measureDistance

Calculates the Manhattan distance between two cells in the given grid.

| Function | Type | | ---------- | ---------- | | measureDistance | (grid: Grid, c1: Vec2, c2: Vec2) => number |

Parameters:

  • grid: The grid.
  • c1: The first cell.
  • c2: The second cell.

Returns:

The Manhattan distance between the two cells.

Examples:

distance(createGrid([3, 3]), [0, 0], [2, 2]);
//=> 4

:gear: mooreNeighbors

Finds the Moore neighbors of a cell (the eight adjacent cells).

| Function | Type | | ---------- | ---------- | | mooreNeighbors | (grid: Grid, cell: Vec2) => Vec2[] |

Parameters:

  • grid: The grid.
  • cell: The cell for which to find neighbors.

Returns:

The Moore neighbors of the cell.

Examples:

mooreNeighbors(createGrid(3, 3), [1, 1]);
//=> [[0, 0], [1, 0], [2, 0], [0, 1], [2, 1], [0, 2], [1, 2], [2, 2]]

:gear: normalizeCell

Normalizes a cell by rounding down fractional coordinates and clamping it to the grid's bounds.

| Function | Type | | ---------- | ---------- | | normalizeCell | (grid: Grid, cell: Vec2) => Vec2 |

Parameters:

  • grid: The grid to normalize.
  • cell: The cell to normalize.

Returns:

The normalized cell.

Examples:

normalizeCell(createGrid(3, 3), [1.5, 2.5]);
//=> [1, 2]
normalizeCell(createGrid(3, 3), [100, 100]);
//=> [2, 2]

:gear: normalizeGrid

Normalizes a grid by ensuring it has at least 1 cell and every row is the same length. Missing cells are filled with 0.

| Function | Type | | ---------- | ---------- | | normalizeGrid | (grid: Grid) => Grid |

Parameters:

  • grid: The grid to normalize.

Returns:

The normalized grid.

Examples:

normalizeGrid([
  [1, 2, 3],
  [4, 5],
  [6, 7, 8, 9],
]);
//=> [
//   [1, 2, 3, 0],
//   [4, 5, 0, 0],
//   [6, 7, 8, 9],
// ]

:gear: orthogonalNeighbors

Finds the orthogonal neighbors of a cell (the four immediately adjacent cells) in a grid.

| Function | Type | | ---------- | ---------- | | orthogonalNeighbors | (grid: Grid, cell: Vec2) => Vec2[] |

Parameters:

  • grid: A grid.
  • cell: The cell for which to find neighbors.

Returns:

The orthogonal neighbors of the cell.

Examples:

orthogonalNeighbors(createGrid(3, 3), [1, 1]);
//=> [[0, 1], [1, 0], [1, 2], [2, 1]]

:gear: setValue

Sets the value of a cell in a grid.

| Function | Type | | ---------- | ---------- | | setValue | (grid: Grid, cell: Vec2, value: number) => Grid |

Parameters:

  • grid: The grid.
  • cell: The cell.
  • value: The value to set.

Returns:

The updated grid.

Examples:

setValue(createGrid(3, 3), [1, 1], 10);

:gear: sizeGrid

Calculates the size of a grid. Even if the grid is not normalized, the maximum size is returned.

| Function | Type | | ---------- | ---------- | | sizeGrid | (grid: Grid) => Vec2 |

Parameters:

  • grid: The grid of which to calculate the size.

Returns:

The number of columns and rows in the grid.

Examples:

sizeGrid([
  [1, 2, 3],
  [4, 5],
  [6, 7, 8, 9],
]);
//=> [4, 3]

:cocktail: Types

:gear: Cell

A cell in a grid where the first value is the column and the second value is the row.

| Type | Type | | ---------- | ---------- | | Cell | Vec2 |

Examples:

const [col, row]: Cell = [1, 2];

:gear: Grid

A grid of numbers organized first by row and then by column.

| Type | Type | | ---------- | ---------- | | Grid | Array<Array<number>> |

Examples:

const myGrid: Grid = [
  [0, 1, 0],
  [0, 0, 6],
  [1, 2, 0],
];