tablatal.js (873B)
1 function Tablatal(data) 2 { 3 this.data = data; 4 5 this.parse = function(type) 6 { 7 var a = []; 8 var lines = this.data.trim().split("\n") 9 var key = make_key(lines[0]); 10 for(id in lines){ 11 if(id == 0 || lines[id].trim() == ""){ continue; } 12 var entry = {}; 13 for(i in key){ 14 entry[i] = lines[id].substr(key[i].from,key[i].to).trim(); 15 } 16 a[a.length] = type ? new type(entry) : entry; 17 } 18 return a 19 } 20 21 function make_key(raw) 22 { 23 var parts = raw.split(" ") 24 var distance = 0; 25 var key = {}; 26 var prev = null 27 for(id in parts){ 28 var part = _.snakeCase(parts[id]); 29 if(part != ""){ 30 key[part] = {from:distance,to:0}; 31 if(key[prev]){ key[prev].to = distance - key[prev].from - 1; } 32 prev = part; 33 } 34 distance += part == "" ? 1 : part.length+1; 35 } 36 return key; 37 } 38 }