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 🙏

© 2024 – Pkg Stats / Ryan Hefner

hex-grids-json

v1.2.2

Published

hex-grids-json =====

Downloads

3

Readme

hex-grids-json

This is a library to overlay a hexagon grid onto your JSON data store like Redux or possibly Firebase. The basis for this library is the grid format described here. It allows you to access your data normally, with all the custom data you provide for each hex, while providing convenience functions for accessing hexes in various directions. Essentially, this allows you to have a denormalized data model that looks normalized.

I recommend setting up the hexagon grid under a separate key in your data store. This library comes with a Redux reducer that when passed no arguments, returns the default state it expects to be able to get to. So, to initialize your store, you can either use the built in combineReducers from Redux or simply call the reducer with no arguments.

import { hexGridsReducer } from 'hex-grids-json';
import { combineReducers } from 'redux';

// Redux way
const reducers = combineReducers({
    hexes: hexGridsReducer
});

// Manual way
const state = {
    hexes: hexGridsReducer()
};

From there, you'll need some function that returns your application's state. (I'll refer to it as getState(), as that's what it is called in Redux.)

You'll also need some function that accepts actions out of the HexGrids class and reduces them. You can either build some simple logic given the exported reducer, or you can use Redux and it's built-in dispatch().

Let's assume we have a Redux store, with our hex grid stored at the key "hexes". Here's an example that stores the color of each hex, with a default color of white:

import HexGrid from 'hex-grids-json';
interface Hex {
    color: string;
}
const grid = new HexGrid<Hex>(getState, dispatch, {
    color: "white"
});

From here, you can access hexes, along with all the other functions included in the class.

// Because hex 0, 0 does not exist, it will be created with default values
const hex = grid.getHex(0, 0);
assert(hex.color).equals('white');
// This dispatches a Redux action to change this property
hex.color = 'blue';
const hex2 = hex.east; // Returns hex 1, 0
// This can continue on for a while
const hex3 = hex.east.east.east.east.east.east.east.east; // Hex 8, 0

Let's say your writing your own reducer, and want to use the hex library but not emit a million actions. You can use HexGrid.modify():

const mGrid = grid.modify(state);
// No Redux action emitted, and therefore no change to the original hex grid.
mGrid.getHex(0, 0).color = 'blue';
assert(grid.getHex(0, 0).color).equals('white');
// However, the change is retained in the modified grid:
assert(mGrid.getHex(0, 0).color).equals('blue');
// When you want to get the state out and return it for your reducer return, use
// build():
return {
    hexes: mGrid.build()
}
// There's nothing special about the build() call, either: you could put it in
// a spread object to make manual edits after the build call as well.