@jeremyckahn/farmhand
Version:
A farming game
26 lines (21 loc) • 604 B
text/typescript
export const decrementItemFromInventory = (
state: farmhand.state,
itemId: string,
howMany: number = 1
): farmhand.state => {
const inventory = [...state.inventory]
const itemInventoryIndex = inventory.findIndex(({ id }) => id === itemId)
if (itemInventoryIndex === -1) {
return state
}
const { quantity } = inventory[itemInventoryIndex]
if (quantity > howMany) {
inventory[itemInventoryIndex] = {
...inventory[itemInventoryIndex],
quantity: quantity - howMany,
}
} else {
inventory.splice(itemInventoryIndex, 1)
}
return { ...state, inventory }
}