aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <holden.rohrer@gmail.com>2019-12-18 14:42:20 -0500
committerHolden Rohrer <holden.rohrer@gmail.com>2019-12-18 14:42:20 -0500
commitd024510e8d2e75683f5499cac54271f1bdbfd498 (patch)
treee5e2eab78a094d3b815163626f428c5e08a77389
parent78a5f75d9501774e6a8b12c357afbe8404b10f36 (diff)
tested and fixed processing bugs in space
-rw-r--r--space.js53
-rw-r--r--tests/space_comb.js24
-rw-r--r--tests/space_fromfetch.js9
3 files changed, 62 insertions, 24 deletions
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<text.length; i++){
@@ -16,11 +17,15 @@ function replace(text, old, repl){ //replaces, in an array `text`, the instances
}
return text;
}
+function zeroifnull(obj, prop, alt=0){ //If obj is null, return 0; else obj.prop
+ if (typeof(obj) == 'undefined') return alt;
+ else return obj[prop];
+}
function Space(){ // CLASS
this.data = []; //
self = this;
- this.fromfetch = function(tiles, dimension){ //tiles is straight from fetch function, dimension is a quadruplet
+ this.fromfetch = function(tiles, dimension, conform=true){ //tiles is straight from fetch/tileUpdate function, dimension is a quadruplet; conform is false for tileUpdate because the cell_props don't actually mean anything; all data is still included
for (let y=dimension[0]; y<=dimension[2]; y++){
for (let line=0; line<8; line++)
this.data.push([]); // Adds lines
@@ -30,19 +35,20 @@ function Space(){ // CLASS
}
function tilein(tile, tilerow){ //tile is one of the tiles from `tiles`, and tilerow is y-dimension[0]; helper function
- let incl = Object.keys(tile.properties.cell_props).map(linenum => 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<Math.max(other.data.length+offset[0],this.data.length); row++) for (let chr = offset[1]; chr<Math.max(otherlens.data[row].length+offset[1],this.data[row].length); chr++){
- if (row < 0)
- this.data.unshift([]);
+ for (let row = offset[0]; row<Math.max(other.data.length+offset[0],this.data.length); row++){
+ if (row < 0) this.data.unshift([]);
+ if (row >= this.data.length) this.data.push([]);
+ for (let chr = offset[1]; chr<Math.max(zeroifnull(other.data[row],'length')+offset[1], zeroifnull(this.data[row],'length')); chr++){
if (chr < 0)
this.data[row].unshift('');
- if (row > 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.