From d024510e8d2e75683f5499cac54271f1bdbfd498 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Wed, 18 Dec 2019 14:42:20 -0500 Subject: tested and fixed processing bugs in space --- space.js | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) (limited to 'space.js') diff --git a/space.js b/space.js index 8710507..5eecf99 100644 --- a/space.js +++ b/space.js @@ -5,10 +5,11 @@ const fs = require('fs') function chop(string, n){ // chops a string into n-sized chunks. Assumed to be perfect multiple - arr = [] - while (string.length > 0){ - arr += string.slice(0,16); + let arr = []; + for (let sec = 0; sec < string.length; sec++){ + arr.push(string.slice(sec*n,(sec+1)*n)); } + return arr; } function replace(text, old, repl){ //replaces, in an array `text`, the instances of `old` with `repl` for (let i=0; i linenum.parseInt()); // list of included lines in the content + let incl = Object.keys(tile.properties.cell_props).map(linenum => parseInt(linenum)) // list of included lines in the content let cont = chop(tile.content,16); let read = 0; //line of cont to read for (let line=0; line<8; line++){ curline = line+8*tilerow; - if (incl.includes(line)){ - this.data[curline] += cont[read].split(''); - read++; + if (conform && incl.includes(line)){ + for (let i=0; i<16; i++) self.data[curline].push(' '); } else { - for (let i=0; i<16; i++) this.data[curline].push(' '); + self.data[curline].push(cont[read].split('')); + read++; } } } + //console.log(this.data); } this.towrite = function(charoffset){ // Does no splitting or anything like that. Just returns a list of triplets for the write function let writes = []; @@ -53,31 +59,30 @@ function Space(){ // CLASS return writes; } this.tofile = function(filename){ - fs.writeFile(filename,this.data.map(row => replace(row,'','&').join('').replace(/\\|&/g, '\\$&')).join('\n')+'\n',(err)=>{console.log(err);}); + fs.writeFile(filename, this.print(), (err)=>{console.log(err)}); }; this.fromfile = function(filename){ //Reads an external file into internal data - fs.readFile(filename, (err, data) => { - if (err) throw err; - self.adhoc(data); - }); + this.adhoc(fs.readFileSync(filename)); } this.adhoc = function(text){ this.data = text.split('\n').map(row => replace( row.replace(/\\(.)/g,'$1').split('') ,'&','' )); } + this.print = function(){ + return this.data.map(row => replace(row,'','&').join('').replace(/\\|&/g, '\\$&')).join('\n'); + } this.comb = function(other, func, offset){ - offsety = offset[0]; - offsetx = offset[1]; - for (let row = offset[0]; row= this.data.length) this.data.push([]); + for (let chr = offset[1]; chr this.data.length) - this.data.push([]); - if (chr > this.data[row].length) + if (chr >= this.data[row].length) this.data[row].push(''); - this.data[row][chr] = func(this.data[row][chr], other.data[row][chr]); - } + let otherchar = other.data[row][chr]; otherchar = (typeof(otherchar) == 'undefined') ? '' : otherchar; + this.data[row][chr] = func(this.data[row][chr], otherchar); + console.log(row,chr,this.data[row][chr]) + }} } this.search = function(other){ loc = []; -- cgit