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

hackedvoxels-itempile

v0.0.1

Published

split and merge 'piles' of items

Downloads

8

Readme

hackedvoxels-itempile

TODO : FIX LINKS

A data structure for groups of identical objects, up to a maximum number. Useful for games. (This module was previously known as "itemstack".)

Build Status

Can be used standalone but most useful with inventory.

Creating

An item pile can be created simply with an item name and count, for example:

var ItemPile = require('itempile');

var x = new ItemPile('dirt', 10);

represents a quantity of 10 dirt. The item type can be an any comparable object (singleton); these examples use strings. The quantity can be omitted to use a default of "1".

Merging

Piles of the same type can be merged:

var a = new ItemPile('dirt', 10);
var b = new ItemPile('dirt', 20);

a.mergePile(b);

results in a increasing to 30 and b to 0. mergePile returns false if the piles differ in type and cannot be merged, otherwise the number of items that did not fit (excess above the maximum pile size):

var a = new ItemPile('dirt', 1);
var b = new ItemPile('dirt', 80);

a.mergePile(b);

increases the count of a to 64, the default ItemPile.maxStackSize limit, and decreases b to 17. The sum of the two pile counts remains invariant, the quantity has just shifted between the two.

Splitting

Want to take items from a pile? Split the pile, specifying the number of items you want:

var a = new ItemPile('dirt', 64)
var b = a.splitPile(16)

b is a new pile with 16 dirt, a is lowered to the remaining 48 dirt. For convenience you can alternatively pass a decimal fraction (such as 0.5, splits the pile in half), or a negative integer (-1 to take all but one).

Other operations

Merging/splitting are the most important but several other methods are provided, see the unit tests for further examples.

Advanced piles

You can create piles of infinite size:

new ItemPile('diamond', Infinity)

and they behave as you expect, sinking unlimited items when merging and sourcing unlimited items when splitting.

Extra data can be attached to a pile, using the "tags" parameter:

new ItemPile('pick', 1, {damage:0})

License

MIT