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 ++++++++++++++++++++++++++---------------------- tests/space_comb.js | 24 ++++++++++++++++++++++ tests/space_fromfetch.js | 9 ++++++++ 3 files changed, 62 insertions(+), 24 deletions(-) create mode 100644 tests/space_comb.js create mode 100644 tests/space_fromfetch.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 = []; diff --git a/tests/space_comb.js b/tests/space_comb.js new file mode 100644 index 0000000..3fae916 --- /dev/null +++ b/tests/space_comb.js @@ -0,0 +1,24 @@ +// Tests space.comb() + +space = require('../space'); + +newspace = new space.Space(); +newspace.adhoc('\ +line1\n\ +&&&&transparency&&&&\n\ +\n\ +afterempty\n') + +otherspace = new space.Space(); +otherspace.adhoc('\ +&&&transparency&&&\n\ +testline'); + +function add(char1,char2){ + if (char1 == '') return char2; + else return char1; +} + +otherspace.comb(newspace, add, [0,0]); + +console.log(otherspace.print()); diff --git a/tests/space_fromfetch.js b/tests/space_fromfetch.js new file mode 100644 index 0000000..2d8572a --- /dev/null +++ b/tests/space_fromfetch.js @@ -0,0 +1,9 @@ +// Tets Space.fromfetch +const space = require('../space') + +newspace = new space.Space() + +newspace.fromfetch({"4,2": {"content": " hhhhhhhh k hone ns ", "properties": {"writability": null, "cell_props": {}}}},[4,2,4,2],conform=false); +// Straight from a real tileUpdate + +console.log(newspace.data); //Should be an array of arrays of chars, representing the content of "content" above. -- cgit