Files
website/src/scripts/models/inventory_parser.coffee
T
Andrew Miner 0d25ac723d Complete refactoring to move Recipes into Items
* Changed "version" to "dataVersion" in data format
* Changed the "modName" and "modVersion" fields on the ModVersion
  class to remove the "mod" prefix and changed the data format to
  match
* Change the Stack class to use the itemSlug instead of the item
  itself
* Update the ModVersion and ModPack classes to include methods for
  recording a correspondence between slugs and names. Update code
  which displays items from stacks to use these methods to determine
  display names.
* Add a reference from Recipe back to the item it creates
2015-01-03 20:08:11 -08:00

31 lines
859 B
CoffeeScript

###
Crafting Guide - inventory_parser.coffee
Copyright (c) 2014 by Redwood Labs
All rights reserved.
###
Inventory = require './inventory'
Item = require './item'
########################################################################################################################
module.exports = class InventoryParser
@ITEM_REGEX = /^([0-9]+)(.*)$/
parse: (data, inventory=null)->
inventory ?= new Inventory
return inventory if not data? or data.length is 0
lines = data.split '\n'
for line in lines
match = InventoryParser.ITEM_REGEX.exec line
name = if match? then match[2].trim() else line
quantity = if match? then parseInt(match[1]) else 1
if name.length > 0
inventory.add _.slugify(name), quantity
return inventory