Skip to content

Instantly share code, notes, and snippets.

@ndrluis
Last active August 29, 2015 14:18
Show Gist options
  • Save ndrluis/5dce1f7e3a4022980503 to your computer and use it in GitHub Desktop.
Save ndrluis/5dce1f7e3a4022980503 to your computer and use it in GitHub Desktop.
Alma Map Editor
import _ from 'underscore'
export class MapParser{
constructor(mapString){
this.mapString = mapString
}
sizes() {
let match = this.mapString.match(/([0-9]*)&/)[1]
return { x: Number(match.slice(0,2)),
y: Number(match.slice(2,4)) }
}
name() {
return this.mapString.match(/\w\D*/)[0]
}
tiles() {
let rawTiles = this.mapString.match(/([a0-9]*\*[0-9]{3})/g)
let tiles = rawTiles.map(rawTile =>
[{ times: Number(rawTile.match(/\*([0-9]{3})/)[1]),
tile: rawTile.match(/([a0-9]*)\*/)[1] }]
)
return _.flatten(tiles)
}
}
import {MapParser} from '../lib/map-parser.es6'
describe('MapParser', () => {
let mapParser
before(() =>{
let mapString = 'New Map Test1020&1500016041000000*0801600127000000a01*0301500017015000044*200'
mapParser = new MapParser(mapString)
})
describe('#size', () => {
it('returns the map sizes', () => {
let sizes = mapParser.sizes()
expect(sizes).to.eql({ x: 10, y: 20 })
})
})
describe('#name', () => {
it('returns the map name', () => {
let mapName = mapParser.name()
expect(mapName).to.eql('New Map Test')
})
})
describe('#tiles', () => {
it('returns the tiles', () => {
let tiles = mapParser.tiles()
expect(tiles).to.eql([ { times: 80, tile: '1500016041000000' },
{ times: 30, tile: '1600127000000a01' },
{ times: 200, tile: '1500017015000044' } ])
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment