// space.js, a full (not sparse data storage object) with some utilities. // This includes from/to fetch, write, files, and ad-hoc strings. [DONE] [DONE] [DONE] [DONE] [DONE] // It provides combination between Spaces. [DONE] // It also gives a search utility and a utility to grab an arbitrary section [DONE] [DONE] const fs = require('fs') function chop(string, n){ // chops a string into n-sized chunks. Assumed to be perfect multiple 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 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 (conform && incl.includes(line)){ for (let i=0; i<16; i++) self.data[curline].push(' '); } else { Array.prototype.push.apply(self.data[curline],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 = []; for (let line = 0; line < this.data.length; line++) for (let chr = 0; chr< this.data[line].length; chr++){ if (this.data[line][chr] == '') continue; writes.push([[charoffset[0]+line,charoffset[1]+chr],this.data[line][chr]]); } return writes; } this.tofile = function(filename){ fs.writeFileSync(filename, this.print()); }; this.fromfile = function(filename){ //Reads an external file into internal data this.adhoc(fs.readFileSync(filename,'utf8')); } this.adhoc = function(text){ text = text.split('\n') text = text.map(row => row.split('')); this.data = text.map(row => { for (let i = 0; i replace(replace(replace(row,'&','\\&'),'\\','\\\\'),'','&').join('')).join('\n'); } this.comb = function(other, func, offset){ for (let row = offset[0]; row= this.data.length) this.data.push([]); for (let chr = offset[1]; chr= this.data[row].length) this.data[row].push(''); this.data[row][chr] = func(this.data[row][chr], other.data[row][chr] || ''); console.log(row,chr,this.data[row][chr]) }} } this.search = function(other){ //Returns first instance of a subspace (prioritized vertically then horizontally) let loc = []; for (let line=0; line<=this.data.length-other.data.length; line++){ for (let chr=0; chr<=this.data[line].length-other.data[0].length; chr++){ var match = true; for (let y=0; y