Skip to content

Commit

Permalink
fixes to simple inventory
Browse files Browse the repository at this point in the history
fixes #19
fixes #18

- Adds the inventory methods inv#isEmpty() and inv#count().
- Fixes minor inconsistency with inv#pickUp() and <<pickup>> macro behavior when used with unique keyword.
  • Loading branch information
ChapelR committed Feb 15, 2019
1 parent 2139a63 commit 3195cd3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
42 changes: 41 additions & 1 deletion docs/simple-inventory.md
Expand Up @@ -470,7 +470,7 @@ This method is useful for enforcing uniqueness; that is, preventing doubles or d

Instance methods, unlike static methods, are called directly on the inventory object, so you replace `<inventory>` with the variable you're using to store / reference the inventory instance you want to work on. See the examples for the methods below if that confuses you.

Of particular note to authors: `<inventory>.has()`, `<inventory>.hasAll()`, and `<inventory>.toArray()`
Of particular note to authors: `<inventory>.has()`, `<inventory>.hasAll()`, `<inventory>.toArray()`, `<inventory>.count()`, and `<inventory>.isEmpty()`.

##### `<inventory>.pickUp()`

Expand Down Expand Up @@ -634,6 +634,46 @@ This method returns an array made up of the items in the calling inventory. Use
<<= _items.join(', ')>> /% shows 'uh oh' %/
```

##### `<inventory>.count()`

**Return Value**: Integer.

**Syntax**: `<inventory>.count([item])`

This method returns the total number of items in the inventory. If you pass it a string corresponding to an item name, it will instead check for how many duplicates of that item are in the inventory.

**Arguments**:

* **item**: (optional) the item to count duplicates of.

**Usage**:
```
/% create an array based on the inventory %/
<<newinventory '$inv' 'this' 'that' 'the other' 'another' 'that'>>
<<= $inv.count()>> /% 5 %/
<<= $inv.count('this')>> /% 1 %/
<<= $inv.count('that')>> /% 2 %/
```

##### `<inventory>.isEmpty()`

**Return Value**: Boolean.

**Syntax**: `<inventory>.isEmpty()`

This method returns whether or not the inventory is empty (has zero items in it). You can also compare `<inventory>.count()` to `0` to achieve similar results.

**Usage**:
```
/% check if the inventory is empty %/
<<if $inventory.isEmpty()>>
You aren't carrying anything.
<<else>>
You have some things on you.
<</if>>
```

##### `<inventory>.sort()`

**Return Value**: This inventory (chainable).
Expand Down
2 changes: 1 addition & 1 deletion scripts/minified/simple-inventory.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 23 additions & 4 deletions scripts/simple-inventory.js
@@ -1,5 +1,5 @@
// simple-inventory.js, by chapel; for sugarcube 2
// version 2.1.0
// version 2.2.0

// namespace
setup.simpleInv = {};
Expand Down Expand Up @@ -140,12 +140,12 @@ setup.simpleInv.inventory.prototype = {
pickUp : function (unique) { // add items to this inventory
var items = [].slice.call(arguments).flatten(), inventory = this;
if (items && items.length) {
if (unique === 'unique' || items[0] === 'unique') { // JS API only; items must be unique in eash inventory instance
if (unique === 'unique' || items[0] === 'unique') { // items must be unique in each inventory instance
items = items.splice(1);
items = (function (items) {
var ret = []; // this code could use some cleanup
items.forEach(function (item) {
if (!inventory.inv.includes(item)) {
if (!inventory.inv.includes(item) && !ret.includes(item)) {
ret.push(item);
}
});
Expand Down Expand Up @@ -203,6 +203,25 @@ setup.simpleInv.inventory.prototype = {
toArray : function () { // not super necessary
return (this.inv);
},

count : function (item) {
// if item, count the number of duplicates
if (item && typeof item === 'string') {
var cnt = 0;
this.toArray().forEach( function (i) {
if (i === item) {
cnt++;
}
});
return cnt;
}
// else return the length
return this.toArray().length;
},

isEmpty : function () {
return this.toArray().length === 0;
},

linkedList : function (loc, action) { // construct the list elements to keep the macro call clean-ish
if (!loc || !(loc instanceof setup.simpleInv.inventory)) {
Expand Down Expand Up @@ -301,7 +320,7 @@ if (setup.simpleInv.options.tryGlobal) {
Macro.add('newinventory', {
handler : function () {

if (this.args.length !== 1) {
if (this.args.length < 1) {
return this.error('incorrect number of arguments');
}
var varName = this.args[0].trim();
Expand Down

0 comments on commit 3195cd3

Please sign in to comment.