# itempile

> split and merge 'piles' of items

Latest version **2.0.0** (published 2016-02-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install itempile
pnpm add itempile
yarn add itempile
bun add itempile
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2016-02-15 |
| First published | 2013-12-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Maintainers | deathcap |
| Keywords | game, items, piles, group, itemstack, inventory |

## Links

- npm: https://www.npmjs.com/package/itempile
- Repository: https://github.com/deathcap/itempile
- Homepage: https://github.com/deathcap/itempile#readme
- Issues: https://github.com/deathcap/itempile/issues
- npm.io page: https://npm.io/package/itempile

## Dependencies (2)

- [clone](https://npm.io/package/clone.md) ^1.0.2
- [deep-equal](https://npm.io/package/deep-equal.md) ^1.0.1

## Recent versions

- 2.0.0 (latest) — 2016-02-15
- 1.0.2 — 2015-02-23
- 1.0.1 — 2015-02-23
- 1.0.0 — 2015-02-23
- 0.3.0 — 2015-02-22
- 0.2.3 — 2014-06-15
- 0.2.2 — 2014-02-28
- 0.2.1 — 2014-02-25
- 0.2.0 — 2014-02-18
- 0.1.1 — 2013-12-31
- 0.1.0 — 2013-12-31
- 0.0.4 — 2013-12-30
- 0.0.3 — 2013-12-22
- 0.0.2 — 2013-12-19

## README

# itempile

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](https://travis-ci.org/deathcap/itempile.png)](https://travis-ci.org/deathcap/itempile)

Can be used standalone but most useful with [inventory](https://github.com/deathcap/inventory).

Requires a ES6-compatible environment (at least partially), tested on Node v4.2.4

## 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

---
_Source: https://npm.io/package/itempile · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
