diff --git a/docs/simple-inventory.md b/docs/simple-inventory.md index cb23882..6cc81a5 100644 --- a/docs/simple-inventory.md +++ b/docs/simple-inventory.md @@ -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 `` 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: `.has()`, `.hasAll()`, and `.toArray()` +Of particular note to authors: `.has()`, `.hasAll()`, `.toArray()`, `.count()`, and `.isEmpty()`. ##### `.pickUp()` @@ -634,6 +634,46 @@ This method returns an array made up of the items in the calling inventory. Use <<= _items.join(', ')>> /% shows 'uh oh' %/ ``` +##### `.count()` + +**Return Value**: Integer. + +**Syntax**: `.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 %/ +<> + +<<= $inv.count()>> /% 5 %/ +<<= $inv.count('this')>> /% 1 %/ +<<= $inv.count('that')>> /% 2 %/ +``` + +##### `.isEmpty()` + +**Return Value**: Boolean. + +**Syntax**: `.isEmpty()` + +This method returns whether or not the inventory is empty (has zero items in it). You can also compare `.count()` to `0` to achieve similar results. + +**Usage**: +``` +/% check if the inventory is empty %/ +<> + You aren't carrying anything. +<> + You have some things on you. +<> +``` + ##### `.sort()` **Return Value**: This inventory (chainable). diff --git a/scripts/minified/simple-inventory.min.js b/scripts/minified/simple-inventory.min.js index 383aa14..2e705b0 100644 --- a/scripts/minified/simple-inventory.min.js +++ b/scripts/minified/simple-inventory.min.js @@ -1,3 +1,3 @@ // simple-inventory.min.js, for SugarCube 2, by Chapel -;setup.simpleInv={},setup.simpleInv.options={tryGlobal:!0,defaultStrings:{empty:"The inventory is empty...",listDrop:"Discard",separator:"\n"}},setup.simpleInv.attachEvent=function(i,t,n,r){$(document).trigger({type:"initialized"===r?":inventory-init":":inventory-update",instance:i,receiving:t,moved:n,context:r})},setup.simpleInv.inventory=function(i){"use strict";if(i=i?(i=[].slice.call(arguments)).flatten():[],!(this instanceof setup.simpleInv.inventory))return new setup.simpleInv.inventory(i);i=(this.inv=i).length?i:null,setup.simpleInv.attachEvent(this,null,i,"initialized")},setup.simpleInv.inventory.is=function(i){return i instanceof setup.simpleInv.inventory},setup.simpleInv.inventory.log=function(i){return setup.simpleInv.inventory.is(i)?"Inventory.log() -> "+i.toArray().join(" - "):"Inventory.log() -> object is not an inventory..."},setup.simpleInv.inventory.removeDuplicates=function(i){if(setup.simpleInv.inventory.is(i)){var t,n=i.toArray();return t=[],n.forEach(function(i){t.includes(i)||t.push(i)}),t}},setup.simpleInv.inventory.getUID=function(i,t){var n=Math.random().toString(36).substring(7);return arguments.length<2&&(i=Math.random().toString(36).substring(7),t=random(99)),"simple-inv-"+t+"-"+Date.now()+"-"+i.replace(/[^A-Za-z0-9]/g,"")+"-"+n},setup.simpleInv.inventory.prototype={transfer:function(i){if(arguments.length<2)return this;if(!(i instanceof setup.simpleInv.inventory))return this;for(var t=[].slice.call(arguments),n=[],r=0,e=(t=t.slice(1).flatten()).length;r"),v.append(r)}):v.wiki(setup.simpleInv.options.defaultStrings.empty),v},constructor:setup.simpleInv.inventory,toJSON:function(){return JSON.reviveWrapper("new setup.simpleInv.inventory("+JSON.stringify(this.inv)+")")},clone:function(){return new setup.simpleInv.inventory(this.inv)}},setup.simpleInv.options.tryGlobal&&(window.Inventory=window.Inventory||setup.simpleInv.inventory),Macro.add("newinventory",{handler:function(){if(1!==this.args.length)return this.error("incorrect number of arguments");var i=this.args[0].trim();if("$"!==i[0]&&"_"!==i[0])return this.error('variable name "'+this.args[0]+'" is missing its sigil ($ or _)');Wikifier.setValue(i,new setup.simpleInv.inventory(this.args.slice(1).flatten()))}}),Macro.add("pickup",{handler:function(){if(this.args.length<2)return this.error("incorrect number of arguments");var i=this.args[0].trim();if("$"!==i[0]&&"_"!==i[0])return this.error('variable name "'+this.args[0]+'" is missing its sigil ($ or _)');var t=Wikifier.getValue(i);if(!setup.simpleInv.inventory.is(t))return this.error("variable "+i+" is not an inventory!");t.pickUp(this.args.slice(1).flatten())}}),Macro.add("drop",{handler:function(){if(this.args.length<2)return this.error("incorrect number of arguments");var i=this.args[0].trim();if("$"!==i[0]&&"_"!==i[0])return this.error('variable name "'+this.args[0]+'" is missing its sigil ($ or _)');var t=Wikifier.getValue(i);if(!setup.simpleInv.inventory.is(t))return this.error("variable "+i+" is not an inventory!");t.drop(this.args.slice(1).flatten())}}),Macro.add("transfer",{handler:function(){if(this.args.length<3)return this.error("incorrect number of arguments");var i=this.args[0].trim();if("$"!==i[0]&&"_"!==i[0])return this.error('variable name "'+this.args[0]+'" is missing its sigil ($ or _)');var t=Wikifier.getValue(i);if(!setup.simpleInv.inventory.is(t))return this.error("variable "+i+" is not an inventory!");var n=this.args[1].trim();if("$"!==n[0]&&"_"!==n[0])return this.error('variable name "'+this.args[1]+'" is missing its sigil ($ or _)');var r=Wikifier.getValue(n);if(!setup.simpleInv.inventory.is(r))return this.error("variable "+n+" is not an inventory!");t.transfer(r,this.args.slice(2).flatten())}}),Macro.add("dropall",{handler:function(){if(1!==this.args.length)return this.error("incorrect number of arguments");var i=this.args[0].trim();if("$"!==i[0]&&"_"!==i[0])return this.error('variable name "'+this.args[0]+'" is missing its sigil ($ or _)');var t=Wikifier.getValue(i);if(!setup.simpleInv.inventory.is(t))return this.error("variable "+i+" is not an inventory!");t.empty()}}),Macro.add("clear","dropall",!1),Macro.add("sort",{handler:function(){if(1!==this.args.length)return this.error("incorrect number of arguments");var i=this.args[0].trim();if("$"!==i[0]&&"_"!==i[0])return this.error('variable name "'+this.args[0]+'" is missing its sigil ($ or _)');var t=Wikifier.getValue(i);if(!setup.simpleInv.inventory.is(t))return this.error("variable "+i+" is not an inventory!");t.sort()}}),Macro.add("inventory",{handler:function(){if(this.args.length<1||2 "+i.toArray().join(" - "):"Inventory.log() -> object is not an inventory..."},setup.simpleInv.inventory.removeDuplicates=function(i){if(setup.simpleInv.inventory.is(i)){var t,n=i.toArray();return t=[],n.forEach(function(i){t.includes(i)||t.push(i)}),t}},setup.simpleInv.inventory.getUID=function(i,t){var n=Math.random().toString(36).substring(7);return arguments.length<2&&(i=Math.random().toString(36).substring(7),t=random(99)),"simple-inv-"+t+"-"+Date.now()+"-"+i.replace(/[^A-Za-z0-9]/g,"")+"-"+n},setup.simpleInv.inventory.prototype={transfer:function(i){if(arguments.length<2)return this;if(!(i instanceof setup.simpleInv.inventory))return this;for(var t=[].slice.call(arguments),n=[],r=0,e=(t=t.slice(1).flatten()).length;r"),v.append(r)}):v.wiki(setup.simpleInv.options.defaultStrings.empty),v},constructor:setup.simpleInv.inventory,toJSON:function(){return JSON.reviveWrapper("new setup.simpleInv.inventory("+JSON.stringify(this.inv)+")")},clone:function(){return new setup.simpleInv.inventory(this.inv)}},setup.simpleInv.options.tryGlobal&&(window.Inventory=window.Inventory||setup.simpleInv.inventory),Macro.add("newinventory",{handler:function(){if(this.args.length<1)return this.error("incorrect number of arguments");var i=this.args[0].trim();if("$"!==i[0]&&"_"!==i[0])return this.error('variable name "'+this.args[0]+'" is missing its sigil ($ or _)');Wikifier.setValue(i,new setup.simpleInv.inventory(this.args.slice(1).flatten()))}}),Macro.add("pickup",{handler:function(){if(this.args.length<2)return this.error("incorrect number of arguments");var i=this.args[0].trim();if("$"!==i[0]&&"_"!==i[0])return this.error('variable name "'+this.args[0]+'" is missing its sigil ($ or _)');var t=Wikifier.getValue(i);if(!setup.simpleInv.inventory.is(t))return this.error("variable "+i+" is not an inventory!");t.pickUp(this.args.slice(1).flatten())}}),Macro.add("drop",{handler:function(){if(this.args.length<2)return this.error("incorrect number of arguments");var i=this.args[0].trim();if("$"!==i[0]&&"_"!==i[0])return this.error('variable name "'+this.args[0]+'" is missing its sigil ($ or _)');var t=Wikifier.getValue(i);if(!setup.simpleInv.inventory.is(t))return this.error("variable "+i+" is not an inventory!");t.drop(this.args.slice(1).flatten())}}),Macro.add("transfer",{handler:function(){if(this.args.length<3)return this.error("incorrect number of arguments");var i=this.args[0].trim();if("$"!==i[0]&&"_"!==i[0])return this.error('variable name "'+this.args[0]+'" is missing its sigil ($ or _)');var t=Wikifier.getValue(i);if(!setup.simpleInv.inventory.is(t))return this.error("variable "+i+" is not an inventory!");var n=this.args[1].trim();if("$"!==n[0]&&"_"!==n[0])return this.error('variable name "'+this.args[1]+'" is missing its sigil ($ or _)');var r=Wikifier.getValue(n);if(!setup.simpleInv.inventory.is(r))return this.error("variable "+n+" is not an inventory!");t.transfer(r,this.args.slice(2).flatten())}}),Macro.add("dropall",{handler:function(){if(1!==this.args.length)return this.error("incorrect number of arguments");var i=this.args[0].trim();if("$"!==i[0]&&"_"!==i[0])return this.error('variable name "'+this.args[0]+'" is missing its sigil ($ or _)');var t=Wikifier.getValue(i);if(!setup.simpleInv.inventory.is(t))return this.error("variable "+i+" is not an inventory!");t.empty()}}),Macro.add("clear","dropall",!1),Macro.add("sort",{handler:function(){if(1!==this.args.length)return this.error("incorrect number of arguments");var i=this.args[0].trim();if("$"!==i[0]&&"_"!==i[0])return this.error('variable name "'+this.args[0]+'" is missing its sigil ($ or _)');var t=Wikifier.getValue(i);if(!setup.simpleInv.inventory.is(t))return this.error("variable "+i+" is not an inventory!");t.sort()}}),Macro.add("inventory",{handler:function(){if(this.args.length<1||2